Spoj balnum Balanced Numbers (digital DP, State compression)

Source: Internet
Author: User
Tags bitset


balnum-balanced Numbers No Tags





Topic links



Balanced numbers has been used by mathematicians for centuries. A positive integer is considered a balanced number if:



1) Every even digit appears an odd number of times in its decimal representation



2) Every odd digit appears an even number of times in its decimal representation



For example, 211, 6222 and 112334445555677 is balanced numbers while 351, and 662 is not.



Given an interval [a, b], your task was to find the amount of balanced numbers in [a, b] where both A and B are included.


Input


The first line contains an integer T representing the number of test cases.



A test case consists of numbers a and B separated by A single space representing the interval. Assume that 1 <= A <= B <= 1019


Output


For each test case, you need to write a number in a line:the amount of balanced numbers in the corresponding inter Val


Example

Input:21 10001 9
Output:
147
4


The idea is to give a range and find the number of equilibrium numbers in this range. The equilibrium number means that each of the even numbers in each position of the number appears an odd number of times, and each odd number appears an even number of times. (also can not appear)
The practice is to save two states with two numbers t1, t2, whether t1 saves each digit in (0~9), and whether t2 saves (0~9) whether each even number appears odd or not, and whether odd number appears. Even times (pre-processed). In this case, (t1 & t2 == 0) can be used to determine whether the total state meets the condition. Obviously t1, t2 is equal to 2^10 = 1024.
And note that the first digit of the number flipped over in this question is 0, which will affect the answer, so if the high digit is 0, the initial state is passed down. I used zip to indicate the initial state of compression. After all, zip is a compression package _(:з"∠)_, and then simply do a digital dp.

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <bitset>
using namespace std;
#define ll long long
ll n, m;
ll dp[20][1024][1024];
int dig[20];
int zip;

void init() {
    for(int i = 0; i < 10; i += 2) zip ^= (1 << i);
    memset(dp, -1, sizeof(dp));
    /*bitset<10> btt(zip);
    cout << btt << endl;*/
}

void print(int num) {
    bitset<10> btt(num);
    cout << btt << endl;
}

ll dfs(int pos, int t1, int t2, int flag0, int lim) {
    //print(t1); print(t2); puts("");
    if(pos == -1) return (t1 & t2) == 0;
    if(!lim && dp[pos][t1][t2] != -1) return dp[pos][t1][t2];
    int End = lim ? dig[pos] : 9;
    ll ret = 0;
    for(int i = 0; i <= End; i++) {
        if(i == 0 && flag0) ret += dfs(pos - 1, 0, zip, 1, (i == End) && lim);
        else ret += dfs(pos - 1, t1 | (1 << i), t2 ^ (1 << i), 0, (i == End) && lim);
    }
    if(!lim) dp[pos][t1][t2] = ret;
    return ret;
}

ll func(ll num) {
    int n = 0;
    while(num) {
        dig[n++] = num % 10;
        num /= 10;
    }
    return dfs(n - 1, 0, zip, 1, 1);
}

int main() {
    init();
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%I64d %I64d", &n, &m);
        printf("%I64d\n", func(m) - func(n - 1));
    }
}

Spoj balnum Balanced Numbers (digital DP, State compression)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.