SPOJ RAONE(数位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:
21 1010 100

 Output:
19explanation:
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 100from and to will be between 0 and 10^8 inclusive 

链接:http://www.spoj.com/problems/RAONE/en/

题意:求一个区间 偶数位置减去奇数位置的差为一的数的个数

思路:数位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 bug printf("hihi\n")

#define eps 1e-8

using namespace std;

#define INF 0x3f3f3f3f
#define N  100

int dp[10][100][100];
int bit[20];

int dfs(int pos,int le,int ri,bool bound)
{
    if(pos==0) return ri-le==1 ? 1:0;
    if(!bound&&dp[pos][le][ri]>=0) return dp[pos][le][ri];
    int up=bound ? bit[pos]:9;
    int ans=0;
    for(int i=0;i<=up;i++)
    {
        if(pos&1)
            ans+=dfs(pos-1,le+i,ri,bound&&i==up);
        else
            ans+=dfs(pos-1,le,ri+i,bound&&i==up);
    }
    if(!bound) dp[pos][le][ri]=ans;
    return ans;
}

int solve(int x)
{
    int i,j;
    int len=0;
    while(x)
    {
        bit[++len]=x%10;
        x/=10;
    }
    return dfs(len,0,0,true);
}

int main()
{
   int i,j;
   memset(dp,-1,sizeof(dp));
   int t;
   scanf("%d",&t);
   int le,ri;
   while(t--)
   {
       scanf("%d%d",&le,&ri);
       printf("%d\n",solve(ri)-solve(le-1));
   }
   return 0;
}

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:
21 1010 100

 Output:
19explanation:
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 100from and to will be between 0 and 10^8 inclusive 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-03 12:10:12

SPOJ RAONE(数位dp)的相关文章

SPOJ LUCIFER (数位dp)

LUCIFER - LUCIFER Number no tags Lucifer is the only human whi has defeated RA-ONE in a computer game .. RA-One is after lucifer for revenge and G-One is there to protect him ... All thi G-One and Ra-one Nonsense has disturbed lucifers life.. He want

SPOJ BALNUM (数位DP)

题意:求区间内出现过的奇数是偶数,出现过的偶数是奇数的个数. 析:这个题是要三进制进行操作的.dp[i][j] 表示前 i 位,状态是 j,可以用三进制来表示 0表示没有出现,1表示奇数,2表示偶数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include

spoj RAONE - Ra-One Numbers (数位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 spec

[数位dp] spoj 10738 Ra-One Numbers

题意:给定x.y,为[x,y]之间有多少个数的偶数位和减去奇数位和等于一. 个位是第一位. 例子: 10=1-0=1 所以10是这样的数 思路:数位dp[i][sum][ok] i位和为sum 是否含有前导0. 然后就是因为有负数 所以根据范围把0设置为100 然后最后和等于101则为所求的数. 代码: [cpp] view plaincopyprint? #include"cstdlib" #include"cstdio" #include"cstrin

SPOJ MYQ10 Mirror Number 数位dp&#39;

题目链接:点击打开链接 MYQ10 - Mirror Number A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b, find the number

SPOJ MYQ10 10649. Mirror Number (数位dp)

SPOJ MYQ10 10649. Mirror Number (数位dp) ACM 题目地址:SPOJ MYQ10 Mirror Number 题意: 求[a,b]中镜像回文的个数. 0 <= a<=b <= 10^44 分析: 看到题目和数据范围就知道是数位dp了. 很明显镜像回文只有0,1,8,跟回文的一题一样,在dfs的时候得开个辅助数组记录前面已经选择的数字. 注意还得去掉前导0. 代码: /* * Author: illuz <iilluzen[at]gmail.com

SPOJ BALNUM Balanced Numbers 状压+数位DP

一开始想了一个用二进制状压的方法,发现空间需要的太大,光光memset都要超时 = = 其实不用每次都memset 也可以用三进制,一开始直接打表出所有的状态转移就好 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream&g

SPOJ NUMTSN NUMTSN - 369 Numbers(数位dp)

NUMTSN - 369 Numbers no tags 7. 369 numbers A number is said to be a 369 number if The count of 3s is equal to count of 6s and the count of 6s is equal to count of 9s. The count of 3s is at least 1. For Example 12369, 383676989, 396 all are 369 numbe

SPOJ BALNUM Balanced Numbers(数位dp,状态压缩)

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)