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 ...
随机推荐
- poj3565Ants(KM-几何与图论的结合)
链接 可以看出蓝的之和一定比红的之和要大,也就是说符合条件的匹配一定是权值最小的,所以二分图的最佳完美匹配..KM #include <iostream> #include<cstd ...
- (二)ubuntu学习前传—uboot常见命令
1.uboot命令类似于linux行缓冲命令行,当我们向终端命令行输入命令的时候,这些命令没有立即被系统识别,而是被缓冲到一个缓存区(也就是系统认为我换没有输入完),当我们按下回车键(换行)后,系统就 ...
- go循环
Go语言里的For循环语句 更多 0 golang package main import "fmt" func main() { sum := 0 for i := 0; i ...
- ZendStudio的配置导出
File(文件)->Export(导 出),再弹出Export窗口中点击"General(常规)",选择"Preferences(首选项)" 点击&quo ...
- Java 两个变量交换值
package test; public class Test { public static void main(String[] args) { int a, b; ...
- DRUPAL点滴
1 admin/config/user-interface/xxx在hook_menu里定义这样一个path,就会在configuration界面看到xxx的链接 2 $news_items = db ...
- Docker 使用指南 (一)—— 基本操作
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/98来源:腾云阁 https://www.qcloud ...
- Laravel多对多简析
首先生成两张数据表,一般要实现两张数据表之间的联系要建立第三张表,如下 数据表生成之后,生成一些测试数据,接下来就对表article_tag表进行操作 在模型文件中声明两张表之间的关系: 测试数据:
- Windows下安装并设置Redis
Redis对于Linux是官方支持的,安装和使用没有什么好说的,普通使用按照官方指导,5分钟以内就能搞定.详情请参考: http://redis.io/download 但有时候又想在windows下 ...
- WPF:xmal 静动态资源
<StackPanel.Resources> <SolidColorBrush x:Key="myBrush" Color="Teal"/&g ...