HDU-4722 Good Numbers 数位DP

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722

  简单的数位DP,f[i][j][k]表示第 i 位数为 j 时余数为k的个数,然后直接找就可以了。。

  1 //STATUS:C++_AC_31MS_412KB
  2 #include <algorithm>
  3 #include <iostream>
  4 //#include <ext/rope>
  5 #include <fstream>
  6 #include <sstream>
  7 #include <iomanip>
  8 #include <numeric>
  9 #include <cstring>
 10 #include <cassert>
 11 #include <cstdio>
 12 #include <string>
 13 #include <vector>
 14 #include <bitset>
 15 #include <queue>
 16 #include <stack>
 17 #include <cmath>
 18 #include <ctime>
 19 #include <list>
 20 #include <set>
 21 #include <map>
 22 using namespace std;
 23 //#pragma comment(linker,"/STACK:102400000,102400000")
 24 //using namespace __gnu_cxx;
 25 //define
 26 #define pii pair<int,int>
 27 #define mem(a,b) memset(a,b,sizeof(a))
 28 #define lson l,mid,rt<<1
 29 #define rson mid+1,r,rt<<1|1
 30 #define PI acos(-1.0)
 31 //typedef
 32 typedef __int64 LL;
 33 typedef unsigned __int64 ULL;
 34 //const
 35 const int N=25;
 36 const int INF=0x3f3f3f3f;
 37 const int MOD=1000000007,STA=8000010;
 38 const LL LNF=1LL<<60;
 39 const double EPS=1e-8;
 40 const double OO=1e15;
 41 const int dx[4]={-1,0,1,0};
 42 const int dy[4]={0,1,0,-1};
 43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 44 //Daily Use ...
 45 inline int sign(double x){return (x>EPS)-(x<-EPS);}
 46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
 47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
 48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
 49 template<class T> inline T Min(T a,T b){return a<b?a:b;}
 50 template<class T> inline T Max(T a,T b){return a>b?a:b;}
 51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
 52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
 53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
 54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
 55 //End
 56 
 57 LL f[N][N][N];
 58 LL a,b;
 59 int num[N];
 60 int T;
 61 
 62 int getnum(LL n)
 63 {
 64     int i,len=0;
 65     while(n){
 66         num[++len]=n%10;
 67         n/=10;
 68     }
 69     return len;
 70 }
 71 
 72 LL getans(LL n)
 73 {
 74     int i,j,len,sum=0;
 75     LL ret=0;
 76     len=getnum(n);
 77     for(i=len;i>=1;i--){
 78         for(j=0;j<=num[i];j++){
 79             if(j==num[i] && i!=1)continue;
 80             ret+=f[i][j][(10-sum)%10];
 81         }
 82         sum=(sum+num[i])%10;
 83     }
 84     return ret;
 85 }
 86 
 87 int main(){
 88  //   freopen("in.txt","r",stdin);
 89     int i,j,k,p,q,ca=1;
 90     LL ansb,ansa;
 91     mem(f,0);f[0][0][0]=1;
 92     for(i=1;i<=19;i++){
 93         for(j=0;j<=9;j++){
 94             for(k=0;k<=9;k++){
 95                 for(p=0;p<=9;p++){
 96                     f[i][j][k]+=f[i-1][p][(k-j+10)%10];
 97                 }
 98             }
 99         }
100     }
101     scanf("%d",&T);
102     while(T--)
103     {
104         scanf("%I64d%I64d",&a,&b);
105 
106         ansb=(b>0)?getans(b):1;
107         ansa=(a-1>0)?getans(a-1):a;
108 
109         printf("Case #%d: %I64d\n",ca++,ansb-ansa);
110     }
111     return 0;
112 }

 

posted @ 2013-09-13 00:35  zhsl  阅读(225)  评论(0编辑  收藏  举报