UVa 11762 Race to 1 (数学期望 + 记忆化搜索)
题意:给定一个整数 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 (数学期望 + 记忆化搜索)的更多相关文章
- uva 11762 数学期望+记忆化搜索
题目大意:给一个正整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/p,否则N不变,问平均情况下需要多少次随机选择,才能把N变成1? 分析:根据数学期望的线性和全期望公 ...
- 【bzoj1415】[Noi2005]聪聪和可可 期望记忆化搜索
题目描述 输入 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行 ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- 【NOI2005】聪聪和可可 概率与期望 记忆化搜索
1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1635 Solved: 958[Submit][Statu ...
- UVa 10285 Longest Run on a Snowboard - 记忆化搜索
记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 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 ...
- UVA 10285 Longest Run on a Snowboard(记忆化搜索)
Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...
- bzoj 1415 期望+记忆化搜索 ****
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdkAAAIfCAIAAACzfDFhAAAgAElEQVR4nOy9bVwTW57vm5fnhed+Pn
随机推荐
- [z]根据awr报告查看最慢的sql语句
http://blog.itpub.net/12679300/viewspace-2125679/ 当系统运行了一段时间后,系统基本也趋于稳定了,SQL调优也变了DBA的一个主要的工作内容,很多时候都 ...
- 13.Mysql触发器
13.触发器13.1 创建触发器定义:触发器是与表有关的数据库对象,在满足定义条件时触发,并执行触发器中定义的语句集合.语法:create trigger 触发器名称 触发时机 触发事件 on 表名 ...
- C# 在网页中将Base64编码的字符串显示成图片
在写一个接口,返回的json里面有图片,是Base64编码的字符串. 测试接口的时候,发现原来在html显示,是直接可以将Base64编码的字符串显示成图片的. 格式如下: <img src=d ...
- css font-size=0的妙用
转自:css font-size=0有什么妙用? 回答一: 问题的根源是 inline(a标签默认是display:inline) 和 inline-block (.list-info 设置的是 di ...
- PAT 1086 就不告诉你(15 )(代码)
1086 就不告诉你(15 分) 做作业的时候,邻座的小盆友问你:"五乘以七等于多少?"你应该不失礼貌地围笑着告诉他:"五十三."本题就要求你,对任何一对给定的 ...
- andorid 进度条和图片的透明度
layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- jQuery操作(一)
基本语法:$(selector).action() 寻找元素(重要的选择器和筛选器) 一:选择器 1.1 基本选择器: $("*") $("#id") $(&q ...
- Java界面编程—API
Java 的 GUI 提供的对象都存在 java.awt 和 java.swing 两个包中. awt :Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能 ...
- ExportGrid Aspose.Cells.dll
using Aspose.Cells; using Aspose.Words; using System; using System.Collections; using System.Collect ...
- c++ boost 苹果内购 IAP验证
// 1111.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <cstdlib> #include <i ...