2019牛客暑期多校训练营(第二场)A Eddy Walker(打表求概率)

题意:给你n,m分别表示 长度为n的环 和最后走到的位置m 问从0点出发每次都能能往前或者往后走 求最后在m点的概率
思路:我们可以先打表模拟一下 发现好像每个点的概率大概都是1/(n-1)

打表代码:

#include<bits/stdc++.h>
#include <random>
#include <chrono>
#define ll long long
#define ull unsigned long long
const int inf = 0x3f3f3f3f;
const int N = 4e5+7;
const ll mod = 998244353;
using namespace std;
int t,now,n,x;
int vis[105],book[105];
int main(){
    cin>>n;
    unsigned seed = chrono::high_resolution_clock::now().time_since_epoch().count() ;
    mt19937 rand_generator(seed) ;
    uniform_int_distribution<int> dist(0,1);
    for(int i=1;i<=100000;i++){
        memset(vis,0,sizeof(vis));
        vis[0] = 1;
        now = 0;
        t = 1;
        while(t<n){
            x=dist(rand_generator)?1:-1;
            now+=x;
            now=(now+n)% n;
            if(!vis[now]){
                vis[now]=1;
                t++;
            }
            if(t==n)
                book[now]++;
        }
    }
    for(int i=0;i<n;i++)
        cout<<book[i]*1.0/100000<<' ';
    cout<<endl;
}
View Code

ac代码:

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int N = 1e5+7;
ll q_pow(ll a,ll n){
    ll ans=1; ll base=a;
    while(n){
        if(n&1) ans=(ans*base)%mod;
        base=base*base%mod;
        n>>=1;
    }
        return ans;
}
ll inv(ll a,ll b){
    return q_pow(a,b-2);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    ll ans=1;
    while(t--){
        ll n,m;
        cin>>n>>m;
        if(m==0){
            if(n>1)
            ans=0;
        }
        else{
            ans=(ans%mod*inv(n-1,mod)%mod)%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2019-07-27 10:57  WAKBGAN  阅读(134)  评论(0编辑  收藏  举报