模拟马尔可夫过程,具体看书。

#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的更多相关文章

  1. 【BZOJ2599】Race(点分治)

    [BZOJ2599]Race(点分治) 题面 BZOJ权限题,洛谷 题解 好久没写过点分治了... 在ppl的帮助下终于想起来了 orz ppl 首先回忆一下怎么求有没有正好是\(K\)的路径 维护一 ...

  2. 【BZOJ-2599】Race 点分治

    2599: [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBSubmit: 2590  Solved: 769[Submit][Status ...

  3. 【UVA】【11762】Race to 1(得到1)

    数学期望/马尔可夫过程 DP/记忆化搜索 刘汝佳老师白书上的例题…… //UVA 11762 #include<vector> #include<cstdio> #includ ...

  4. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  5. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  6. 【CF671E】Organizing a Race 单调栈+线段树

    [CF671E]Organizing a Race 题意:n个城市排成一排,每个城市内都有一个加油站,赛车每次经过第i个城市时都会获得$g_i$升油.相邻两个城市之间由道路连接,第i个城市和第i+1个 ...

  7. 【BZOJ2599】[IOI2011]Race 树的点分治

    [BZOJ2599][IOI2011]Race Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 100000 ...

  8. 【思维】Kenken Race

    题目描述 There are N squares arranged in a row, numbered 1,2,...,N from left to right. You are given a s ...

  9. 【BZOJ】2599: [IOI2011]Race 点分治

    [题意]给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000.注意点从0开始编号,无解输出-1. [算法]点分治 [题解] ...

随机推荐

  1. C++陷阱系列:让面试官倒掉的题

    http://blog.chinaunix.net/uid-22754909-id-3969535.html 今天和几位同仁一起探讨了一下C++的一些基础知识,在座的同仁都是行家了,有的多次当过C++ ...

  2. OpenCV彩色图像转灰度图

    核心函数cvSplit(). #include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplI ...

  3. maven中如何得到父工程的位置

    目前的项目是一个父子工程项目,想要在子工程的pom文件中操作父工程目录下的资源. maven官方提供了标准的写法,比如parent.basedir之类的,网上可以找到很多,但尝试了之后就是不识别. 搞 ...

  4. 配置Mac自带的Apache http服务器

    Mac系统是自带Apache,所以很方便我们做一些http测试. 我可以先启动默认的服务器 $ sudo apachectl start 在浏览器打开:http://localhost 将会看到下面信 ...

  5. Linux中安装apache

    一.前言 安装的背景就是公司项目要用,自己在linux安装软件不多,过程比较曲折,需要记录一下(最近几篇博客好像都会有这种高大上的背景)~先说下笔者安装环境以及相关软件版本: 操作系统:RedHat ...

  6. oracle视图就是封装了一条写好的sql语句 可通过视图修改表结构 ; oracle需要手动创建序列

    create sequence student_sid; --创建序列 oracle只能通过手动方式创建序列

  7. 关于<meta http-equiv="Content-Type" content="text/html:charset=UTF-8">

    meta是html的元标签,其中包含了对应html的相关信息,客户端浏览器或服务端程序都会根据这些信息进行处理.我们以<meta http-equiv="Content-Type&qu ...

  8. 图片和byte[]的互相转换

    //图片的"读"操作 //①参数是图片路径:返回Byte[]类型: //参数是图片的路径 public byte[] GetPictureData(string imagePath ...

  9. G D 3 2 预 处 理 符 号 配 置 中 定 义

    Is mainly used in MCU and peripherals to choose, without having to modify macro definitions in the c ...

  10. Numpy array学习笔记