UVa11762 Race to 1

期望DP

 

一个数只能分解成不大于它的数,那么转移构成拓扑关系。

 

试了一下预处理出不大于x的质数个数,然而程序并没有变快

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=1000100;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
14     return x*f;
15 }
16 double f[mxn];
17 int pri[mxn],cnt=0,sum[mxn];
18 bool vis[mxn];
19 void init(){
20     for(int i=2;i<mxn;i++){
21         if(!vis[i]){pri[++cnt]=i;sum[i]++;}
22         for(int j=1;j<=cnt && (long long)i*pri[j]<mxn;j++){
23             vis[i*pri[j]]=1;
24             if(i%pri[j]==0)break;
25         }
26     }
27 //    for(int i=2;i<mxn;i++){sum[i]+=sum[i-1];};
28     return;
29 }
30 double DFS(int x){
31     if(x==1)return 0;
32     if(f[x])return f[x];
33     double res=0;int c=0;
34     int n=0;
35     for(int i=1;i<=cnt && pri[i]<=x;i++){
36         n++;
37         if(x%pri[i]==0){
38             c++;
39             res+=DFS(x/pri[i]);
40         }
41     }
42 //    res=(res+sum[x])/(double)c;
43     res=(res+n)/(double)c;
44     return f[x]=res;
45 }
46 int main(){
47     int i,j,x;
48     int T=read(),cas=0;
49     init();
50     while(T--){
51         x=read();
52         printf("Case %d: %.10f\n",++cas,DFS(x));
53     }
54     return 0;
55 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6284994.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值