C - Race to 1 Again

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

设dp[i]表示i变成1的期望次数,则
dp[i]=(SUM(dp[j])/k)+1,j为i的因子,k为其因子个数
然而当取其因子为1时,j=i/1=i,所以:dp[i]=((SUM(dp[j'])+dp[i])/k)+1,j'为i除开因子i的因子
整理:dp[i]=(SUM(dp[j'])+k)/(k-1)
记忆化搜索即可。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 100000 double dp[N+]; double dfs(int n)
{
if(n==) return dp[n]=;
if(dp[n]!=-) return dp[n];
int k=;
double s=;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
if(i*i!=n)
{
k+=;
if(i!=n) s+=dfs(i);
if(n/i!=n) s+=dfs(n/i);
}
else
{
k+=;
if(i!=n) s+=dfs(i);
}
}
}
return dp[n]=(s+k)/(k-);
}
int main()
{
for(int i=;i<=N;i++) dp[i]=-;
int T,iCase=;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
dfs(n);
printf("Case %d: %.10f\n",iCase++,dp[n]);
}
return ;
}

[LOJ 1038] Race to 1 Again的更多相关文章

  1. LightOJ - 1038 Race to 1 Again —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1038 1038 - Race to 1 Again    PDF (English) Statistics Foru ...

  2. Lightoj 1038 - Race to 1 Again (概率DP)

    题目链接: Lightoj  1038 - Race to 1 Again 题目描述: 给出一个数D,每次可以选择数D的一个因子,用数D除上这个因子得到一个新的数D,为数D变为1的操作次数的期望为多少 ...

  3. LightOJ 1038 - Race to 1 Again(期望+DP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1038 题意是:给你一个N (1 ≤ N ≤ 105) 每次N都随机选一个因子d,然后让 ...

  4. loj 1038(dp求期望)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25915 题意:求一个数不断地除以他的因子,直到变成1的时候 除的次 ...

  5. Light OJ 1038 - Race to 1 Again(概率DP)

    题目的意思是说任何一个大于1的整数,经过若干次除以自己的因子之后可以变为1, 求该变换字数的数学期望值.   题目分析: 我们设置dp[n] 为数字n的期望.假设n的因子为k1, k2, k3.... ...

  6. lightoj 1038 Race to 1 Again

    题意:给一个数,用这个数的因数除以这个数,直到为1时,求除的次数的期望. 设一个数的约数有M个,E[n] = (E[a[1]]+1)/M+(E[a[2]]+1)/M+...+(E[a[M]]+1)/M ...

  7. LightOJ 1038 Race to 1 Again(概率dp+期望)

    https://vjudge.net/problem/LightOJ-1038 题意:给出一个数n,每次选择n的一个约数m,n=n/m,直到n=1,求次数的期望. 思路:d[i]表示将i这个数变成1的 ...

  8. LightOJ 1038 Race to 1 Again (概率DP,记忆化搜索)

    题意:给定一个数 n,然后每次除以他的一个因数,如果除到1则结束,问期望是多少. 析:概率DP,可以用记忆公搜索来做,dp[i] = 1/m*sum(dp[j] + 1) + 1/m * (dp[i] ...

  9. LightOJ - 1038 Race to 1 Again 递推+期望

    题目大意:给出一个数,要求你按一定的规则将这个数变成1 规则例如以下,如果该数为D,要求你在[1,D]之间选出D的因子.用D除上这个因子,然后继续按该规则运算.直到该数变成1 问变成1的期望步数是多少 ...

随机推荐

  1. Kakfa揭秘 Day2 Kafka内核再揭秘

    Spark Streaming揭秘 Day33 Kafka内核再揭秘 优秀的框架会把引擎做到极致,Kafka就是这样,让我们再深入一下研究. 设计目标 kafka系统有着比较独特的的设计,包括5点: ...

  2. iOS通过http post上传图片 (转)

    转载自:http://www.cocoachina.com/bbs/read.php?tid=89985 由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他 ...

  3. Xcode常用设置

    1.设置主题和字体大小 2.设置显示代码行号

  4. Transaction Log Truncation

    --method 1-- ALTER DATABASE KIS_Sample3 SET RECOVERY SIMPLE ) ALTER DATABASE KIS_Sample3 SET RECOVER ...

  5. maven 编译部署src/main/java下的资源文件

    maven 编译部署src/main/java下的资源文件 maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到targ ...

  6. jQuery中的join方法

    和JS 中的JOIN 方法一样,将一数组按照JOIN的参数连接起来.比如: var arr = [ "a", "b", "c", " ...

  7. [BEC][hujiang] Lesson02 Unit1:Working life ---Reading

    2 1.1Working Life p7 reading attitudes to work Question6: 对于Attitude问题 1 I be willing/ unwilling to ...

  8. Deployment of VC2008 apps without installing anything

    If you create a default CRT/MFC application with VS2008, this application will not run on other comp ...

  9. USB Type-C工作原理解析

    自从苹果发布了新MacBook,USB Type-C接口就成为了热议对象.我来从硬件角度解析下这个USB Type-C,以便大家更好的了解USB Type-C的工作原理. 特色 尺寸小,支持正反插,速 ...

  10. cmd启动tomcat

    1.安装jdk 2.安装tomcat 3.需要配置两个用户环境变量,仅仅配置系统变量没用. a)JAVA_HOME:D:\programing~tools\java~tools\JDK(tm)\jdk ...