标签:des style blog http io ar color os sp
3 0 100 1 10 5 100
Case #1: 1 Case #2: 2 Case #3: 13
2013 ACM/ICPC Asia Regional Chengdu Online
题意:给你2个数n,m 要你求出0-m中,f(i)<=f(n)的数的个数
思路:数位DP的模板题目,这个时候的状态为当前位数的值
直接代码吧,或者可以参考我写的第一篇概率DP题提供一下思想
AC代码:
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int f[20][200000],bits[20]; int dfs(int pos,int sum,bool bianjie) { int ans=0; if(pos==-1)return sum>=0; if(sum<0)return 0; if(!bianjie&&f[pos][sum]!=-1) return f[pos][sum]; int u=bianjie?bits[pos]:9; for(int i=0;i<=u;i++) { ans+=dfs(pos-1,sum-i*(1<<pos),bianjie&&i==u); } return bianjie?ans:f[pos][sum]=ans; } int n,m; int solve() { int sum=0; int len=0; while(n) { sum=sum+(n%10)*(1<<len); len++; n/=10; } // printf("%d\n",sum); int len_m=0; while(m) { bits[len_m++]=m%10; m/=10; } return dfs(len_m-1,sum,true); } int main() { int t; cin>>t; int cnt=1; memset(f,-1,sizeof(f)); while(t--) { scanf("%d %d",&n,&m); cout<<"Case #"<<cnt++<<": "<<solve()<<endl; } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/u012313382/article/details/41382335