[LOJ 1038] 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的更多相关文章
- LightOJ - 1038 Race to 1 Again —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1038 1038 - Race to 1 Again PDF (English) Statistics Foru ...
- Lightoj 1038 - Race to 1 Again (概率DP)
题目链接: Lightoj 1038 - Race to 1 Again 题目描述: 给出一个数D,每次可以选择数D的一个因子,用数D除上这个因子得到一个新的数D,为数D变为1的操作次数的期望为多少 ...
- LightOJ 1038 - Race to 1 Again(期望+DP)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1038 题意是:给你一个N (1 ≤ N ≤ 105) 每次N都随机选一个因子d,然后让 ...
- loj 1038(dp求期望)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25915 题意:求一个数不断地除以他的因子,直到变成1的时候 除的次 ...
- Light OJ 1038 - Race to 1 Again(概率DP)
题目的意思是说任何一个大于1的整数,经过若干次除以自己的因子之后可以变为1, 求该变换字数的数学期望值. 题目分析: 我们设置dp[n] 为数字n的期望.假设n的因子为k1, k2, k3.... ...
- 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 ...
- 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的 ...
- LightOJ 1038 Race to 1 Again (概率DP,记忆化搜索)
题意:给定一个数 n,然后每次除以他的一个因数,如果除到1则结束,问期望是多少. 析:概率DP,可以用记忆公搜索来做,dp[i] = 1/m*sum(dp[j] + 1) + 1/m * (dp[i] ...
- LightOJ - 1038 Race to 1 Again 递推+期望
题目大意:给出一个数,要求你按一定的规则将这个数变成1 规则例如以下,如果该数为D,要求你在[1,D]之间选出D的因子.用D除上这个因子,然后继续按该规则运算.直到该数变成1 问变成1的期望步数是多少 ...
随机推荐
- delphi xe memory leak produced in WSDLLookup.pas
constructor TWSDLLookup.Create; begin FLookup := TDictionary<string, Variant>.Create; end; des ...
- Delphi中的四舍五入函数
一.Delphi中的四舍五入法 四舍五入是一种应用非常广泛的近似计算方法,针对不同的应用需求,其有算术舍入法和银行家舍入法两种. 所谓算术舍入法,就是我们通常意义上的四舍五入法.其规则 ...
- Spark Streaming揭秘 Day18 空RDD判断及程序中止机制
Spark Streaming揭秘 Day18 空RDD判断及程序中止机制 空RDD的处理 从API我们可以知道在SparkStreaming中,对于RDD的操作一般都是在foreachRDD和Tra ...
- 在SAE上同步djanogo的mysql数据库
折腾了一个下午,终于搞掂了把djanogo应用的mysql数据库导入到SAE上了,归根到底麻烦的根源是SAE限制多多.下面简单记录一下过程以备日后参考使用. 首先还是修改settings.py,把数据 ...
- linux crontab 命令
Linux 系统提供了使用者控制计划任务的命令 :crontab 命令. 一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划 ...
- html 5 drag and drop upload file
compatible: chrome firefox ie 11 , not supported demo: http://demo.tutorialzine.com/2011/09/html5-fi ...
- input输入框的各种样式
输入框景背景透明: <input style="background:transparent;border:1px solid #ffffff"> 鼠标划过输入框,输入 ...
- Elasticsearch搜索类型(query type)详解
关于我,邯郸人. 对这类话题感兴趣?欢迎发送邮件至donlianli@126.com 请支持原创http://www.cnblogs.com/donlianli/p/3857500.html e ...
- [转]深度理解依赖注入(Dependence Injection)
http://www.cnblogs.com/xingyukun/archive/2007/10/20/931331.html 前面的话:提到依赖注入,大家都会想到老马那篇经典的文章.其实,本文就是相 ...
- POJ1734 - Sightseeing trip
DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...