Codeforces Beta Round #50 C. First Digit Law(概率dp,好题)

博客探讨了概率理论中的Benford's Law,并通过Codeforces上的一个问题介绍了如何运用概率动态规划解决此类问题。文章详细解释了题目要求,即计算在给定区间内随机变量以1开头的概率,并给出了解题思路,特别是概率DP的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. First Digit Law
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the probability theory the following paradox called Benford's law is known: "In many lists of random numbers taken from real sources, numbers starting with digit 1 occur much more often than numbers starting with any other digit" (that's the simplest form of the law).

Having read about it on Codeforces, the Hedgehog got intrigued by the statement and wishes to thoroughly explore it. He finds the following similar problem interesting in particular: there are N random variables, the i-th of which can take any integer value from some segment [Li;Ri] (all numbers from this segment are equiprobable). It means that the value of the i-th quantity can be equal to any integer number from a given interval [Li;Ri] with probability 1 / (Ri - Li + 1).

The Hedgehog wants to know the probability of the event that the first digits of at least K% of those values will be equal to one. In other words, let us consider some set of fixed values of these random variables and leave only the first digit (the MSD — most significant digit) of each value. Then let's count how many times the digit 1 is encountered and if it is encountered in at least K per cent of those N values, than such set of values will be called a good one. You have to find the probability that a set of values of the given random variables will be a good one.

Input

The first line contains number N which is the number of random variables (1 ≤ N ≤ 1000). Then follow N lines containing pairs of numbers Li, Ri, each of whom is a description of a random variable. It is guaranteed that 1 ≤ Li ≤ Ri ≤ 1018.

The last line contains an integer K (0 ≤ K ≤ 100).

All the numbers in the input file are integers.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).

Output

Print the required probability. Print the fractional number with such a precision that the relative or absolute error of the result won't exceed 10 - 9.

Examples
input
1
1 2
50
output
0.500000000000000
input
2
1 2
9 11
50
output
0.833333333333333

题意:

给出n个数,然后给出n行,每行li,ri,代表第i个数在区间[li,ri]中,求一个概率使得这n个数中有K%的数是1开头的。


题解:

首先,可以先求出每个区间以1开头的数有多少个,即每个区间满足条件的概率pi,要使得有K%的区间满足条件,这个该怎么求呢?

我一开始以为是概率方面的题,想了一堆容斥什么的,发现都不可做,最后看了官方题解。

题解是用概率dp做的(妙啊!),d[i][j]表示前i个数有j个满足条件的概率。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=1000+10;
ll f[30];
ll pow(int x)
{
    ll t=10,ans=1;
    while(x)
    {
        if(x&1) ans*=t;
        t=t*t;
        x/=2;
    }
    return ans;
}
ll cal(ll x)
{
    int cnt=0;
    ll t=x;
    ll las=0;
    while(t)
    {
        las=t;
        cnt++;
        t/=10;
    }
    if(las!=1) return f[cnt];
    else return x-pow(cnt-1)+1+f[cnt-1];
}
double p[maxn];
double d[maxn][maxn];
int main()
{
    f[1]=1;
    ll q=1;
    rep(i,2,20)
    {
        q*=10;
        f[i]=f[i-1]+q;
    }
    int n;
    scanf("%d",&n);
    rep(i,1,n+1)
    {
        ll l,r;
        scanf("%lld%lld",&l,&r);
        ll ans=cal(r)-cal(l-1);
        p[i]=1.0*ans/(r-l+1);
    }
    int k;
    scanf("%d",&k);
    d[0][0]=1;
    rep(i,1,n+1) rep(j,0,i+1)
    {
        if(j>0)
        d[i][j]=d[i-1][j-1]*p[i]+d[i-1][j]*(1-p[i]);
        else d[i][j]=d[i-1][j]*(1-p[i]);
    }
    double kk=1.0*k/100;
    double ans=0;
    rep(i,0,n+1)
    {
        double t=1.0*i/n;
        if(t>=kk) ans+=d[n][i];
    }
    printf("%.9lf\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值