Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. 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 divisor of D (1 to D). Then he divides D by the number to obtain new D. He repeats this procedure until D becomes 1. What is the expected number of moves required for N to become 1.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case begins with an integer N (1 ≤ N ≤ 10^5).
Output
For each case of input you have to print the case number and the expected value. Errors less than 10-6 will be ignored.
Sample Input
3
1
2
50
Sample Output
Case 1: 0
Case 2: 2.00
Case 3: 3.0333333333
大致题意:告诉你一个正整数N,每次等概率的选择它的一个约数,然后除掉这个约数,更新N的值,问当N值变成1的期望次数。
思路:假设N的约数a[i]有num个,从小到大编号为1到num,那么a[1]=1,a[num]=N。易知,当一个数除以它的一个约数所得到的数一定是它的另一个约数。假设dp[i]表示将i值变成1所需的期望次数。
那么 dp[N]=(dp[a[1]]+1)/num+(dp[a[2]]+1)/num+….+(dp[a[num]]+1)/num
化解下得 dp[N]=(dp[a[1]]+dp[a[2]]+….+dp[a[num-1]]+num)/(num-1),dp[1]=0。
代码如下
#include<bits/stdc++.h>
const int N=1e5+5;
#define LL long long int
double dp[N];
void init()
{
dp[1]=0;
for(int i=2;i<N;i++)
{
int cnt=2;
double sum=0;
for(LL j=2;j*j<=i;j++)
{
if(i%j==0)
{
sum+=dp[j];
if(j*j<i)
{
sum+=dp[i/j];
cnt++;
}
cnt++;
}
}
dp[i]=(sum+cnt)/(cnt-1);
}
}
int main()
{
init();
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
int n;
scanf("%d",&n);
printf("Case %d: %.10lf\n",cas,dp[n]);
}
return 0;
}