2019牛客暑期多校训练营(第二场)A Eddy Walker 打表规律?

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

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

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

打表:

#include 
using namespace std;
int vis[30];
int book[30];
int cnt;
int main() {
	int pos, cnt;
	int x;
	int n;
	cin >> n;
	for(int i = 1; i <= 1000000; i++) {
		memset(vis, 0, sizeof(vis));
		vis[0] = 1;
		
		pos = 0;
		cnt = 1;
		while(cnt < n) {
			x = rand() % 2 ? 1 : -1;
			pos += x;
			pos = (pos % n + n) % n;
			if(!vis[pos]) {
				vis[pos] = 1;
				cnt++;
			}
			if(cnt == n) {
				book[pos]++;
			}
		}
	}
	
	
	for(int i = 0 ; i < n; i++) {
		cout << i << " " << book[i] << endl;
	}
	return 0;
} 

代码:

#include 
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1050;
ll ans[N];
ll ksm(ll x, ll y) {
	ll res = 1;
	while(y) {
		if(y & 1) res = res * x % mod;
		x = x * x % mod;
		y >>= 1;
	}
	return res;
}
int main() {
	int T;
	int n, m;
	scanf("%d", &T);
	ans[0] = 1;
	for(int i = 1; i <= T; i++) {
		scanf("%d %d", &n, &m);
		if(n == 1) ans[i] = 1;
		else if(m == 0) ans[i] = 0;
		else ans[i] = ksm(n - 1, mod - 2);
		ans[i] = ans[i] * ans[i - 1] % mod;
	}
	for(int i = 1; i <= T; i++) printf("%lld\n", ans[i]);
	return 0;
}

 

你可能感兴趣的:(打表)