(概率)UVA11762 Race to 1,推导

洛谷题面uva11762
由于感觉蓝书上那个状态转移方程少了一点,百度出一堆不知道写的啥东西,所以自己推了一下
image

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
const int N = 1e6 + 5;
const int maxn = 1e6 + 10;
int p[maxn], v[maxn], cnt;

void Euler(int n){
	cnt = 0;
	for (int i = 2; i <= n; i++) {
		if (!v[i])p[++cnt] = i, v[i] = i;
		for (int j = 1; j <= cnt; j++)
		{
			if (p[j] > v[i] || p[j] > n / i)break;
			v[i * p[j]] = p[j];
		}
	}
}

double dp[maxn];

double dfs(int x)
{
	if (x == 1)return 0;
	if (dp[x] > 0)return dp[x];
	int cnt = 0, tot = 0;
	for (int i = 1; p[i] <= x; i++)
	{
		tot++;
		//cout << p[i] << endl;
		if (x % p[i] == 0)
			cnt++, dp[x] += dfs(x / p[i]);
	}
	return dp[x] = double(dp[x] + tot) / cnt;
}

int main()
{
	Euler(maxn - 5);
	int t;
	scanf("%d", &t);
	int cas = 0;
	while (t--) 
	{
		int x;
		scanf("%d", &x);
		printf("Case %d: %.7lf\n", ++cas, dfs(x));
	}
	return 0;

}
posted @ 2021-11-18 16:46  Lecoww  阅读(37)  评论(0编辑  收藏  举报