题目链接: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. jsp中jquery用法一步刷新 验证用户名是否存在

    <script type="text/javascript"> /* $(document).ready(function(){ var id="ha&quo ...

  2. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序         本章节你将学到: SP中工作流的新功能: 理解工作流管理服务 ...

  3. &lt;LeetCode OJ&gt; 141 / 142 Linked List Cycle(I / II)

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  4. 【GoldenGate】使用OGG,两个Oracle库之间单向同步数据

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  5. Centos 7.0防火墙问题

    从Centos7开始,自带的防火墙从iptables更改成了firewall.一般在企业环境,出于人力和稳定性考虑,还是用成熟的技术比较稳妥. 以下是关闭firewall的方法 systemctl s ...

  6. 【selenium】Selenium基于Python3的Web自动化测试脚本在IE上运行慢的解决方法

    阐述问题: 执行自动化脚本时,发现文本输入在IE浏览器上特别慢,这样大大降低了自动化效率 解决办法:原因是原先下载的IEDriverServer.exe为64位系统的IE,换为32位的IEDriver ...

  7. golang手动管理内存

    作者:John Graham-Cumming.   原文点击此处.翻译:Lubia Yang(已失效) 前些天我介绍了我们对Lua的使用,implement our new Web Applicati ...

  8. Angualr 实现复选框全选功能

    html <html lang="en"> <head> <meta charset="UTF-8"> <title& ...

  9. 查看SELinux状态并关闭SELinux

    SELinux(Security-Enhanced Linux)是Linux上最杰出的新安全子系统.在linux内核级别上提供了一个灵活的强制访问控制系统(MAC),这个强制访问控制系统是建立在自由访 ...

  10. CentOS7时间设置及ntp同步配置(转)

    出处:http://www.centoscn.com/CentOS/config/2015/1105/6385.html http://www.centoscn.com/CentOS/config/2 ...