标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 388 Accepted Submission(s): 212
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 //#include<bits/stdc++.h> 8 #include<iostream> 9 #include<cstring> 10 #include<cmath> 11 #include<cstdio> 12 #define ll long long 13 #define mod 1000000007 14 #define PI acos(-1.0) 15 using namespace std; 16 int t; 17 ll l,r; 18 int num[25]; 19 ll dp[25][5]; 20 /* 21 1 奇奇 22 2 奇偶 23 3 偶奇 24 4 偶偶 25 */ 26 ll dfs(int pos,int status,int flag) 27 { 28 if(pos<1) 29 { 30 if(status==2||status==3) 31 return 1; 32 else 33 return 0; 34 } 35 if(!flag&&dp[pos][status]) 36 return dp[pos][status]; 37 int end=flag ? num[pos] : 9;//如果之前都是前导零 则当前这位可以取0~9 38 ll ans=0; 39 for(int i=0;i<=end;i++) 40 { 41 if(!status) 42 { 43 if(!i) 44 { 45 ans=dfs(pos-1,0,0); 46 } 47 else if(i&1) 48 { 49 ans+=dfs(pos-1,1,flag&&i==end); 50 } 51 else 52 { 53 ans+=dfs(pos-1,3,flag&&i==end); 54 } 55 56 } 57 else 58 { 59 if(status==1){ 60 if(i&1){ 61 ans+=dfs(pos-1,2,flag&&i==end); 62 } 63 } 64 else if(status==2){ 65 if(i&1){ 66 ans+=dfs(pos-1,1,flag&&i==end); 67 } 68 else{ 69 ans+=dfs(pos-1,3,flag&&i==end); 70 } 71 } 72 else if(status==3){ 73 if(i&1){ 74 ans+=dfs(pos-1,1,flag&&i==end); 75 } 76 else 77 ans+=dfs(pos-1,4,flag&&i==end); 78 } 79 else{ 80 if(!(i&1)) 81 { 82 ans+=dfs(pos-1,3,flag&&i==end); 83 } 84 } 85 } 86 } 87 dp[pos][status]=ans; 88 return ans; 89 } 90 ll slove(ll x) 91 { 92 memset(dp,0,sizeof(dp)); 93 int len=0; 94 while(x) 95 { 96 num[++len]=x%10; 97 x/=10; 98 } 99 return dfs(len,0,1); 100 } 101 int main() 102 { 103 scanf("%d",&t); 104 for(int i=1;i<=t;i++) 105 { 106 scanf("%I64d %I64d",&l,&r); 107 printf("Case #%d: %I64d\n",i,slove(r)-slove(l-1)); 108 } 109 return 0; 110 }
2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5887181.html