Race to 1 概率dp

Time Limit: 10000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

 

B

Race to 1

Input: Standard Input

Output: Standard Output

 

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

In each turn he randomly chooses a prime number less than or equal to D. If D is divisible by the prime number then he divides D by the prime number to obtain new D. Otherwise he keeps the old D. He repeats this procedure until D becomes 1. What is the expected number of moves required for N to become 1.

[We say that an integer is said to be prime if its divisible by exactly two different integers. So, 1 is not a prime, by definition. List of first few primes are 2, 3, 5, 7, 11, …]

Input

Input will start with an integer T (T <= 1000), which indicates the number of test cases. Each of the next T lines will contain one integer N (1 <= N <= 1000000).

Output

For each test case output a single line giving the case number followed by the expected number of turn required. Errors up to 1e-6 will be accepted.

Sample Input                             Output for Sample Input

3

1

3

13

  

Case 1: 0.0000000000

Case 2: 2.0000000000

Case 3: 6.0000000000

 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <vector>
 7 using namespace std;
 8 #define maxn 1000010
 9 int a[maxn]={0},b[maxn],t;
10 void init()
11 {
12     long long i,j;
13     t=0;
14     for(i=2;i<maxn;i++)
15     {
16         if(!a[i])
17         {
18             b[t++]=i;
19             j=i*i;
20             while(j<maxn)
21             {
22                 a[j]=1;
23                 j+=i;
24             }
25         }
26     }
27 }
28 double fun(int n)
29 {
30     double sum=0;
31     int i,x=0,y=0;
32     for(i=0;b[i]<=n&&i<t;i++)
33     {
34         x++;
35         if(n%b[i]==0)
36         {
37             y++;
38             sum+=fun(n/b[i]);
39         }
40     }
41     sum+=x;
42     if(y)
43     return sum/y;
44     else return sum;
45 }
46 int main()
47 {
48     init();
49     int i,j,t,n;
50     scanf("%d",&t);
51     for(i=1;i<=t;i++)
52     {
53         scanf("%d",&n);
54         printf("Case %d: %lf
",i,fun(n));
55     }
56 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3646792.html