SPOJ LUCIFER (Digital DP)

Source: Internet
Author: User

Lucifer-lucifer numberNo Tags

Lucifer is the only human whi have defeated Ra-one in a computer game.

Ra-one are after Lucifer for revenge and G-one are there to protect him ...

All thi G-one and ra-one nonsense have disturbed lucifers life.

He wants to get Rid of Ra-one and kill him. He found that Ra-one can is killed only by the throwing Lucifer number of weapons at him.

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

Any number was LUCIFER number if the difference between Sum of digits at even location and Sum of digits at odd location I S 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 have access to a Warehouse which have lots of weapons.

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

Can you help him?

Input

First line would have a number ' t ' denoting the number of test cases.

Each of the following T lines would 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


t be is less than 100


/* Link: http://www.spoj.com/problems/LUCIFER/en/Test instructions: To find the number of numbers in an interval minus the odd position in a range of prime numbers: Digital DP Dp[i][j][k] The i-bit, the odd digit and the J, Even digit and k conditions */#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-8typedef Long long ll;using namespace std; #define N 11int 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;}





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

SPOJ LUCIFER (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.