Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <vector>
- using namespace std;
- #define MAXDIG 20
- #define MAXPOT 12
- #define PRAZNO 0
- #define NEPARNO 1
- #define PARNO 2
- #define MAXMASK 60000
- int dig[MAXDIG];
- int pot[MAXPOT];
- long long dp[MAXDIG][MAXMASK][2][2];
- long long rec(int x, int mask, int dira, int poz){
- int currmask = mask;
- if(MAXDIG == x){
- for(int i = 0; i < 10; ++i){
- int d = currmask % 3;
- currmask /= 3;
- if(i % 2 == 0 && PARNO == d)
- return 0;
- if(i % 2 == 1 && NEPARNO == d)
- return 0;
- }
- return 1;
- }
- long long &ref = dp[x][mask][dira][poz];
- if(-1 != ref)
- return ref;
- ref = 0;
- for(int i = 0; i < 10; ++i){
- if(dira && i > dig[x])
- break;
- int d = currmask % 3;
- currmask /= 3;
- int newmask;
- if(PRAZNO == d){
- newmask = mask + (NEPARNO - PRAZNO) * pot[i];
- }else if(PARNO == d){
- newmask = mask + (NEPARNO - PARNO) * pot[i];
- }else{
- newmask = mask + (PARNO - NEPARNO) * pot[i];
- }
- if(!poz && i == 0)
- newmask = mask;
- ref += rec(x+1, newmask, dira && (dig[x] == i), poz || (i > 0));
- }
- return ref;
- }
- long long count(long long x){
- memset(dig, 0, sizeof dig);
- for(int i = MAXDIG-1; i >= 0; --i){
- dig[i] = x % 10;
- x /= 10;
- }
- memset(dp, -1, sizeof dp);
- return rec(0, 0, 1, 0);
- }
- void run(){
- long long a, b;
- scanf("%lld%lld", &a, &b);
- long long ret = count(b) - count(a-1);
- printf("%lld\n", ret);
- }
- int main(){
- pot[0] = 1;
- for(int i = 1; i < MAXPOT; ++i)
- pot[i] = 3 * pot[i-1];
- int T;
- scanf("%d", &T);
- for(int i = 0; i < T; ++i)
- run();
- return 0;
- }
Add Comment
Please, Sign In to add comment