[CF54C]First Digit Law

题目

传送门

人话表述:

给你一个 \(n\) 表示数列 \(a\) 有多少个数;

对于每个数,给出 \(l_i,r_i\) 表示数 \(a_i\in [l_i,r_i]\)

给出 \(k\),让你求数列 \(a\) 中有至少 \(\frac{n\times k}{100}\) 个数最高位为 \(1\) 的概率并输出;

题解

巧妙的数位 \(DP\) + 概率 \(DP\) + 背包(虽然说题目表述不清就是了

可以利用 \(l_i,r_i\) 求出 \(a_i\) 最高位为 \(1\) 的概率 \(p_i\).

现在我们已经有了每一位出现合法数字的概率,考虑怎么拼凑出答案

关注题目,“求在 \(n\) 个数中,至少有 \(k\) 个数最高位为 \(1\) 的概率”,发现有点像背包

考虑定义状态 \(f[i][j]\) 表示前 \(i\) 个数中,有 \(j\) 个数的最高位为 \(1\) 的概率,那么我们可以写出转移:

对于 \(j=0\),实际上只可能由 \(f[i-1][0]\) 一种情况转移过来,而这一位也只能选择 \(0\),那么就有状转

\[f[i][0]=f[i-1][0]\times (1-p[i]) \]

对于 \(j>0\),有两种情况:

  • 这一位选非 \(1\) 开头的数字,由 \(f[i-1][j]\) 转移;
  • 这一位选择 \(1\) 开头的数字,由 \(f[i-1][j-1]\) 转移;

这两种情况相互独立,用加法连接,那么有转移

\[f[i][j]=f[i-1][j]\times (1-p[i])+f[i-1][j-1]\times p[i] \]

最后统计答案,由于都是独立,也直接相加即可

#include<cstdio>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int maxn=1000;

double p[maxn+5];

int n,k;

inline LL calc(const LL up){
    int len=0;LL tmp=up;
    while(tmp>9)++len,tmp/=10;
    LL ten=1,ret=1;
    rep(i,1,len)ret+=ten,ten*=10;
    if(tmp>1)return ret+ten;
    else return ret+up-(ten-1);
}

inline void Init(){
    n=read(1);
    LL l,r;
    rep(i,1,n){
        l=read(1ll),r=read(1ll);
        p[i]=(double)(calc(r)-calc(l-1))/(r-l+1);
    }k=read(1);
}

double f[maxn+5][maxn+5];
inline void Get_f(){
    f[0][0]=1;
    rep(i,1,n){
        rep(j,0,n){
            f[i][j]=f[i-1][j]*(1-p[i]);
            if(j)f[i][j]+=f[i-1][j-1]*p[i];
        }
    }
}

inline void Get_ans(){
    double ans=0;
    fep(i,n,0)if((double)(i)>=(double)k*n/100)
        ans+=f[n][i];
        else break;
    printf("%.10f",ans);
}

signed main(){
    Init();
    Get_f();
    Get_ans();
    return 0;
}
posted @ 2020-08-11 10:01  Arextre  阅读(145)  评论(0编辑  收藏  举报