codeforces 54C First Digit Law (数位dp+概率dp+背包模型)
2015-02-11 14:34
288 查看
题意:
给出n个区间,然后每个区间中取任何数的概率都是1/(r-l+1),现在问n个区间任意的组成的任意集合,求某个集合满足里面的数首位是1的个数大于k的概率。
题解:
说起来很绕,读了一个小时也没读懂啊!不看题解的翻译根本读不懂题目啊!我严重怀疑出题人的英语水平。
不过是一道好题,首先要得到区间中首位为1的数字个数要数位dp或者组合数学,然后就是背包模型的概率dp,因为要求任意字集合满足条件的格律,那么背包再合适不过,以去的区间个数作为重量,概率作为价值。之间用乘号。
给出n个区间,然后每个区间中取任何数的概率都是1/(r-l+1),现在问n个区间任意的组成的任意集合,求某个集合满足里面的数首位是1的个数大于k的概率。
题解:
说起来很绕,读了一个小时也没读懂啊!不看题解的翻译根本读不懂题目啊!我严重怀疑出题人的英语水平。
不过是一道好题,首先要得到区间中首位为1的数字个数要数位dp或者组合数学,然后就是背包模型的概率dp,因为要求任意字集合满足条件的格律,那么背包再合适不过,以去的区间个数作为重量,概率作为价值。之间用乘号。
#include<iostream> #include<math.h> #include<stdio.h> #include<algorithm> #include<string.h> #include<vector> #include<map> using namespace std; typedef long long lld; const int oo=0x3f3f3f3f; const lld OO=1LL<<61; const int MOD=(1e9)+7; const int maxn=1005; double dp[maxn],p[maxn]; lld Cnt(lld n) { lld ans=0,x=1,cnt=0,high=0,num=n; while(num) { high=num%10; num/=10; cnt++; } for(int i=1;i<cnt;i++,x*=10) ans+=x; if(high>1) ans+=x; else if(high==1) ans+=n-x+1; return ans; } int main() { int n,k; lld l,r; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) { scanf("%I64d %I64d",&l,&r); lld temp=Cnt(r)-Cnt(l-1); p[i]=1.0*temp/(r-l+1); } scanf("%d",&k); memset(dp,0,sizeof dp); dp[0]=1.0; for(int i=1;i<=n;i++) { for(int j=n;j>=0;j--) { dp[j]=dp[j]*(1.0-p[i]); if(j>0) dp[j]+=dp[j-1]*p[i]; } } double ans=0.0; for(int i=0;i<=n;i++) if(i*100>=n*k) ans+=dp[i]; printf("%.15lf\n",ans); } return 0; } /** 1 1 2 50 2 1 2 9 11 50 */
相关文章推荐
- codeforces(背包)
- Codeforces 922 E Birds (背包dp)被define坑了的一题
- Codeforces 189A - Cut Ribbon (dp + 完全背包)
- codeforces 543A A. Writing Code(完全背包优化dp )
- codeforces189 A. Cut Ribbon【完全背包】
- 背包模型
- 容斥原理+背包模型---nkoj3659硬币
- CodeForces - 365D Free Market 背包求状态数+贪心
- CodeForces - 106C Buns (多重背包二进制优化)
- Codeforces 544C Writing Code【二维完全背包】
- CodeForces - 106C (多重背包转换为01背包)
- codeforces 946 D Timetable(预处理+分组背包)
- codeforces 167B Wizards and Huge Prize (概率dp,类似背包)
- D - Free Market CodeForces - 365D 背包求状态数+贪心 好题!
- Codeforces 364B 背包+贪心
- CodeForces - 417A Elimination (完全背包)
- codeforces 148E Porcelain 多重背包
- 背包问题经典模型
- Codeforces 864E Fire(背包DP)
- Selling Souvenirs CodeForces - 808E 【01背包变形】 前缀和+三分