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. 解决 windows2012 下无法安装 sql2008R2

    Press the Windows logo key, type control panel, and then click the Control Panel icon. Note If you a ...

  2. Spark Streaming揭秘 Day28 在集成开发环境中详解Spark Streaming的运行日志内幕

    Spark Streaming揭秘 Day28 在集成开发环境中详解Spark Streaming的运行日志内幕 今天会逐行解析一下SparkStreaming运行的日志,运行的是WordCountO ...

  3. [Git]代码管理工具简单使用

    1 Git简介 Git是分布式的版本控制系统,是Linux内核开发者林纳斯·托瓦兹(Linus Torvalds)为更好地管理Linux内核开发而设计.与CVS.Subversion一类的集中式版本控 ...

  4. OCP考试之052

    Oracle Database 11g:Administration I 考试时间:90分钟 考试题目:70题 考试语言:英语 考试分数:66% 考试内容: 了解Oracle数据库体系结构 解释的内存 ...

  5. 使用WebGL实现一个Viewer来显示STL文件

    关键字:WebGL,STL,ThreeJS,Chrome,Viewer,Python3.4, HTML5,Canvas. OS:Windows 10. 本文介绍如何使用ThreeJS来实现一个WebG ...

  6. .NET中的弱引用

    弱引用是什么? 要搞清楚什么是弱引用,我们需要先知道强引用是什么.强引用并不是什么深奥的概念,其实我们平时所使用的.Net引用就是强引用.例如: Cat cat = new Cat(); 变量cat就 ...

  7. C语言字符串与字符数组

    字符串儿与字符数组 字符数组的定义: Char buffer[]; 字符数组初始化: Char buffer1[]="hello world"; 利用scanf输入一个字符串儿 代 ...

  8. fedora 安装chrome

    1. 下载chrome的rpm安装包 2. 安装lsb:   sudo yum install redhat-lsb.x86_64 3. rpm -i google-chrome-{xxxxx}.rp ...

  9. owa Your request can't be completed right now. Please try again later.

    Your request can't be completed right now. Please try again later.

  10. c#用反射原理递归遍历复杂实体对象

    之前在网上看到的都是遍历那种比较简单的实体对象,但是如果有实体嵌套,甚至是包含有List<XXInfo>这种属性的时候就没有办法处理了.通过递归遍历的方式可以完成对复杂实体对象的所有属性的 ...