UVA 11762 - Race to 1

题意:给定一个n,每次随即选择一个n以内的质数,假设不是质因子,就保持不变,假设是的话。就把n除掉该因子,问n变成1的次数的期望值

思路:tot为总的质数。cnt为质因子个数,那么f(n)=(1−cnt/tot)∗f(n)+∑f(n/prime)∗(1/tot),然后利用记忆化搜索去做就可以

代码:

#include <stdio.h>
#include <string.h> const int N = 1000005;
int t, n, prime[N], pn = 0, vis[N];
double f[N]; void get_table() {
for (int i = 2; i < N; i++) {
if (vis[i]) continue;
prime[pn++] = i;
for (int j = i; j < N; j += i)
vis[j] = 1;
}
} double dfs(int n) {
if (f[n] != -1) return f[n];
f[n] = 0;
if (n == 1) return f[n];
int tot = 0, cnt = 0;
for (int i = 0; i < pn && prime[i] <= n; i++) {
tot++;
if (n % prime[i]) continue;
cnt++;
f[n] += dfs(n / prime[i]);
}
f[n] = (f[n] + tot) / cnt;
return f[n];
} int main() {
get_table();
for (int i = 0; i < N; i++) f[i] = -1;
int cas = 0;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
printf("Case %d: %.7lf\n", ++cas, dfs(n));
}
return 0;
}

UVA 11762 - Race to 1(概率)的更多相关文章

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

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

  2. Race to 1 UVA - 11762 (记忆dp概率)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  3. UVA - 11762 - Race to 1 记忆化概率

    Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can bed ...

  4. UVa 11762 - Race to 1

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

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

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

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

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

  7. UVa 11762 (期望 DP) Race to 1

    设f(x)表示x转移到1需要的次数的期望,p(x)为不超过x的素数的个数,其中能整除x的有g(x)个 则有(1-g(x)/p(x))的概率下一步还是转移到x,剩下的情况各有1/p(x)的概率转移到x/ ...

  8. UVa 10491 Cows and Cars (概率&广义三门问题 )

    10491 - Cows and Cars Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onli ...

  9. UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)

    UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...

随机推荐

  1. Application、Activity Stack 和 Task的区别

    Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...

  2. 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

    Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and ...

  3. Vue初识:一个前端萌新的总结

    一.前言 时隔三年,记得第一次写博客还是2015年了,经过这几年的洗礼,我也从一个后端的小萌新变成现在略懂一点点知识的文青.如今对于前端的东东也算有一知半解,个人能力总的来说,也能够独立开发产品级项目 ...

  4. centos7安装mysql和mysql-connector-c++

    最近为了搭建自己的开发环境,又一次在centos7上面开始安装mysql和c++的访问环境,特此记录一下搭建过程,方便以后查阅 一.安装mysql centos7 默认安装了mariaDB,导致不能安 ...

  5. CF832B Petya and Exam

    思路: 模拟. 实现: #include <iostream> using namespace std; string a, b; ]; bool solve() { ) return f ...

  6. Android常用的Dialog对话框用法

    Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的是V7 包里的AlertDialog. im ...

  7. 【PostgreSQL-9.6.3】触发器实例

    1. 创建一个触发器,表中的行在任何时候被插入或更新时,当前用户名和时间也会被标记在该行中.并且它会检查雇员的姓名以及薪水. --创建测试表 CREATE TABLE emp ( empname te ...

  8. 如何利用Flashback Query 恢复误删除的数据

    网上有很多关于数据回复的文章,这里整理一篇供大家参考,希望能帮助的大家! 推荐一家即时通讯云服务商:www.yun2win.com,功能包含im即时通讯.实时音视频.电子白板.屏幕共享的多种融合通讯云 ...

  9. 删除ListView item数据 页面不刷新

    最近碰到一个匪夷所思的事情.就是我删除listView中一条item数据  网络请求成功了 但是页面不成功,一番折腾 ,找到问题 ,原来我给item 添加了北京点击事假,又给listView 被禁设置 ...

  10. Python 之读取大文件readline与readlines的差别

    import time def get_all_lines(filename): start_time = time.time() try: f = open(filename, 'rb') exce ...