SPOJ Raone (Digital DP)

Source: Internet
Author: User





Raone-ra-one NumbersNo 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 have special properties. He says he loves to destroy cities which has ra-one numbers as their ZIp Code.



Any number was 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 was 3,5,3 (unit place was location 1)



Digits at even location is 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 is about the Ra-one and wants to the deploy his Army members in those cities. 1 Army member'll be deployed.



G-one knows the range of Zip-codes where Ra-one might attack & needs your help to find out what many Army members he NE Eds.



Can you help him?


Input


First line'll has only one integer ' t ' of zip-code ranges. It's followed by T lines



Each line from 2nd line cotains 2 "from" and "to". These indicate the range of ZIP codes where Ra-one might attack. (From and to is included in the range)


Output


A single number for each test case telling the many Army members G-one needs to deploy.



Each number should is 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 

Links: http://www.spoj.com/problems/RAONE/en/
Test instructions: The number of points in which an interval even position minus an odd position is one



IDEA: Digital 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 have special properties. He says he loves to destroy cities which has ra-one numbers as their ZIp Code.



Any number was 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 was 3,5,3 (unit place was location 1)



Digits at even location is 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 is about the Ra-one and wants to the deploy his Army members in those cities. 1 Army member'll be deployed.



G-one knows the range of Zip-codes where Ra-one might attack & needs your help to find out what many Army members he NE Eds.



Can you help him?


Input


First line'll has only one integer ' t ' of zip-code ranges. It's followed by T lines



Each line from 2nd line cotains 2 "from" and "to". These indicate the range of ZIP codes where Ra-one might attack. (From and to is included in the range)


Output


A single number for each test case telling the many Army members G-one needs to deploy.



Each number should is on separate lines


Example

 Input:
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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



SPOJ Raone (Digital 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.