标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 32358 Accepted Submission(s): 11514
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define ll long long ll dp[50][5]; char ta[50], tb[50]; string n, m; ll nn, mm; ll dfs(string dig, int pos, int flag, int lim) { if(pos == -1) return (flag == 2) || (flag == 4); if(!lim && dp[pos][flag] != -1) return dp[pos][flag]; int End = lim ? dig[pos] - ‘0‘ : 9; ll ans = 0; for(int i = 0; i <= End; i++) { int tmp = flag; if(i == 4 || flag == 4) tmp = 4; else if(flag == 0 && i == 6) tmp = 1; else if(flag == 1 && i != 6 && i != 2) tmp = 0; else if(flag == 1 && i == 2) tmp = 2; ans += dfs(dig, pos - 1, tmp, lim && i == End); } if(!lim) dp[pos][flag] = ans; return ans; } int main() { while(~scanf("%I64d %I64d", &nn, &mm)) { if(nn == 0 && mm == 0) break; memset(dp, -1, sizeof(dp)); nn--; sprintf(ta, "%I64d", nn); sprintf(tb, "%I64d", mm); n = ta; m = tb; reverse(n.begin(), n.end()); reverse(m.begin(), m.end()); ll cnt = dfs(m, m.size() - 1, 0, 1) - dfs(n, n.size() - 1, 0, 1); printf("%I64d\n", mm - nn - cnt); } }
标签:
原文地址:http://www.cnblogs.com/lonewanderer/p/5654562.html