【UVA】【11762】Race to 1(得到1)

数学期望/马尔可夫过程

  DP/记忆化搜索

  刘汝佳老师白书上的例题……

 1 //UVA 11762
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<iostream>
 7 #include<algorithm>
 8 #define rep(i,n) for(int i=0;i<n;++i)
 9 #define F(i,j,n) for(int i=j;i<=n;++i)
10 #define D(i,j,n) for(int i=j;i>=n;--i)
11 using namespace std;
12 int getint(){
13     int v=0,sign=1; char ch=getchar();
14     while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
15     while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
16     return v*=sign;
17 }
18 const int N=1e6+10;
19 #define debug
20 /******************tamplate*********************/
21 int prime[N],tot;
22 bool check[N];
23
24 void getpr(int n){
25     memset(check,0,sizeof check);
26     F(i,2,n){
27         if (!check[i]) prime[tot++]=i;
28         rep(j,tot){
29             if (i*prime[j]>n)break;
30             check[i*prime[j]]=1;
31         }
32     }
33 }
34 bool vis[N];
35 double f[N];
36 double dp(int x){
37     if (x==1) return 0.0;
38     if (vis[x]) return f[x];
39     vis[x]=1;
40     double &ans = f[x];
41     int g=0,p=0; ans=0;
42     rep(j,tot){
43         if (prime[j]>x) break;
44         p++;
45         if (x%prime[j]==0){ g++; ans+=dp(x/prime[j]);}
46     }
47     ans=(ans+p)/g;
48     return ans;
49 }
50 int main(){
51 #ifndef ONLINE_JUDGE
52     freopen("11762Race_to_1.in","r",stdin);
53     freopen("11762Race_to_1.out","w",stdout);
54 #endif
55     getpr(N-2);
56     int T=getint();
57     F(t,1,T)
58         printf("Case %d: %.10lf\n",t,dp(getint()));
59     return 0;
60 }

时间: 2024-12-14 17:43:32

【UVA】【11762】Race to 1(得到1)的相关文章

uva 11762 - Race to 1(马尔可夫)

题目链接:uva 11762 - Race to 1 题目大意:给出一个整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/P,否则N不变.问平均情况下需要多少次选择,才能把N变成1. 解题思路:马尔可夫,例如N=6时,f(6)=1+f(6)?13+f(4)?13+f(2)?13,1是只第一次转移,后面分别对应的是选择5,2,3的情况.所以有f(x)=∑f(x/y)+p(x)g(x),p(x)为不超过x的素数个数,g(x)为是x因子的素数个数. #include <

UVA 11762 - Race to 1(概率)

UVA 11762 - Race to 1 题意:给定一个n,每次随即选择一个n以内的质数,如果不是质因子,就保持不变,如果是的话,就把n除掉该因子,问n变成1的次数的期望值 思路:tot为总的质数,cnt为质因子个数,那么f(n)=(1?cnt/tot)?f(n)+∑f(n/prime)?(1/tot),然后利用记忆化搜索去做即可 代码: #include <stdio.h> #include <string.h> const int N = 1000005; int t, n,

UVA - 11762 - Race to 1 记忆化概率

Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can bedivided by at least one prime number less than or equal to that number. So, he is now playing withthis property. He selects a number N. And he calls th

UVA - 11762 Race to 1

Dilu have learned a new thingabout integers, which is - any positive integer greater than 1 can be divided byat least one prime number less than or equal to that number. So, he is nowplaying with this property. He selects a number N. And he calls thi

UVA 11762 Race to 1(记忆化+期望)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操作次数,则有: f[x]=1+f[x]*(1-g[x]/p[x])+sigma(f[x][y])/p[x] 进一步为: f[x]=(sigma(f[x/y])+p[x])/g[x] 其中p[x]表示1..x的素数个数,p[x]表示素数中可以整除x的个数. 保留vis可以节约时间. [代码] 1 #i

UVA 12034 - Race(递推)

UVA 12034 - Race 题目链接 题意:给定n匹马,要求出可能的排名情况(可能并列) 思路:递推,dp[i][j]表示i匹马的时候有j种不同名次,那么dp[i][j]可以由dp[i - 1][j - 1]插入j个不同位置得来,或者由dp[i - 1][j]放入已有j的名次得来,得到递推式dp[i][j] = j * (dp[i - 1][j - 1] + dp[i - 1][j]); 然后对于n的答案为sum{dp[n][j]} (1 <= j <= n) 代码: #include

UVa 11762 (期望 DP) Race to 1

设f(x)表示x转移到1需要的次数的期望,p(x)为不超过x的素数的个数,其中能整除x的有g(x)个 则有(1-g(x)/p(x))的概率下一步还是转移到x,剩下的情况各有1/p(x)的概率转移到x/y 根据全期望公式,f(x) = 1 + (1-g(x)/p(x)) * f(x) + sum{ 1/p(x) * f(x/y) | y是能整除x且不超过x的素数 } 代码是用记忆化搜索计算f的 1 #include <cstdio> 2 #include <cstring> 3 #i

Race to 1 UVA - 11762 (记忆dp概率)

#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cma

UVA 12034 Race (递推神马的)

Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded and wandered around, even in their holidays. They passed several months in this way. But everything has an end. A holy person, Munsiji came into their

uva 12034 Race

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma