题意:

给你n个区间,在每个区间里各取一个数(随机取),求这n个数中超过K%的数是首位为1数的概率

分析:

dp[i][j]取前i个数,有j个是首位为1的数的概率

易知,dp[i][j]=dp[i-1][j]*(1-p[i])+dp[i-1][j-1]*p[i];

现在关键是求p[i],第i个区间首位为1的数出现的概率,用数位统计一下即可

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
ll sum[],a[];
double dp[][],p[];
int bit[],n,k;
void init(){
sum[]=;
for(int i=;i<=;++i)
sum[i]=sum[i-]*;
}
ll countsum(int l){
ll total=;
for(int i=;i<=l;++i)
total+=sum[i];
return total;
}
//统计首位为1数的数量
ll countone(ll x){
if(x==)return ;
init();
int len=;
while(x){
bit[++len]=x%;
x/=;
}
if(bit[len]>){
return countsum(len-);
}
else{
ll tmp=countsum(len-);
a[]=;
for(int i=;i<=len-;++i)
a[i]=a[i-]+bit[i]*sum[i-];
tmp+=a[len-]+;
return tmp;
}
}
int main()
{
ll l,r;
while(~scanf("%d",&n)){
for(int i=;i<=n;++i){
scanf("%I64d%I64d",&l,&r);
ll tmp=countone(r)-countone(l-);
//cout<<tmp<<endl;
p[i]=1.0*tmp/(r-l+);
//cout<<p[i]<<endl;
}
dp[][]=p[];
dp[][]=1.0-p[];
for(int i=;i<=n;++i)
for(int j=;j<=i;++j){
dp[i][j]+=dp[i-][j]*(1.0-p[i]);
if(j>)
dp[i][j]+=dp[i-][j-]*p[i];
}
scanf("%d",&k);
double ans=0.0;
for(int j=;j<=n;++j)
if(j>=(1.0*n*k/))
ans+=dp[n][j];
printf("%.15lf\n",ans);
}
return ;
}

CodeForces 54C-First Digit Law(数位,概率dp)的更多相关文章

  1. Codeforces 678E. Another Sith Tournament(概率DP,状压)

    Codeforces 678E. Another Sith Tournament 题意: n(n<=18)个人打擂台赛,给定任意两人对决的胜负概率,比赛规则:可指定一人作为最开始的擂主,每次可指 ...

  2. Codeforces B. Bad Luck Island(概率dp)

    题目描述: Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. Codeforces 148D Bag of mice:概率dp 记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 一个袋子中有w只白老鼠,b只黑老鼠. 公主和龙轮流从袋子里随机抓一只老鼠出来,不放回,公 ...

  4. Codeforces 167B Wizards and Huge Prize(概率dp)

    题意: n个人,开始有一个容量为k得背包,击败一个人背包可以获得一定容量或得到一个财富(放入背包内),给出击败每个人的概率,求至少击败l个人,且背包容量大于获得的总财富值的概率 分析: 状态好确定,d ...

  5. Codeforces 601C Kleofáš and the n-thlon 概率dp

    Kleofáš and the n-thlon 我们可以用dp算出比当前这个人得分少的概率, 然后人数乘概率就好啦. dp[ i ][ j ]表示进行了 i 轮 得分为 j 的概率, 因为每个人都是独 ...

  6. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  7. codeforces#1139D. Steps to One (概率dp+莫比乌斯反演)

    题目链接: http://codeforces.com/contest/1139/problem/D 题意: 在$1$到$m$中选择一个数,加入到一个初始为空的序列中,当序列的$gcd$和为$1$时, ...

  8. 2018.12.12 codeforces 931E. Game with String(概率dp)

    传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...

  9. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  10. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

随机推荐

  1. 【转】Java读取文件方法大全

    本文转自:http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html#undefined 目录: 按字节读取文件内容 按字符读取文 ...

  2. 李洪强iOS开发之让您的Xcode键字如飞

    手指在键盘上飞速跳跃,终端上的代码也随着飞舞,是的这确实很酷.优秀的程序员总是这么一群人,他们不拘于现状,不固步自封,他们喜欢新奇的事,他们把自己发挥到极致. 指法攻略 放下您钟爱的鼠标吧,在前行之中 ...

  3. linux上tomcat安装

    转载:http://my.oschina.net/zmf/blog/309484 tomcat安装 下载后解压:tar -zxvf apache-tomcat-7.0.28 移动到/usr/local ...

  4. ADO.NET基础01(ADO.NET组成,数据库的方式,SqlCommand,SqlDataReader)

    什么是ADO.NET: ADO.NET就是一组类库,这组类库可以让我们通过程序的方式访问数据库,就像System.IO下的类操作文件一样, System.Data.这组类是用来操作数据库(不光是MSS ...

  5. 简单的SocketExample

    客户端//---------------VerySimpleClient.java package SocketExample; // Tue Nov 2 18:34:53 EST 2004 // / ...

  6. Junit使用教程(四)

    一.会用Spring测试套件的好处 在开发基于Spring的应用时,如果你还直接使用Junit进行单元测试,那你就错过了Spring为我们所提供的饕餮大餐了.使用Junit直接进行单元测试有以下四大不 ...

  7. vmware shared holder 虚拟机设置共享目录

    1, 安装 vm-tools http://askubuntu.com/questions/29284/how-do-i-mount-shared-folders-win7-host-in-ubunt ...

  8. Android开发之EventBus的简单使用

    参考: 1.http://blog.csdn.net/harvic880925/article/details/40660137 2.http://blog.csdn.net/harvic880925 ...

  9. 转:Android 设置屏幕不待机

    本文转载于:http://blog.csdn.net/yudajun/article/details/7748760 Android设置支部待机有两种方法 第一种简单通过设置WindowManager ...

  10. 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)

    这个题容易出错想了挺长时间,然后代码不长,1Y.. 做完题,看了一下别人的博客,也可以优先用 闪烁法术, 在闪烁法术不不如跑步的阶段(即魔法恢复的时候)用跑步. 洞穴逃生 描述: 精灵王子爱好冒险,在 ...