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

#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. Gated Recurrent Unit (GRU)

                                   Gated Recurrent Unit (GRU) Outline                             Backgr ...

  2. struts2中的action为什么要继承ActionSupport类,不继承也可以,有什么好处?

    简单来说,有很多相关的方法都加载进来,你直接调用就行了,而且在安全上和稳定性上做了很好的处理 实际上继承ActionSupport之后 就等同于实现了很多接口 Action,Validateable, ...

  3. [leetcode-635-Design Log Storage System]

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  4. HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...

  5. lintcode-113-删除排序链表中的重复数字 II

    113-删除排序链表中的重复数字 II 给定一个排序链表,删除所有重复的元素只留下原链表中没有重复的元素. 样例 给出 1->2->3->3->4->4->5-&g ...

  6. 【PHP】- include、require、include_once 和 require_once的区别

    1.include:会将指定的档案读入并且执行里面的程序.      被导入的档案中的程序代码都会被执行,而且这些程序在执行的时候会拥有和源文件中呼叫到 include() 函数的位置相同的变量范围( ...

  7. GraphQL & Apollo & Vue

    GraphQL & Apollo & Vue https://www.howtographql.com/vue-apollo/0-introduction/ https://githu ...

  8. ELK + Kafka + Filebeat

    ELK + Kafka + Filebeat学习 https://blog.csdn.net/qq_21383435/article/details/79463832 https://blog.csd ...

  9. 大数据Hadoop-1

    大数据Hadoop学习之搭建hadoop平台(2.2)   关于大数据,一看就懂,一懂就懵. 一.概述 本文介绍如何搭建hadoop分布式集群环境,前面文章已经介绍了如何搭建hadoop单机环境和伪分 ...

  10. [luogu2617]Dynamic Rankings

    题面在这里 description 动态区间第\(k\)大 data range \[n,m\le 10000,a_i,t\le 1e^9\] solution 前置技能:主席树,静态区间第\(k\) ...