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

SPOJ LUCIFER (数位dp)

时间:2015-07-24 14:19:59      阅读:117      评论:0      收藏:0      [点我收藏+]

标签: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 wants to get Rid of Ra-One and kill him . He found that Ra-One can be killed only by throwing Lucifer number of weapons at him.

Lucifer number  shares the some properties of Ra-One Numbers numbers and G-One Numbers

Any number is LUCIFER NUMBER  if the Difference between Sum of digits at even location and Sum of digits at odd location is prime number .. For eg... for 20314210 is lucifer number

digits at odd location 0,2,1,0

digits at even location 1,4,3,2

diff = (1+4+3+2)-(0+2+1+0)=10-3  = 7 ..... a prime number.

Lucifer has access to a Warehouse which has lots of weapons ..

He wants to know in how many ways can he kill him.

Can you help him?

 

Input

First line will have a number ‘t‘ denoting the number of test cases.

each of the following t lines will have 2 numbers ‘a‘ , ‘b‘

Output

Print single number per test case, depicting the count of Lucifer numbers in the range a,b inclusive.

Example

Input:

5
200 250
150 200
100 150
50 100
0 50

 Output:
2
16
3
18
6


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


/*
链接:http://www.spoj.com/problems/LUCIFER/en/
题意:求一个区间内偶数位置减去奇数位置的差为质数的数的个数
思路:数位dp dp[i][j][k] 第i位,奇数位和为j,偶数位和为k的情况
*/

#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

typedef long long ll;

using namespace std;
#define N 11

int dp[N][90][90];
int bit[N];
int pri[200];

void inint()
{
    int i,j;
    pri[1]=1;
    pri[0]=1;
    for(i=2;i<200;i++)
        if(!pri[i])
          for(j=i*2;j<200;j+=i)
             pri[j]=1;

}

int dfs(int pos,int odd,int even,bool bound)
{
    if(pos==0)
    {
        if(even<=odd) return 0;
        if(pri[even-odd]) return 0;
        return 1;
    }

    if(!bound&&dp[pos][odd][even]>=0) return dp[pos][odd][even];

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

    for(int i=0;i<=up;i++)
    {
        int tt;
        if(pos&1)
            tt=dfs(pos-1,odd+i,even,bound&&i==up);
        else
            tt=dfs(pos-1,odd,even+i,bound&&i==up);
        ans+=tt;
    }


   if(!bound) dp[pos][odd][even]=ans;
   return ans;
}

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

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





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

SPOJ LUCIFER (数位dp)

标签:dp

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

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