题目链接:https://vjudge.net/problem/LightOJ-1038

1038 - Race to 1 Again
Time Limit: 2 second(s) Memory Limit: 32 MB

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

Output for Sample Input

3

1

2

50

Case 1: 0

Case 2: 2.00

Case 3: 3.0333333333

题意:

给出一个数n,每次随机变为它的某一个因子(1~n),直到最后变为1。问n平均多少步操作到达1?

题解:

1.假设ci为n的因子,那么:dp[n] = (dp[1]+1 + dp[c2]+1 + dp[c3]+1 + ……  + dp[n]+1)/k,k为因子个数。

2.由于左右两边都有dp[n],那么移项得:dp[n] = (dp[1] + dp[c2] + dp[c3] + ……  + k)/(k-1) 。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; double dp[MAXN];
void init()
{
memset(dp, , sizeof(dp));
for(int i = ; i<MAXN; i++)
{
int cnt = ; double sum = ;
for(int j = ; j<=sqrt(i); j++) if(i%j==) {
sum += dp[j];
cnt++;
if(j!=i/j) sum += dp[i/j], cnt++;
}
dp[i] = 1.0*(sum+cnt)/(cnt-);
}
} int main()
{
init();
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d",&n);
printf("Case %d: %.10lf\n", ++kase, dp[n]);
}
}

LightOJ - 1038 Race to 1 Again —— 期望的更多相关文章

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

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

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

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

  3. 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的 ...

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

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

  5. Lightoj 1038 - Race to 1 Again【期望+dp】

    题目:戳这里 题意:一个数字n不断迭代地除以自身的因子得到1.求这个过程中操作除法次数的期望. 解题思路: 求概率基本都是从一个最基础的状态开始延伸推出公式,得出答案.因为每个数都有个共同的最终状态1 ...

  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,记忆化搜索)

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

  8. Race to 1 Again LightOJ - 1038

    Race to 1 Again LightOJ - 1038 题意:有一个数字D,每次把D变为它的一个因数(变到所有因数的概率相等,可能是本身),变到1后停止.求对于某个初始的D变到1的期望步数. x ...

  9. Day11 - D - Race to 1 Again LightOJ - 1038

    设dp_i为所求答案,每次选择因数的概率相同,设i有x个因数,dp_i=sum(1/x*x_j)+1,(x_j表示第j个因数),那我们就预处理每个数的因数即可,T=10000,需要预处理出答案 #in ...

随机推荐

  1. springboot + mybatis配置多数据源示例

    转:http://www.jb51.net/article/107223.htm 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)Datab ...

  2. linux 木马

    参考 http://wangzan18.blog.51cto.com/8021085/1740113 http://www.cnblogs.com/jluzhsai/p/3756280.html  ( ...

  3. jquery 创建jquery的dom对象---------------获取自身的html节点及其子节点的html

    1.var domObj = $("<dom>"); 2.var a = $("<a href='www.baidu.com'>"); ...

  4. hint指定index的深入理解

    http://czmmiao.iteye.com/blog/1480247创建一个表,含有位图index和b-tree index SQL> create table t as select o ...

  5. layui-简单辅助元素 - 页面元素

    本篇主要集中罗列页面中的一些简单辅助元素,如:引用块.字段集区块.横线等等,这些元素都无需依赖任何模块 <!DOCTYPE html> <html> <head> ...

  6. 深度 | Facebook的图像识别很强大,一次开源了三款机器视觉工具(附论文)

    http://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&mid=2650718597&idx=1&sn=56aa4e5deff9962 ...

  7. jdbc 简单连接

    package itcast; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;i ...

  8. 编写mipsel mt7620 Led驱动(一)

    1.看原理图中知芯片上66引脚控制一个LED 2.在Datasheet中找出GPIO pin 3.在ProgrammingGuid  System Contrl中找到GPIO控制寄存器地址: 4.控制 ...

  9. RPMBUILD源码打包资源汇总(转)

    http://mattshma.github.io/2015/11/04/rpm%E6%89%93%E5%8C%85/ http://400053.blog.51cto.com/390053/7210 ...

  10. ASP.NET动态网站制作(3)--css(2)

    前言:css分为四次课讲完,第一节课内容见ASP.NET动态网站制作(2)--css(1),接下来的内容会涉及到定位.浮动.盒子模型(第二次课).css的具体应用(第三次课).css3(第四次课).今 ...