LightOJ - 1038 Race to 1 Again —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1038
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 —— 期望的更多相关文章
- 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,然后让 ...
- 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 递推+期望
题目大意:给出一个数,要求你按一定的规则将这个数变成1 规则例如以下,如果该数为D,要求你在[1,D]之间选出D的因子.用D除上这个因子,然后继续按该规则运算.直到该数变成1 问变成1的期望步数是多少 ...
- Lightoj 1038 - Race to 1 Again【期望+dp】
题目:戳这里 题意:一个数字n不断迭代地除以自身的因子得到1.求这个过程中操作除法次数的期望. 解题思路: 求概率基本都是从一个最基础的状态开始延伸推出公式,得出答案.因为每个数都有个共同的最终状态1 ...
- 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,记忆化搜索)
题意:给定一个数 n,然后每次除以他的一个因数,如果除到1则结束,问期望是多少. 析:概率DP,可以用记忆公搜索来做,dp[i] = 1/m*sum(dp[j] + 1) + 1/m * (dp[i] ...
- Race to 1 Again LightOJ - 1038
Race to 1 Again LightOJ - 1038 题意:有一个数字D,每次把D变为它的一个因数(变到所有因数的概率相等,可能是本身),变到1后停止.求对于某个初始的D变到1的期望步数. x ...
- 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 ...
随机推荐
- git pull“No remote repository specified”解决方法
git pull“No remote repository specified”解决方法 学习了:http://www.paopaoche.net/jiaocheng/77226.html 修改“.g ...
- C 作用域规则
C 作用域规则 任何一种编程中,作用域是程序中定义的变量所存在的区域,超过该区域变量就不能被访问.C 语言中有三个地方可以声明变量: 在函数或块内部的局部变量 在所有函数外部的全局变量 在形式参数的函 ...
- AWK 思维导图
完整的AWK思维导图 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing
- 求解复数组 中模较大的N个数
//求解复数组 中模较大的N个数 void fianN_Complex(Complex outVec[], int& len, std::vector<int>& inde ...
- linux配置nfs步骤及心得
这节我们介绍NFS的相关概念,以及怎样配置NFS和在client中查看NFS. NFS的配置过程非常easy. 在server端中编辑/etc/exports文件,加入例如以下内容: ...
- virtualbox 4.3.10 ubuntu 12.04 mount share folder bug
virtualbox 4.3.10 不能mount共享文件夹,这是一个bug,参考如下链接 https://www.virtualbox.org/ticket/12879 执行以下命令即可:sudo ...
- ss请cc来家里钓鱼,鱼塘可划分为n*m的格子,每个格子有不同的概率钓上鱼,cc一直在坐标(x,y)的格子钓鱼,而ss每分钟随机钓一个格子。问t分钟后他们谁至少钓到一条鱼的概率大?为多少?
include "stdafx.h" #include<iostream> #include<vector> #include<math.h> ...
- Android中处理崩溃异常和记录日志(转)
现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了崩溃现象,开 ...
- 不同特权级间代码段的跳转{ 门 + 跳转(jmp + call) + 返回(ret) }
[0]写在前面 0.1)我们讲 CPU的保护机制,它是可靠的多任务运行环境所必须的: 0.2) CPU保护机制:分为段级保护 + 页级保护: 0.2.1)段级保护分为:段限长 limit 检查.段类型 ...
- Python学习总结之三 -- 优雅的字符串
优雅的字符串 前言 记得我在Python学习总结第一篇中有提到字符串,那个可以算是先打个招呼吧,因为没有提到任何关于字符串的处理方法.今天,给大家详细讲解一下Python中字符串的使用方法,如有不当或 ...