C - Race to 1 Again

C - Race to 1 Again

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 ≤ 105).

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

分析:

因 为 所 有 数 最 终 都 会 到 达 1 , 所 以 用 d p [ i ] , 表 示 i 到 1 的 的 操 作 期 望 因为 所有数最终都会到达1,所以用dp[i],表示i到1的的操作期望 1dp[i]i1

d p [ i ] = ∑ d ∣ i ( d p [ d ] + 1 ) ∗ ( 1 / t o t ) , t o t 代 表 i 的 因 子 的 个 数 dp[i]=\sum _{d|i}(dp[d]+1)*(1/tot),tot代表i的因子的个数 dp[i]=di(dp[d]+1)(1/tot)toti

d = i d=i d=i的时候,这时候将等式右边移项即可

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+10;
const int branch=26;
const int inf=0x7fffffff;
const ll mod=1e9+7;
double dp[maxn];//n到i的期望
int tot;
void init()
{
    dp[1]=0;
    for(int k=2; k<=100000; ++k)
    {
        tot=2;
        dp[k]=2.0;
        for(int i=2; i*i<=k; ++i)//求出小于k的所有因子
            if(k%i==0)
            {
                dp[k]+=dp[i]+1;
                tot++;
                if(i!=k/i)
                {
                    dp[k]+=dp[k/i]+1;
                    tot++;
                }
            }
        dp[k]/=tot-1;
    }
}
int main()
{
    int cas=0;
    int n,t;
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("Case %d: %.7f\n",++cas,dp[n]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值