【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. [算法]点分治 [题解] ...
随机推荐
- 为什么logstash进程的CPU使用率100%?
机器上有个进程cpu使用率很高,近100%了, Tasks: 120 total, 2 running, 118 sleeping, 0 stopped, 0 zombie%Cpu(s): 99.0 ...
- spring boot 打包问题
一.jar包 1.maven build package 2.linux 下执行 java -jar & 命令后台运行,也可加入服务运行 二.war包 1.将pom中的<packagin ...
- Kindle 3(非常旧的版本) 隔一段时间自动重启问题
买了本新书后,kindle 3 自己没事就在那边重启,几分钟一次 查到解决方案1: https://answers.yahoo.com/question/index?qid=2014040815565 ...
- spring mvc:实现给Controller函数传入list<pojo>参数
[1]前端js调用示例: ...insertStatisData?statisDatas=[{'cid':'2','devId':'9003','deviceName':'测试名','endTime' ...
- JavaScript Map数据结构
Array.prototype.remove = function (s) { for (var i = 0; i < this.length; i++) { if (s == this[i]) ...
- identity方式
identity方式 <generator class="identity"/>identity方式表示数据库的主键生成方式为采用数据库的主键生成机制,例如S ...
- java-2018-01-17计划
1.一句英语 包括单词 2.一个java版本的设计模式 参考:https://github.com/iluwatar/java-design-patterns 学习了抽象工厂模式 总结:java的RS ...
- Python数据分析(四)DataFrame, Series, ndarray, list, dict, tuple的相互转换
转自:https://blog.csdn.net/lambsnow/article/details/78517340 import numpy as np import pandas as pd ## ...
- BZOJ4487 JSOI2015染色问题(组合数学+容斥原理)
逐个去除限制.第四个限制显然可以容斥,即染恰好c种颜色的方案数=染至多c种颜色的方案数-染至多c-1种颜色的方案数+染至多c-2种颜色的方案数…… 然后是限制二.同样可以容斥,即恰好选n行的方案数=至 ...
- Conjugate 解题报告
Conjugate 问题描述 在不存在的 \(\text{noip day3}\) 中,小 \(\text{w}\) 见到了一堆堆的谜题. 比如这题为什么会叫共轭? 他并不知道答案. 有 \(n\) ...