uva11762 Race to 1

175 篇文章 0 订阅
137 篇文章 0 订阅

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 de nition. List of rst 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.

dp(i) 表示从 i 开始的期望步数,有n个不超过 i 的质数,其中有m个是 i 的因数,分别为a1..am,那么有

dp(i)=1+(1mn)dp(i)+j=1mdp(iaj)1n

化简得
dp(i)=mj=1dp(iaj)+nm

记忆化搜索。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
const int maxx=1000000;
int prm[1000010],tot,have[1000010];
double dp[1000010];
double dfs(int x)
{
    if (x==1) return 0;
    if (dp[x]) return dp[x];
    int m=0,n=0,i;
    for (i=1;i<=tot&&prm[i]<=x;i++)
    {
        n++;
        if (x%prm[i]==0) dp[x]+=dfs(x/prm[i]),m++;
    }
    return dp[x]=(dp[x]+n)/m;
}
int main()
{
    int T,K,i,j,x;
    for (i=2;i<=maxx;i++)
    {
        if (!have[i]) prm[++tot]=i;
        for (j=1;j<=tot&&(LL)prm[j]*i<=maxx;j++)
        {
            have[prm[j]*i]=1;
            if (i%prm[j]==0) break;
        }
    }
    scanf("%d",&T);
    for (K=1;K<=T;K++)
    {
        scanf("%d",&x);
        printf("Case %d: %.10f\n",K,dfs(x));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值