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 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:
5200 250150 200100 15050 1000 50

 Output:
2163186
NOTE: t will be less than 100from 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;
}

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

时间: 2024-10-14 02:12:46

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

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 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 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 spec

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)   

[数位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 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