-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDp(using Check).cpp
More file actions
74 lines (72 loc) · 1.39 KB
/
Dp(using Check).cpp
File metadata and controls
74 lines (72 loc) · 1.39 KB
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
#include<bits/stdc++.h>
using namespace std;
int dp[10][100];
int arr[10];
int a[105];
int sieve()
{
fill(a,a+100,0);
a[0]=1;
a[1]=1;
for(int i=2;i<=10;i++)
{
if(a[i]==0)
{
for(int j=i*i;j<=100;j+=i)
{
a[j]=1;
}
}
}
}
int rec(int i,int s,bool check)
{
int r;
if(i<=-1)
{
if(a[s]==0)
return 1;
else
return 0;
}
if(!check && dp[i][s]!=-1)
return dp[i][s];
if(check==true)
{
r=arr[i];
}
else
r=9;
int ans=0;
for(int j=0;j<=r;j++)
{
ans+=rec(i-1,s+j,check&&(j==r));
}
if(!check)
dp[i][s]=ans;
return ans;
}
int digits(int i)
{
int k=0;
while(i!=0)
{
int d=i%10;
arr[k++]=d;
i=i/10;
}
return rec(k-1,0,true);
}
int main()
{
int t;
cin>>t;
sieve();
memset(dp,-1,sizeof(dp));
while(t--)
{
int f,l;
cin>>f>>l;
cout<<digits(l)-digits(f-1)<<"\n";
}
}