light oj 1038 DP求期望

来源:互联网 发布:中网数据股份有限公司 编辑:程序博客网 时间:2024/05/17 00:45

题意:求一个数不断地除以他的因子,直到变成1的时候 除的次数的期望


http://www.lightoj.com/volume_showproblem.php?problem=1038


简单期望题

dp[i]=(dp[a1]+1+dp[a2]+1+..dp[ax]+1)/x;

a1->ax为i的所有因子,移项、化简一下就可以得到dp[i]的递推式了

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;double dp[100010];int main(){    int t,ca=1,n;    scanf("%d",&t);    dp[1]=0;    for(int i=2;i<=100000;i++)    {        double sum=0;        int cnt=0;        for(int j=1;j<=sqrt(i*1.0);j++){            if(i%j==0){                sum+=dp[j];cnt++;                if(j!=1 && j != i/j) sum+=dp[i/j],cnt++;            }        }        cnt++;        sum+=cnt;        dp[i]=sum/(cnt-1);    }    while(t--)    {        scanf("%d",&n);         printf("Case %d: %.20lf\n",ca++,dp[n]);    }    return 0;}




原创粉丝点击