标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30114 Accepted Submission(s): 10603
1 #include <cstdio> 2 3 int dp[7][3]; 4 /** 5 * dp[i][0]:长度为i,不存在不吉利数 6 * dp[i][1]:长度为i,不存在不吉利数,且最高位为2 7 * dp[i][2]:长度为i,存在不吉利数 8 */ 9 int bit[8]; 10 11 void init() 12 { 13 dp[0][0] = 1; 14 dp[0][1] = 0; 15 dp[0][2] = 0; 16 for(int i = 1; i <= 6; ++i){ 17 dp[i][0] = dp[i-1][0]*9-dp[i-1][1]; 18 dp[i][1] = dp[i-1][0]; 19 dp[i][2] = dp[i-1][2]*10+dp[i-1][0]+dp[i-1][1]; 20 } 21 } 22 23 int solve(int n) 24 { 25 int len = 0; 26 int tmp = n; 27 while(n){ 28 bit[++len] = n%10; 29 n /= 10; 30 } 31 bit[len+1] = 0; 32 int ans = 0; 33 bool flag = false; 34 for(int i = len; i>0; --i){ 35 ans += bit[i]*dp[i-1][2]; 36 if(flag) 37 ans += bit[i]*dp[i-1][0]; 38 else{ 39 if(bit[i]>4) 40 ans += dp[i-1][0]; 41 if(bit[i+1] == 6 && bit[i]>2) 42 ans += dp[i][1]; 43 if(bit[i]>6) 44 ans += dp[i-1][1]; 45 } 46 if(bit[i] == 4 || (bit[i+1] == 6 && bit[i] == 2)) 47 flag = true; 48 } 49 if(flag) 50 ++ans; //这个数本身 51 return tmp-ans; 52 } 53 54 int main() 55 { 56 init(); 57 int n,m; 58 while(scanf("%d%d",&n,&m), n){ 59 printf("%d\n",solve(m)-solve(n-1)); 60 } 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/inmoonlight/p/5269214.html