Description
Balanced numbers have 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, 77, 211, 6222 and 112334445555677 are balanced numbers while 351, 21, and 662 are not.
Given an interval [A, B], your task is 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 two numbers A and B separated by a single space representing the interval. You may assume that 1 <= A <= B <= 1019
Output
For each test case, you need to write a number in a single line: the amount of balanced numbers in the corresponding interval
Example
Input: 2 1 1000 1 9
Output: 147 4
Hint
Added by: | Angel Paredes |
Date: | 2012-02-12 |
Time limit: | 0.123s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: PERL 6 |
Resource: | Cuban Olympiad in Informatics 2012 - Day 2 Problem A |
对于一个数如果这个数中 每个偶数出现的次数是奇数 奇数出现的是偶数 那么为所求的
我们用 0,1,2 表示(0~9)中某个数出现0次 奇数次 偶数次
蒟蒻不太会状态压缩 就xjb开了11维的dp - -
ACcode:
#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
ll dp[22][3][3][3][3][3][3][3][3][3][3];
int data[20];
ll dfs(int len,int is_0,int is_1,int is_2,int is_3,int is_4,int is_5,int is_6,int is_7,int is_8,int is_9,int pre,int limit){
if(!len){
if(is_0!=2&&is_1!=1&&is_2!=2&&is_3!=1&&is_4!=2&&is_5!=1&&is_6!=2&&is_7!=1&&is_8!=2&&is_9!=1)
return 1;
return 0;
}
if(!limit&&dp[len][is_0][is_1][is_2][is_3][is_4][is_5][is_6][is_7][is_8][is_9]!=-1)
return dp[len][is_0][is_1][is_2][is_3][is_4][is_5][is_6][is_7][is_8][is_9];
int ed=limit?data[len]:9;
ll ans=0;
for(int i=0;i<=ed;++i){
if(i==0){
if(pre)
ans+=dfs(len-1,is_0,is_1,is_2,is_3,is_4,is_5,is_6,is_7,is_8,is_9,1,limit&&i==ed);
else
ans+=dfs(len-1,(is_0+1)%2==0?2:1,is_1,is_2,is_3,is_4,is_5,is_6,is_7,is_8,is_9,0,limit&&i==ed);
}
else if(i==1)
ans+=dfs(len-1,is_0,(is_1+1)%2==0?2:1,is_2,is_3,is_4,is_5,is_6,is_7,is_8,is_9,0,limit&&i==ed);
else if(i==2)
ans+=dfs(len-1,is_0,is_1,(is_2+1)%2==0?2:1,is_3,is_4,is_5,is_6,is_7,is_8,is_9,0,limit&&i==ed);
else if(i==3)
ans+=dfs(len-1,is_0,is_1,is_2,(is_3+1)%2==0?2:1,is_4,is_5,is_6,is_7,is_8,is_9,0,limit&&i==ed);
else if(i==4)
ans+=dfs(len-1,is_0,is_1,is_2,is_3,(is_4+1)%2==0?2:1,is_5,is_6,is_7,is_8,is_9,0,limit&&i==ed);
else if(i==5)
ans+=dfs(len-1,is_0,is_1,is_2,is_3,is_4,(is_5+1)%2==0?2:1,is_6,is_7,is_8,is_9,0,limit&&i==ed);
else if(i==6)
ans+=dfs(len-1,is_0,is_1,is_2,is_3,is_4,is_5,(is_6+1)%2==0?2:1,is_7,is_8,is_9,0,limit&&i==ed);
else if(i==7)
ans+=dfs(len-1,is_0,is_1,is_2,is_3,is_4,is_5,is_6,(is_7+1)%2==0?2:1,is_8,is_9,0,limit&&i==ed);
else if(i==8)
ans+=dfs(len-1,is_0,is_1,is_2,is_3,is_4,is_5,is_6,is_7,(is_8+1)%2==0?2:1,is_9,0,limit&&i==ed);
else if(i==9)
ans+=dfs(len-1,is_0,is_1,is_2,is_3,is_4,is_5,is_6,is_7,is_8,(is_9+1)%2==0?2:1,0,limit&&i==ed);
}
return limit?ans:dp[len][is_0][is_1][is_2][is_3][is_4][is_5][is_6][is_7][is_8][is_9]=ans;
}
ll fun(ll a){
int len=0;
while(a){
data[++len]=a%10;
a/=10;
}
return dfs(len,0,0,0,0,0,0,0,0,0,0,1,1);
}
void doit(){
ll a,b;
scanf("%lld%lld",&a,&b);
printf("%lld\n",fun(b)-fun(a-1));
}
int main(){
int loop;memset(dp,-1,sizeof(dp));
scanf("%d",&loop);
while(loop--)doit();
return 0;
}
看起来还是蛮带感的qaq