【Uva11762】Race to 1
模拟马尔可夫过程,具体看书。
#include<bits/stdc++.h>
#define N 1000010
using namespace std;
int prime[N],use[N],cnt=;
void calcprime(){
cnt=;memset(use,,sizeof(use));
for(int i=;i<=N;i++){
if(use[i]){prime[++cnt]=i;}
for(int j=;j<=cnt;j++){
int t=prime[j]*i;if(t>N)break;
use[t]=;
if(i%prime[j]==)break;
}
}
}
bool vis[N];double f[N];
double dfs(int x){
if(x==)return 0.0;if(vis[x])return f[x];
vis[x]=;double &ans=f[x];
int g=,p=;ans=;
for(int j=;j<=cnt;j++){
if(prime[j]>x)break;++p;
if(x%prime[j]==){g++;ans+=(dfs(x/prime[j]));}
}
ans=(ans+p)/g;return ans;
}
inline int read(){
int f=,x=;char ch;
do{ch=getchar();if(ch=='-')f=-;}while(ch<''||ch>'');
do{x=x*+ch-'';ch=getchar();}while(ch>=''&&ch<='');
return f*x;
}
int main(){
calcprime();int T=read();
for(int t=;t<=T;t++){
printf("Case %d: %.10lf\n",t,dfs(read()));
}
return ;
}#include<bits/stdc++.h>
#define N 1000010
using namespace std;
int prime[N],use[N],cnt=;
void calcprime(){
cnt=;memset(use,,sizeof(use));
for(int i=;i<=N;i++){
if(use[i]){prime[++cnt]=i;}
for(int j=;j<=cnt;j++){
int t=prime[j]*i;if(t>N)break;
use[t]=;
if(i%prime[j]==)break;
}
}
}
bool vis[N];double f[N];
double dfs(int x){
if(x==)return 0.0;if(vis[x])return f[x];
vis[x]=;double &ans=f[x];
int g=,p=;ans=;
for(int j=;j<=cnt;j++){
if(prime[j]>x)break;++p;
if(x%prime[j]==){g++;ans+=(dfs(x/prime[j]));}
}
ans=(ans+p)/g;return ans;
}
inline int read(){
int f=,x=;char ch;
do{ch=getchar();if(ch=='-')f=-;}while(ch<''||ch>'');
do{x=x*+ch-'';ch=getchar();}while(ch>=''&&ch<='');
return f*x;
}
int main(){
calcprime();int T=read();
for(int t=;t<=T;t++){
printf("Case %d: %.10lf\n",t,dfs(read()));
}
return ;
}
【Uva11762】Race to 1的更多相关文章
- 【BZOJ2599】Race(点分治)
[BZOJ2599]Race(点分治) 题面 BZOJ权限题,洛谷 题解 好久没写过点分治了... 在ppl的帮助下终于想起来了 orz ppl 首先回忆一下怎么求有没有正好是\(K\)的路径 维护一 ...
- 【BZOJ-2599】Race 点分治
2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 2590 Solved: 769[Submit][Status ...
- 【UVA】【11762】Race to 1(得到1)
数学期望/马尔可夫过程 DP/记忆化搜索 刘汝佳老师白书上的例题…… //UVA 11762 #include<vector> #include<cstdio> #includ ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【CF671E】Organizing a Race 单调栈+线段树
[CF671E]Organizing a Race 题意:n个城市排成一排,每个城市内都有一个加油站,赛车每次经过第i个城市时都会获得$g_i$升油.相邻两个城市之间由道路连接,第i个城市和第i+1个 ...
- 【BZOJ2599】[IOI2011]Race 树的点分治
[BZOJ2599][IOI2011]Race Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 100000 ...
- 【思维】Kenken Race
题目描述 There are N squares arranged in a row, numbered 1,2,...,N from left to right. You are given a s ...
- 【BZOJ】2599: [IOI2011]Race 点分治
[题意]给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000.注意点从0开始编号,无解输出-1. [算法]点分治 [题解] ...
随机推荐
- GraphSAGE 代码解析(二) - layers.py
原创文章-转载请注明出处哦.其他部分内容参见以下链接- GraphSAGE 代码解析(一) - unsupervised_train.py GraphSAGE 代码解析(三) - aggregator ...
- Ubuntu 和 Windows 之间进行远程访问和文件互传
1. 利用 Ubuntu 自带软件 Remmina 对另一台 Ubuntu 电脑进行远程访问(同一局域网下) 假设要用 A 电脑来控制 B 电脑,首先需要在 B 电脑上进行桌面共享设置 . 然后打 ...
- 并查集——hdu1213(入门)
传送门:How Many Tables 模板代入 判断几个连通分支 DFS亦可完成 [并查集] #include <iostream> #include <cstdio> #i ...
- SIP初步
http://blog.sina.com.cn/s/blog_6b10255301012db7.html 1.什么是SIP SIP(会话发起协议)属于IP应用层协议,用于在IP网上为用户提供会话应用. ...
- Asp.Net生命周期系列一
Asp.Net生命周期对于初级甚至中级程序员来说,一直都是一个难题,很多程序员不了解生命周期,导致使用Asp.Net做开发感觉很不灵活,感觉太多东西被微软封装好了,我们不能改变,其实只要你稍微了解一下 ...
- PokeCats开发者日志(三)
现在是PokeCats游戏开发的第四天的晚上,明天要过周末了,所以提前写一下开发者日志吧! day4 day4主要是优化界面和增加游戏可玩性. (1)感觉只有三只喵喵的话,玩家只需要无脑点 ...
- Node js 安装+回调函数+事件
/* 从网站 https://nodejs.org/zh-cn/ 下载 这里用的 9.4.0 版本 下载完安装 安装目录是 D:\ApacheServer\node 一路默认安装 安装后打开cmd命令 ...
- Java集合(2)——深入理解ArrayList、Vector和LinkedList
回顾 Java集合主要分为两个体系结构,Collection和Map.这篇博客主要介绍Collection子接口List下的三个经常使用的实现类:ArrayList.Vector和LinkedList ...
- mac tomcat: error 1 operation not permitted
在用微信传一个tomcat压缩包到别人后,解压后无法用idea启动,提示 error 1 operation not permitted,但是直接用命令可以执行. 仔细查看,原来是因为mac出于安全考 ...
- hihocoder 1457(后缀自动机+拓扑排序)
题意 给定若干组由数字构成的字符串,求所有不重复子串的和(把他们看成十进制),答案mod(1e9+7) 题解: 类似后缀数组的做法,把字符串之间用':'连接,这里用':'是因为':'的ascii码恰好 ...