spoj 10606 BALNUM - Balanced Numbers 数位dp

链接:http://www.spoj.com/problems/BALNUM/


BALNUM - Balanced Numbers

no tags 

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

题意:

问A到B之间有多少个数,有多少数 符合以下两个条件。

数位0-9 中 ,每一个奇数数位有 偶数个, 偶数数位有奇数个。

也就是如果有1 的话,那必须有偶数个1。如果有2的话,必须有奇数个2,当然没有也可以。


做法:

弄个三进制记录状态,表示0-9 数位,各个数位出现过没有,没有出现在对应的三进制位标记0,出现奇数次  标记1, 出现偶数次标记2。

注意前导零是不能算的。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map> 


typedef long long LL;
const int maxn=22;
int dig[maxn];
int san[maxn];
LL f[maxn][60000];//LL可能超内存
//之后的偶数 要出现 奇数次   奇数出现偶数次    是不是前导0  前导0 都不算
// 0 出现过  1 奇数次  2 偶数次
LL dfs(int pos,int sta,int limit){
    if (pos<0) 
	{
		if(limit==1)
			int kk=1;
		for(int i=0;i<=9;i++)
		{
			int tem=sta%3;
			sta/=3;
			if(tem!=0)
			{
				if(tem%2==1&&i%2==1)
					return 0;
				if(tem%2==0&&i%2==0)
					return 0;
			}
		}
		return 1;
	}
    if (!limit&&f[pos][sta]!=-1) return f[pos][sta];
    LL res=0;
    int last=limit?dig[pos]:9;
    for (int i=0;i<=last;i++){ 

		int tsta=sta;
		if(i==0)
		{
			if(tsta!=0)//前面有任何数出现过  就不是前导0
			{  
				int tem=tsta%3;
				tsta=tsta/3*3;
				if(tem==1) tem=2;
				else tem=1;
				tsta+=tem;
			}
		}
		else
		{
			int tem=tsta/san[i]%3;
			int hou=tsta%san[i];
			if(hou!=tsta%san[i+1])
				int kk=1;
			int qian=tsta/san[i+1]*san[i+1];

			if(tem==0||tem==2) tem=1;
			else tem=2;

			tsta=qian+tem*san[i]+hou; 
		}

		res+=dfs(pos-1,tsta,limit&&(i==last));
    }
    if (!limit) f[pos][sta]=res;
    return res;
}

LL solve(LL n){ 
    int len=0;
    while (n){
        dig[len++]=n%10;
        n/=10;
    }
    return dfs(len-1,0,1);
}

int main()
{
	san[0]=1;
	for(int i=1;i<=10;i++)
		san[i]=san[i-1]*3;
	memset(f,-1,sizeof f);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		LL a,b;
		//scanf("%lld",&a);
		cin>>a>>b;
		cout<<solve(b)-solve(a-1)<<endl;
		//cin>>a;
	//	cout<<solve(a)<<endl;
	} 
	return 0;
}












你可能感兴趣的:(dp)