Eddy Walker(随机数猜概率)

题目链接:https://ac.nowcoder.com/acm/contest/882/A

题意:n个点的环,初始在0,可以随机向前向后,n个位置都走完,最后停在m的概率,最后输出前i组概率的乘积

题解:打表发现,1 ~ n-1的点概率为\frac{1}{n-1},特别情况,n=1,m=0概率为1,其他m=0概率为0

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<cstdlib>
#include<ctime> 
using namespace std;
typedef long long ll;
//bool vis[1005];
//int num[1005];
const int mod=1e9+7;
ll ksm(ll a,ll b){
	ll s=1;
	while(b){
		if(b&1){
			s%=mod;
			a%=mod;
			s*=a;
		}
		a%=mod;
		a*=a;
		b>>=1;
	}
	return s%mod;
}
int main(){
//    随机数求概率,找规律
//    srand((unsigned)time(NULL));
//    int n;
//    while(cin>>n){  
//	    memset(num,0,sizeof(num));
//    	for(int i=1;i<=1000000;i++){
//		    memset(vis,0,sizeof(vis));
//			int pos=0;
//	        int cnt=1;
//	        vis[0]=true;
//	        while(cnt<n){
//	        	int f=rand()%2;
//	    		if(!f) f=-1;
//	    		pos+=f;
//	    		pos=(pos%n+n)%n;
//	    		if(!vis[pos]){
//	    			vis[pos]=true;
//	    			cnt++;
//				}
//				if(cnt==n){
//					num[pos]++;
//				}
//			}
//		}
//		for(int i=0;i<n;i++){
//			cout<<i<<": "<<num[i]<<endl;
//		}
//	}
    int T;
    scanf("%d",&T);
    ll ans=1;
    while(T--){
    	ll cnt;
    	int n,m;
    	scanf("%d%d",&n,&m);
    	if(n==1&&m==0) cnt=1;
    	else if(n>1&&m==0) cnt=0;
    	else if(n>1&&m>0) cnt=ksm(n-1,mod-2);
    	ans*=cnt;
    	ans%=mod;
    	printf("%lld\n",ans);
	}
	return 0;
}



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值