POJ 3286 How between 0 & #39; s? (Digital dp)
Question
Input n, m, evaluate n ~ The total number of digits 0 in the m range.
Ideas
Use 2034 as an example.
The number of times that 0 appears in bits
When a single position is 0, you do not need to consider it later. You only need to consider the front. Because 0 is smaller than 4, even if the front is obtained to the maximum, the front can be 1 ~ 203 (because the current BIT is 0, the front cannot be 0 ). A total of 203 types.
10: when the value of 10 is 0, the first value is 1 ~ 20, followed by 0 ~ 9. A total of 123*10 types.
: When the value of a hundred bits is 0, because 0 is equal to the upper limit of the Current BITs, 0 ~ can be taken after the first 1 ~ 99. When the preceding value is 2, the following value can only be 0 ~ 34. A total of 1*100 + 35 types.
The number of thousands cannot be 0, so the total number is 0.
Convert the above idea into code.
Code
#include
#include #include
#include
#include
#include
#include
#include
using namespace std;const int N = 10009;#define LL long longLL p[20];void init(){ p[0] = 1; for(int i=1; i<18; i++) p[i] = p[i-1]*10;}LL solve(LL x){ if(x == -1) return -1; LL ans = 0; for(int i=1; ; i++) { LL l = x/p[i]; LL r = x%p[i-1]; LL now = x%p[i]/p[i-1]; if(now > 0) ans += l*p[i-1]; else ans += (l-1)*p[i-1] + r+1; if(p[i] > x) break; } return ans;}int main(){ LL n, m; init(); while(cin>>n>>m && (n!=-1 || m!=-1)) printf("%lld\n", solve(m) - solve(n-1)); return 0;}