LightOJ_1038 Race to 1 Again
题意:
给一个数n, 每次操作是随机的选择一个[1,N]区间内能够被n整除的数进行除法, 然后得到一个新的n。
问n变成1时的期望操作次数。
思路:
设E[n] 为 当数为x时, 变成 1 期望的次数, 则有转移方程。
E[n] = sigma E[n / x[i]] / k + 1(x[i] 为能被n被整除的数), k为n在区间[1,n]能被n整除的个数。
化简:E[n] = E[n] / k + sigma E[n / x[i]] / k + 1
= k * (sigmaE[n / x[i]] / k + 1) / (k - 1)
= (sigmaE[n / x[i]] + k) / (k - 1)。
代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define MAXN 100010
#define MOD 1000000007
#define eps 1e-6
int n;
double f[MAXN];
bool vis[MAXN];
vector <int> g[MAXN];
void init()
{
for(int i = ; i < MAXN; i ++)
for(int j = ; j * i < MAXN; j ++)
g[j * i].push_back(i); f[] = ;
memset(vis, false, sizeof(vis));
}
double dp(int x)
{
if(x == ) return 0.0;
if(vis[x]) return f[x];
vis[x] = true;
double& ans = f[x];
ans = 0.0;
for(int i = ; i < g[x].size(); i ++)
ans += dp(x/g[x][i]);
ans += (g[x].size() + 1.0);
ans /= g[x].size();
return ans;
} int main()
{
int T;
int kcase = ;
init();
scanf("%d", &T);
while(T --)
{
scanf("%d", &n);
printf("Case %d: %.7lf\n", ++ kcase, dp(n));
}
return ;
}
LightOJ_1038 Race to 1 Again的更多相关文章
- Promise.race
[Promise.race] 返回最先完成的promise var p1 = new Promise(function(resolve, reject) { setTimeout(resolve, 5 ...
- golang中的race检测
golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...
- 【BZOJ-2599】Race 点分治
2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 2590 Solved: 769[Submit][Status ...
- hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- Codeforces Round #131 (Div. 2) E. Relay Race dp
题目链接: http://codeforces.com/problemset/problem/214/E Relay Race time limit per test4 secondsmemory l ...
- 【多线程同步案例】Race Condition引起的性能问题
Race Condition(也叫做资源竞争),是多线程编程中比较头疼的问题.特别是Java多线程模型当中,经常会因为多个线程同时访问相同的共享数据,而造成数据的不一致性.为了解决这个问题,通常来说需 ...
- Codeforces Round #328 (Div. 2) C. The Big Race 数学.lcm
C. The Big Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/probl ...
- HDU 4123 Bob’s Race 树的直径 RMQ
Bob’s Race Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=41 ...
- [LOJ 1038] Race to 1 Again
C - Race to 1 Again Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu D ...
随机推荐
- Linux安装程序Anaconda分析
1.概述 Anaconda是RedHat.CentOS.Fedora等Linux的安装管理程序.它能够提供文本.图形等安装管理方式,并支持Kickstart等脚本提供自己主动安装的功能.此外, ...
- [React] React Fundamentals: JSX Deep Dive
"JSX transforms from an XML-like syntax into native JavaScript. XML elements and attributes are ...
- android Fragments详解六:处理fragement的生命周期
把条目添加到动作栏 你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptio ...
- 两款Mac下的视频下载利器
1 iSkysoft iTube Studio 2.jaksta mac
- Form 对象的集合
<html> <body id="myBody" class="myBody"> <form id="myForm&qu ...
- 第二次装OA系统
第二次安装:1.解压之后再MYOA目录下 找到一键安装.bat2.安装之后,一闪而过.(电脑上不需要apache,也不需要 mysql)3.192.168.1.111(自己电脑IP)看是否可以,若不可 ...
- java 中能否使用 动态加载的类(Class.forName) 来做类型转换?
今天同事提出了一个问题: 将对象a 转化为类型b,b 的classpath 是在配置文件中配置的,需要在运行中使用Class.forName 动态load进来,因为之前从来没有想过类似的问题,所以懵掉 ...
- order by跟group by 跟having----------------sum() 求和 avg()求平均 count() 求个数--------------like
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoEAAACdCAIAAABEujUmAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWX ...
- 雷鸟(Thunderbird)收取Gmail出错,收到警告邮件
如题,每次打开thunderbird都会收到以下提醒邮件,后来发现,只要将thunderbird中和google服务有关的插件卸载掉就可以了 We prevented the sign-in atte ...
- .NET设计模式(6):原型模式(Prototype Pattern)
):原型模式(Prototype Pattern) ); //使用颜色 string colorName = "red"; C ...