码迷,mamicode.com
首页 > 其他好文 > 详细

spoj RAONE - Ra-One Numbers (数位dp)

时间:2015-04-19 10:14:09      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:数位dp

RAONE - Ra-One Numbers

no tags 

In the War between good and evil . Ra-One is on the evil side and G-One on the good side.

Ra-One is fond of destroying cities and its G-one‘s duty to protect them..

 

Ra-One loves to destroy cities whose Zip Code has special properties. He says he loves to destroy cities which have Ra-One numbers as their ZIp Code.

Any number is Ra-one if the Difference between Sum of digits at even location and Sum of digits at odd location is One (1).. For eg... for 234563 is Ra-One number

digits at odd location are 3,5,3 (unit place is location 1 )

digits at even location are 2,4,6

Diff = (2+4+6)-(3+5+3)=12-11 = 1.

And 123456 is not Ra-One number

diff = (5+3+1) - (2+4+6) = -4

 

G-One knows this about Ra-one and wants to deploy his Army members in those cities. 1 army member will be deployed in each city.

G-one knows the range of ZIP-Codes where Ra-One might attack & needs your help to find out how many army members he needs.

Can you help Him ?

Input

first line will have only one integer ‘t‘ number of Zip-Code ranges. it is followed by t lines

each line from 2nd line cotains 2 integer ‘from‘  and ‘to‘. these indicate the range of Zip codes where Ra-one might attack .(from and to are included in the range)

Output

A single number for each test case telling how many army members G-One needs to deploy.

each number should be on separate lines

Example

Input:
2
1 10
10 100


 Output:
1
9
explanation:
for 1st test case the only number is 10
for 2nd test case numbers are 10,21,32,43,54,65,76,87,98

NOTE:
t will be less than 100
from and to will be between 0 and 10^8 inclusive

题意:就是在区间中偶数位和减去奇数位和等于1的数有多少个


思路:直接数位dp记忆化


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
//typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;
#define mod 1000000007

#define INF 0x3f3f3f3f
#define N  500

int dp[10][N];
int bit[N];

int dfs(int pos,int va,bool bound)
{
	if(pos==0) return va==1;
	if(!bound&&dp[pos][va]!=-1) return dp[pos][va];

	int up=bound ? bit[pos]:9;

	int i,t;
	int ans=0;

	fre(i,0,up+1)
	{
	   if(pos&1)  t=-1;
	   else  t=1;

	   ans+=dfs(pos-1,va+t*i,bound&&i==up);
	}
   if(!bound) dp[pos][va]=ans;
   return ans;
}

int solve(int x)
{
	int len=0;
	while(x)
	{
		bit[++len]=x%10;
		x/=10;
	}
   return dfs(len,0,true);
}
int main()
{
	mem(dp,-1);
	int le,ri;
	int t;
	sf(t);
	while(t--)
	{
		sff(le,ri);
		pf("%d\n",solve(ri)-solve(le-1));
	}

  pf("\n");

}




spoj RAONE - Ra-One Numbers (数位dp)

标签:数位dp

原文地址:http://blog.csdn.net/u014737310/article/details/45126517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!