UVA-11761 - Markov/Memoized Search

https://vjudge.net/problem/UVA-11762

    Given an integer n, randomly select a prime number less than or equal to n each time, if it is a factor of n, n becomes n/x, otherwise unchanged, ask the expected number of times n becomes 1.

  f[i]=1/(m1+m2)*(SUM{ f[i] } + SUM{ f[i/x] }) +1, it is good to memorize the search after simplification. When sieving prime numbers, you need to Saves all prime numbers so it can't be optimized for radicals.

  

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<cmath>
 9 #include<ctime>
10 #include<time.h> 
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define pb push_back
15 #define debug puts("debug")
16 #define LL long long 
17 #define pii pair<int,int>
18 #define eps 1e-10
19 bool is[1001000];
20 int prime[100000],tot;
21 double f[1001000];
22 void init(){
23     is[0]=is[1]=1;
24     for(LL i=2;i<=1000000;++i){
25         if(!is[i]){
26             prime[tot++]=i;
27             for(LL j=i*i;j<=(LL)1000000;j+=i)
28                is[j]=1;
29         }
30     }
31 }
32 double dfs(int u){
33     if(f[u]) return f[u];
34     if(u==1) return f[u]=0;
35     int g=0,p=0;
36     f[u]=0;
37     for(int i=0;i<tot&&prime[i]<=u;++i){
38         p++;
39         if(u%prime[i]==0){
40             f[u]+=dfs(u/prime[i]);    
41             g++;
42         }
43     }
44 f[u]=(f[u]+p)/g;
45     return f[u];
46 }
47 int main()
48 {
49     int n,m,i,j,k,t;
50     int cas=0;
51     init();
52     cin>>t;
53     while(t--){
54         memset(f,0,sizeof(f));
55         scanf("%d",&n);
56         printf("Case %d: %.11f\n",++cas,dfs(n));
57     }
58     return 0; 
59 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256189&siteId=291194637