-
Notifications
You must be signed in to change notification settings - Fork 9
/
LUCIFER - LUCIFER Number.cpp
82 lines (81 loc) · 1.75 KB
/
LUCIFER - LUCIFER Number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> v;
int prime[110] = {0};
void sieve()
{
prime[0] = prime[1] = 1;
for(int i=2; i<110; i++){
if(prime[i]==0){
for(int j=i*i; j<110; j+=i){
prime[j] = 1;
}
}
}
}
ll dp[12][50][50][2];
ll fun(int pos, int even, int odd, int isSmall)
{
if(pos==v.size()){
int x = even-odd;
if(x<0) return 0;
return(prime[x]==0);
}
ll& ret = dp[pos][even][odd][isSmall];
if(ret!=-1) return ret;
ret = 0;
if(isSmall){
int ev = 0,od = 0;
for(int i=0; i<=9; i++){
if(pos%2==0)
ev = i;
else od = i;
ret+=fun(pos+1,even+ev, odd+od,1);
}
}
else{
int ev=0,od=0;
for(int i=0; i<v[pos]; i++){
if(pos%2==0)
ev = i;
else od = i;
ret+=fun(pos+1,even+ev, odd+od,1);
}
if(pos%2==0)ret+=fun(pos+1,even+v[pos], odd,0);
else ret+=fun(pos+1,even, odd+v[pos],0);
}
return ret;
}
ll solve(ll n)
{
if(n<=0) return 0;
v.clear();
for(int i=1; i<=10; i++){
v.push_back(n%10);
n/=10;
}
reverse(v.begin(), v.end());
// for(int i=0; i<v.size(); i++){
// cout<<v[i]<<" ";
// }
// cout<<endl;
memset(dp, -1, sizeof dp);
return fun(0,0,0,0);
}
int main()
{
sieve();
ll a,b;
int tc;
scanf("%d",&tc);
while(tc--){
scanf("%lld%lld",&a,&b);
if(a>b) swap(a,b);
printf("%lld\n",solve(b) - solve(a-1));
}
return 0;
}