题意:给定一个整数 n ,然后你要把它变成 1,变换操作就是随机从小于等于 n 的素数中选一个p,如果这个数是 n 的约数,那么就可以变成 n/p,否则还是本身,问你把它变成 1 的数学期望是多少。

析:一个很明显的期望DP,dp[i] 表示把 i 变成 1 的期望是多少,枚举每一种操作,列出表达式,dp[i] = ∑dp[i/x]/q + p/q*dp[i] + 1,其中 x 表示枚举的素数,然后 p 表示不是 i 的约数个数,q 是小于等于 n 的素数个数,然后变形,可以得到 dp[i] = (∑dp[i/x] + q) / (q-p),可以用记忆化搜索来做。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000000 + 10;
const int maxm = 100 + 2;
const LL mod = 100000000;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} double dp[maxn];
bool vis[maxn];
int prime[maxn], cnt; double dfs(int x){
if(x == 1) return 0.;
double &ans = dp[x];
if(ans > 0.) return ans;
ans = 0.;
int q = 0, p = 0;
for(int i = 0; i < cnt && prime[i] <= x; ++i, ++q)
if(x % prime[i] == 0) dp[x] += dfs(x / prime[i]);
else ++p;
ans += q;
ans /= q - p;
return ans;
} int main(){
for(int i = 2; i < maxn; ++i) if(!vis[i]){
prime[cnt++] = i;
if(i > 1000) continue;
for(int j = i*i; j < maxn; j += i) vis[j] = 1;
}
int T; cin >> T; ms(dp, 0);
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
printf("Case %d: %.6f\n", kase, dfs(n));
}
return 0;
}

  

UVa 11762 Race to 1 (数学期望 + 记忆化搜索)的更多相关文章

  1. uva 11762 数学期望+记忆化搜索

    题目大意:给一个正整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/p,否则N不变,问平均情况下需要多少次随机选择,才能把N变成1? 分析:根据数学期望的线性和全期望公 ...

  2. 【bzoj1415】[Noi2005]聪聪和可可 期望记忆化搜索

    题目描述 输入 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行 ...

  3. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  4. 【NOI2005】聪聪和可可 概率与期望 记忆化搜索

    1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1635  Solved: 958[Submit][Statu ...

  5. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

  6. 【UVA 437】The Tower of Babylon(记忆化搜索写法)

    [题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  7. UVA 825 Walking on the Safe Side(记忆化搜索)

      Walking on the Safe Side  Square City is a very easy place for people to walk around. The two-way ...

  8. UVA 10285 Longest Run on a Snowboard(记忆化搜索)

    Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...

  9. bzoj 1415 期望+记忆化搜索 ****

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdkAAAIfCAIAAACzfDFhAAAgAElEQVR4nOy9bVwTW57vm5fnhed+Pn

随机推荐

  1. day 21 封装,多态,类的其他属性

    封装 封装:将一些数据,重要的信息等等放到一个地方(空间中) class A: country = 'China' area = '深圳' def __init__(self,name,age): s ...

  2. 通过docker-compose构建ghost博客(一)

    通过命令构建ghost博客 docker run -d --name ghost -p : -v $PWD/data:/var/lib/ghost ghost 当然也可以编写yml文件,通过docke ...

  3. Aspose.Words三 创建表格

    创建表格,实现合并行.和并列.表居中.表格水平和垂直居中.设置单元格边框颜色和样式. string templateFile = Server.MapPath("table_templ.do ...

  4. ecplise中创建jsp页面时默认的编码格式为ISO-8859-1,这里我们将其编码格式设置为utf-8

    我们在创建jsp页面时,默认的编码格式为ISO-8859-1,我们如果想要将其改为utf-8还要自己手动去更改. 因此可以设置Jsp默认的编码为utf-8,具体步骤如下: 启动Eclipse,点击菜单 ...

  5. python 面向对象编程 之 反射

    1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...

  6. 采用RedisLive监控Redis服务——安装手册

    #1.gcc编译环境确认 .tgz cd Python- ./configure /bin/python2. /usr/bin/python #运行python查看版本 python -V #进行更改 ...

  7. 让eclipse调试和豌豆荚并存

    豌豆荚有一个设置 设置->高级设置->开发者模式 勾上开发者模式 确定. 你什么手机的驱动都不用安装了. 就可以直接使用豌豆荚,也可以使用eclipse进行调试.

  8. [AI]AI章2 框架比较

    深度学习框架比较 神经网络一般包括:训练,测试两大阶段.训练:就是把训练数据(原料)和神经网络模型:如AlexNet.RNN等“倒进” 神经网络训练框架例如cafffe等然后用 CPU或GPU(真火) ...

  9. ES线程池

    每个Elasticsearch节点内部都维护着多个线程池,如index.search.get.bulk等,用户可以修改线程池的类型和大小,线程池默认大小跟CPU逻辑一致 一.查看当前线程组状态 cur ...

  10. ExportGrid Aspose.Cells.dll

    using Aspose.Cells; using Aspose.Words; using System; using System.Collections; using System.Collect ...