Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can be
divided by at least one prime number less than or equal to that number. 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 prime number less than or equal to D. If D is divisible by the
prime number then he divides D by the prime number to obtain new D. Otherwise he keeps the old
D. He repeats this procedure until D becomes 1. What is the expected number of moves required for
N to become 1.
[We say that an integer is said to be prime if its divisible by exactly two different integers. So, 1 is not
a prime, by definition. List of first few primes are 2, 3, 5, 7, 11, ...]
Input
Input will start with an integer T (T ≤ 1000), which indicates the number of test cases. Each of the
next T lines will contain one integer N (1 ≤ N ≤ 1000000).
Output
For each test case output a single line giving the case number followed by the expected number of turn
required. Errors up to 1e-6 will be accepted.
Sample Input
3
1
3
13
Sample Output
Case 1: 0.0000000000
Case 2: 2.0000000000
Case 3: 6.0000000000

题意:给出一个整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/P,否则N不变。问平均情况下需要多少次选择,才能把N变成1.

题解:记录dp[n] 表示 N = n是答案是多少

   dp[n] = 1 +  ∑dp[素数因子] *  1/素数总和  +   ∑dp[x] * (x为非因子素数)/ 素数总和

记忆化爆搜就可以

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N=; int H[N + ],P[N],cnt,vis[N + ];
double dp[N + ];
void prime_table() {
H[] = ;
for(int i = ; i <= N ; i++) {
if(!H[i]) {
P[++cnt] = i;
for(int j = * i ; j <= N ; j += i) H[j] = ;
}
}
}
double dfs(int n) {
if(vis[n]) return dp[n];
if(n == ) return dp[n] = ;
double& ans = dp[n];
int sum = , g = ;
vis[n] = ;
for(int i = ; i <= cnt && P[i] <= n; i++) {
sum ++;
if(n % P[i] == ) {
ans += dfs(n / P[i]);
}else g++;
}
return ans = (ans + sum) / (sum - g);
}
int main () {
cnt = ;
prime_table();
int T, cas = , n;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
dfs(n);
printf("Case %d: %.10f\n", cas++, dp[n]);
}
return ;
}

代码

UVA - 11762 - Race to 1 记忆化概率的更多相关文章

  1. UVA 11762 - Race to 1(概率)

    UVA 11762 - Race to 1 题意:给定一个n,每次随即选择一个n以内的质数,假设不是质因子,就保持不变,假设是的话.就把n除掉该因子,问n变成1的次数的期望值 思路:tot为总的质数. ...

  2. UVa 11762 Race to 1 (数学期望 + 记忆化搜索)

    题意:给定一个整数 n ,然后你要把它变成 1,变换操作就是随机从小于等于 n 的素数中选一个p,如果这个数是 n 的约数,那么就可以变成 n/p,否则还是本身,问你把它变成 1 的数学期望是多少. ...

  3. UVa 11762 - Race to 1

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVa 10599【lis dp,记忆化搜索】

    UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...

  5. UVa 1629 切蛋糕(记忆化搜索)

    https://vjudge.net/problem/UVA-1629 题意: 有一个n行m列的网格蛋糕上有一些樱桃.每次可以用一刀沿着网格线把蛋糕切成两块,并且只能直切不能拐弯.要求最后每一块蛋糕上 ...

  6. UVa 10118 Free Candies (记忆化搜索+哈希)

    题意:有4堆糖果,每堆有n(最多40)个,有一个篮子,最多装5个糖果,我们每次只能从某一堆糖果里拿出一个糖果,如果篮子里有两个相同的糖果, 那么就可以把这两个(一对)糖果放进自己的口袋里,问最多能拿走 ...

  7. UVA 11762 Race to 1(记忆化+期望)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操 ...

  8. [uva 11762]Race to 1[概率DP]

    引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...

  9. UVA 11468 AC自动机入门题 记忆化概率dp+ac自动机

    /** 链接:https://vjudge.net/problem/UVA-11468 详见lrj训练指南P218 我的是反向求存在模板串的概率. dp[i][j]表示当前i位置选择字符,前面i-1个 ...

随机推荐

  1. docker(部署常见应用):docker部署nginx

    上回说到centos安装docker. 这次用实战,docker部署运行常见的应用. docker常用命令 参看:docker命令大全.这里不做赘述. docker部署nginx 1.docker h ...

  2. flash as3.0学习笔记

    F9开动作模板 trace输出 trace(a); 影片剪辑 var mc:MovieClip = new MovieClip();//属性(x,y轴)方法 play,stop mc.x = 10 / ...

  3. 项目中遇到的所有ECharts图表集合

    全放在了ECharts官网示例里面以后会一直往里面添加: https://gallery.echartsjs.com/explore.html?u=bd-2133619855&type=wor ...

  4. 同一sql程序执行比数据库执行慢

    最近项目发现同一个sql在java端执行比在数据库执行慢很多,原因可能是程序的sql参数类型与数据库字段的类型不一致.

  5. Super超级ERP系统---(4)采购管理--采购单创建

    Erp系统中采购是系统必不可少的一部分,也就是ERP种的进货模块,超级ERP系统中的采购模块选选择采购供应商,然后选择进货商品的数量和采购价格,创建采购进货单 1.创建采购单 2.审核采购单 采购单创 ...

  6. DataGridView属性设置汇总

    1.标题列居中 外观  ColumnHeadersDefaultCellStyle - Alignment - MiddleCenter 2.表格内容居中 外观  DefaultCellStyle - ...

  7. 使用Caffe预测遇到的问题

    1. 在使用网络预测图像时, prediction = net.predict( [input_image] ) 出现: net.image_dims[0] 不是整数情况, (2).甚至以为np.ze ...

  8. Swift - 关键字(typealias、associatedtype)

    Typealias typealias 是用来为已经存在的类型重新定义名字的,通过命名,可以使代码变得更加清晰.使用的语法也很简单,使用typealias 关键字像使用普通的赋值语句一样,可以将某个已 ...

  9. git基础讲解

    idea :https://blog.csdn.net/autfish/article/details/52513465 eclipse:https://blog.csdn.net/yang57266 ...

  10. JS 封装一个求圆面积的函数 传值:半径

    y(6) var s = ""; function y (r){ s = Math.PI*r*r; alert(s); }