lightoj 1038 Race to 1 Again expectations

DP [i] represents the number from i to 1 desired.

dp [i] = Σdp [j] / cnt + 1. (CNT is the number of all factors, and containing 1 i) but Σdp [j] has a dp [i]. The dp [i] are transposed to the left to give dp [i] = (Σdp [j] - dp [i] + cnt) / (cnt - 1). Pretreatment out ahead of time, O (1) can be answered.

 1 #include <cstdio>
 2 #include <cmath>
 3 using namespace std;
 4 double dp[100100];
 5 int n,T,cas;
 6 int main()
 7 {
 8     dp[1] = 0;
 9     for (int i = 2;i <= 100000;i++)
10     {
11         int t = sqrt(i),cnt = 2;
12         double sum = 0;
13         for (int j = 2;j <= t;j++)
14             if (i % j == 0)
15             {
16                 cnt++;
17                 sum += dp[j];
18                 if (i / j != j)
19                 {
20                     cnt++;
21                     sum += dp[i / j];
22                 }
23             }
24         dp[i] = (sum + cnt) / (cnt - 1);
25     }
26     for (scanf("%d",&T);T != 0;T--)
27     {
28         cas++;
29         scanf("%d",&n);
30         printf("Case %d: %.6lf\n",cas,dp[n]);
31     }
32     return 0;
33 }

 

Guess you like

Origin www.cnblogs.com/iat14/p/11410287.html