题目链接: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. Codeforces445A_DZY Loves Chessboard(预处理)

    DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. JAVA Eclipse 出现 load id=gralloc != hmi-id=gralloc怎么办

    一般是应用程序权限导致的,在Manifest.xml文件中,targetSdkVersion设置不正确,你可以直接删掉这个信息                              

  3. WIN SERVER8更改MYSQL的datadir后,数据库启动不起来

    曾经一直都没注意的问题.这次就记录下来吧.假设有相同问题的也当做一个提示 WIN SERVER8更改MYSQL的datadir后,数据库启动不起来,详细原因有二. 首先.先来看一下怎样更改 datad ...

  4. hibernate uuid

  5. JavaScript replace 回调函数用法

    params.query = params.query.replace(/[\+\-\&\|\!\(\)\{\}\[\]\^\"\~\*\?\:\\]/g,function(a){ ...

  6. Shell脚本之:while

    while循环用于不断执行一系列命令,也用于从输入文件中读取数据:命令通常为测试条件.其格式为: while command do Statement(s) to be executed if com ...

  7. HttpClient 模拟登录网易微博

          实现核心:代码执行流程,根据抓包工具,模拟浏览器请求步骤走       private static void testLogin()       {             try    ...

  8. MVC中的ViewData、ViewBag和TempData

    一.ViewBag和ViewData的定义 public dynamic ViewBag { get; } public ViewDataDictionary ViewData { get; set; ...

  9. jQuery Validate(二)

    刚刚试了所谓的新版的用法.千万别问我是多新,因为我也不知道... <!DOCTYPE html> <html> <head> <script src=&quo ...

  10. openwrt patch

    一: 这几天使用一款电信的4G网卡,发现了一些问题,所以决定打个pitch来解决问题,顺便把patch的生成与使用学习一下 二:安装patch的管理工具quilt 1. sudo apt-get in ...