标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1423 Accepted Submission(s): 544
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 LL dp[25][1<<10][11],L,R; 5 int k,b[25],dy[1<<10]; 6 int calc(int st){ 7 if(dy[st] != -1) return dy[st]; 8 int x = st; 9 st = (st&0x55555555) + ((st>>1)&0x55555555); 10 st = (st&0x33333333) + ((st>>2)&0x33333333); 11 st = (st&0x0F0F0F0F) + ((st>>4)&0x0F0F0F0F); 12 st = (st&0x00FF00FF) + ((st>>8)&0x00FF00FF); 13 st = (st&0x0000FFFF) + ((st>>16)&0x0000FFFF); 14 return dy[x] = st; 15 } 16 int update(int x,int s){ 17 for(int i = x; i < 10; ++i) 18 if(s&(1<<i)) return (s^(1<<i))|(1<<x); 19 return s|(1<<x); 20 } 21 LL dfs(int p,int st,bool flag,bool low){ 22 if(!p) return calc(st) == k; 23 if(flag && dp[p][st][k] != -1) return dp[p][st][k]; 24 LL ret = 0; 25 int u = flag?9:b[p]; 26 for(int i = 0; i <= u; ++i) 27 ret += dfs(p-1,(low || i > 0)?update(i,st):0,flag || (i < u),(low || i > 0)); 28 if(flag) dp[p][st][k] = ret; 29 return ret; 30 } 31 LL solve(LL x){ 32 int len = 0; 33 while(x){ 34 b[++len] = x%10; 35 x /= 10; 36 } 37 return dfs(len,0,0,0); 38 } 39 int main(){ 40 int kase,cs = 1; 41 memset(dp,-1,sizeof dp); 42 memset(dy,-1,sizeof dy); 43 scanf("%d",&kase); 44 while(kase--){ 45 scanf("%I64d%I64d%d",&L,&R,&k); 46 printf("Case #%d: ",cs++); 47 printf("%I64d\n",solve(R) - solve(L-1)); 48 } 49 return 0; 50 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4782349.html