HDU 2089 - 不要62
还是基础,不多说。。。- -坑爹悲催的我正在学数数。。。
1 /* 2 ID:esxgx1 3 LANG:C++ 4 PROG:hdu2089 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 12 #define NN 9 13 unsigned dp[NN][4]; 14 15 void work(int LN) 16 { 17 dp[0][0] = 1; 18 for(int i=1; i<=LN; ++i) { 19 dp[i][0] = dp[i-1][0] * 9 - dp[i-1][1]; /// 无4和62的【含1】 20 dp[i][1] = dp[i-1][0]; /// 开头是2的, 无62无4 21 dp[i][2] = dp[i-1][2] * 10 + dp[i-1][0] + dp[i-1][1]; /// 有4或者62的 22 // printf("%d 0=%d, 1=%d, 2=%d\n", i, dp[i][0], dp[i][1], dp[i][2]); 23 } 24 } 25 26 unsigned solve(int i, int n, int &extra) 27 { 28 unsigned curr = n % 10; 29 unsigned rslt = curr * dp[i-1][2]; 30 int extra0 = extra; 31 if (n/10) rslt += solve(i+1, n/10, extra0); 32 if (extra0) { 33 rslt += curr * dp[i-1][0]; 34 extra = 1; 35 } else { 36 if (curr > 4) rslt += dp[i-1][0]; 37 else if (curr == 4) extra = 1; 38 39 if (n/10 % 10 > 6) rslt += dp[i][1]; 40 else if (n/10 % 10 == 6) { 41 if (curr > 2) rslt += dp[i][1]; 42 else if (curr == 2) extra = 1; 43 } 44 } 45 return rslt; 46 } 47 48 #define sol(n) ({int e=0; solve(1, n, e) + (e?1:0);}) 49 50 int main(void) 51 { 52 #ifndef ONLINE_JUDGE 53 freopen("in.txt", "r", stdin); 54 #endif 55 work(NN); 56 unsigned N, M; 57 while(scanf("%u%u", &N, &M) > 0) { 58 if (!(N || M)) break; 59 int e0, e1; 60 e0 = e1 = 0, N--; 61 printf("%u\n", M - N - (sol(M) - sol(N))); 62 } 63 return 0; 64 }
2014-08-07 23:43:32 | Accepted | 2089 | 0MS | 320K | 1357 B | G++ |
This article is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。