SPOJ10606---Balanced Numbers (tri-digit DP)

Source: Internet
Author: User

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:
2
1 1000
1 9

Output:
147
4

Three-in-state + digital DP

/************************************************************************* > File Name:spoj10606.cpp > Au Thor:alex > Mail: [email protected] > Created time:2015 February 28 Saturday 19:43 30 seconds ****************************** ******************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL; LL dp[ -][60000];intbit[ -];intthree[ -];ints[ -];intGetsta (intStaintE) {intSS =0;memsetS0,sizeof(S));intCNT =0; while(STA) {s[cnt++] = sta%3; STA/=3; }if((++s[e]) = =3) {S[e] =1; } for(inti =0; I <=9;    ++i) {SS + = s[i] * (Three[i]); }returnSS;}BOOLJudge (intSTA) {memsetS0,sizeof(S));intCNT =0; while(STA) {s[cnt++] = sta%3; STA/=3; } for(inti =0; I <=9; ++i) {if(! S[i]) {Continue; }if((I &1)! = (S[i] &1))        {Continue; }Else{return 0; }    }return 1;} LL DFS (intCurintEintStaBOOLFlagBOOLZero) {if(cur = =-1)    {if(zero) {return 0; }returnJudge (STA); }if(!flag && ~dp[cur][sta]) {returnDp[cur][sta]; } LL ans =0;intEnd = Flag? Bit[cur]:9; for(inti =0; I <= end; ++i) {intNewsta = Getsta (STA, i);if(Zero &&!i) {ans + = DFS (cur-1,0,0, Flag && (i = = end),1); }Else if(Zero && i) {ans + = DFS (cur-1, I, Three[i], flag && (i = = end),0); }Else{ans + = DFS (cur-1, I, Newsta, flag && (i = = end),0); }    }if(!flag)    {Dp[cur][sta] = ans; }returnAns;} LL Calc (intN) {intCNT =0; while(n) {bit[cnt++] = nTen; N/=Ten; }returnDFS (CNT-1,0,0,1,1);}intMain () {intT    LL L, R; three[0] =1;memset(DP,-1,sizeof(DP)); for(inti =1; I <=9; ++i) {Three[i] =3* Three[i-1]; }scanf("%d", &t); while(t--) {scanf("%lld%lld", &l, &r);printf("%lld\n", Calc (r)-Calc (L-1)); }return 0;}

SPOJ10606---Balanced Numbers (tri-digit DP)

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.