Race to 1 Again LightOJ - 1038 (概率 dp)

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值