UVa 11762 - Race to 1
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2862
晕 多加了一个# wa了N久 细节呀
代码及其注释:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue> #define ull unsigned long long
#define ll long long
#define lint long long
using namespace std; const double eps=1e-12;
const int INF=0x3f3f3f3f;
const int N=1000010;
double dp[N];//记忆化搜索数组
int p[N];//标记是否是素数
int prime[N];//将素数从小到大放到prime数组里面
double dfs(int x)
{
if(dp[x]>=-0.5)
return dp[x];//记忆化
if(x==1)//边界
return (dp[x]=0.0);
double sum=0.0;
int p=0,g=0;//p表示所有小于等于x的素数个数 g表示这些素数中是x的约数的个数
for(p=0;prime[p]<=x;++p)//枚举所以小于等于x的素数
if(x%prime[p]==0)
{
++g;
sum+=dfs(x/prime[p]);
}
return (dp[x]=(sum+p)/g);//这个式子推导可得
}
int main()
{
//freopen("data.in","r",stdin);
memset(p,-1,sizeof(p));
int ln=0;
for(int i=2;i<N;++i)
{
if(p[i])
{
prime[ln++]=i;
for(int j=i+i;j<N;j+=i)
{
p[j]=0;
}
}
}
for(int i=0;i<N;++i) dp[i]=-1.0;
int T;
scanf("%d",&T);
for(int c=1;c<=T;++c)
{
int n;
scanf("%d",&n);
printf("Case %d: %.10lf\n",c,dfs(n));
}
return 0;
}
UVa 11762 - Race to 1的更多相关文章
- UVA 11762 - Race to 1(概率)
UVA 11762 - Race to 1 题意:给定一个n,每次随即选择一个n以内的质数,假设不是质因子,就保持不变,假设是的话.就把n除掉该因子,问n变成1的次数的期望值 思路:tot为总的质数. ...
- [uva 11762]Race to 1[概率DP]
引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...
- UVA 11762 Race to 1(记忆化+期望)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操 ...
- UVa 11762 Race to 1 (数学期望 + 记忆化搜索)
题意:给定一个整数 n ,然后你要把它变成 1,变换操作就是随机从小于等于 n 的素数中选一个p,如果这个数是 n 的约数,那么就可以变成 n/p,否则还是本身,问你把它变成 1 的数学期望是多少. ...
- UVA - 11762 - Race to 1 记忆化概率
Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can bed ...
- 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/ ...
- Race to 1 UVA - 11762 (记忆dp概率)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
- UVa 12034 - Race(递推 + 杨辉三角)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 12034 Race (递推神马的)
Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded ...
随机推荐
- js之oop <一> 创建对象,构造器(class)
js中除了基本类型,就是对象.可以说在js中处处皆对象. 由于js是弱语言,在编写的过程中很容易混淆 object 和 class 也就是对象和构造器. object(对象):一般对象都由var关键字 ...
- hdu3264Open-air shopping malls(二分)
链接 枚举伞的圆心,最多只有20个,因为必须与某个现有的圆心重合. 然后再二分半径就可以了. #include <iostream> #include<cstdio> #inc ...
- 省市县distpicker的使用
下载地址https://github.com/fengyuanchen/distpicker 1.引入 <!-- 引入地址 begin --> <script type=" ...
- 【Todo】Java新技术学习笔记-from某技术分析
看到这篇文章:http://mt.sohu.com/20160806/n462923089.shtml <十余年技术大牛告诉你,这些Java新技术不可错过> 虽然讲的比较泛,但是里面提到的 ...
- WPA/WPA2四次握手
WPA/WPA2四次握手 官方文档:https://en.wikipedia.org/wiki/IEEE_802.11i-2004 The four-way handshake is designed ...
- golang时间
//获取本地location toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板 ...
- spring配置详解
1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...
- PRIMARY LANGUAGE ID not a number
用vs2010修改别人的源代码(估计是vc6下的) .RC 文件,报错: 1>.RC(8): error RC2144: PRIMARY LANGUAGE ID not a number 1 ...
- 《Java程序设计》 实验一 实验报告
实验一 Java开发环境的熟悉(Windows + IDEA) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.完成 ...
- 在ubuntu(linux)下安装vim,以及vim的常用命令
介绍vim: vim是一种编辑器,自我感觉这东西好用,就是现在有些不太习惯 如何安装: 如果是使用redhat ,系统自带着vi和vim 如果是使用ubuntu,建议使用apt-get命令, 可以尝试 ...