Race to 1 Again LightOJ
来源:互联网 发布:淘宝状态码-2 编辑:程序博客网 时间:2024/05/21 09:03
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 ≤ 10^5).
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
大致题意:告诉你一个正整数N,每次等概率的选择它的一个约数,然后除掉这个约数,更新N的值,问当N值变成1的期望次数。
思路:假设N的约数a[i]有num个,从小到大编号为1到num,那么a[1]=1,a[num]=N。易知,当一个数除以它的一个约数所得到的数一定是它的另一个约数。假设dp[i]表示将i值变成1所需的期望次数。
那么 dp[N]=(dp[a[1]]+1)/num+(dp[a[2]]+1)/num+….+(dp[a[num]]+1)/num
化解下得 dp[N]=(dp[a[1]]+dp[a[2]]+….+dp[a[num-1]]+num)/(num-1),dp[1]=0。
代码如下
#include<bits/stdc++.h>const int N=1e5+5;#define LL long long intdouble dp[N];void init() { dp[1]=0; for(int i=2;i<N;i++) { int cnt=2; double sum=0; for(LL j=2;j*j<=i;j++) { if(i%j==0) { sum+=dp[j]; if(j*j<i) { sum+=dp[i/j]; cnt++; } cnt++; } } dp[i]=(sum+cnt)/(cnt-1); }}int main(){ init(); int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++) { int n; scanf("%d",&n); printf("Case %d: %.10lf\n",cas,dp[n]); } return 0;}
- Race to 1 Again LightOJ
- LightOJ 1038 Race to 1 Again
- lightoj 1038 - Race to 1 Again
- lightOJ 1038 Race to 1 Again
- LightOJ 1038 - Race to 1 Again 【DP】
- lightoj 1038 Race to 1 Again
- LightOJ 1038 Race to 1 Again
- LIghtOJ 1038 Race to 1 Again
- LightOJ 1038Race to 1 Again
- LightOJ 1038 Race to 1 Again (约数+期望)
- LightOJ 1038 Race to 1 Again 期望 记忆化dp
- 【概率DP】 LightOJ 1038 Race to 1 Again
- LightOJ - 1038 Race to 1 Again 递推+期望
- LightOJ 1038 - Race to 1 Again(dp)
- LightOJ 1038 - Race to 1 Again (期望dp)
- 期望概率dp lightoj 1038 Race to 1 Again
- LightOJ 1038 - Race to 1 Again(期望DP)
- lightoj 1038-Race to 1 Again 期望类DP
- java设计模式(三)——单例模式(静态内部类单例模式)
- 1018. 锤子剪刀布 (20)
- leetcode 313 Super ugly number 解题报告
- Mysql分组以及聚合函数
- c++继承里面的构造函数和析构函数
- Race to 1 Again LightOJ
- CentOS 7下安装Python3.5
- idea14使用maven创建web工程
- Springdata_Jpa注解开发
- Java后台开发必备技能JDBC+Servlet+JSP
- 一次性口令机制java实现(信息安全)
- 全选 删除
- The Necklace UVA10054
- codevs 2611 观光旅游