zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    HTML总结
    js五角星评分特效
    正则表达式
    C#文件路径的写法
    vs2010发布网站
    INI文件阅读器
    .net读取xml文件中文乱码问题解决办法
    js利用定时器动态改变层大小
    c#中ref和out的用法
    分享一篇关于C#对文件操作的日志,方法很全
  • 原文地址:https://www.cnblogs.com/wmj6/p/11254208.html
Copyright © 2011-2022 走看看