-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdp_primes_cumulative.cpp
56 lines (53 loc) · 1004 Bytes
/
dp_primes_cumulative.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
/*
* Dynamic Programming
* Question:https://www.hackerrank.com/contests/coding-club-delhi-contest/challenges/bhawana-with-her-new-game/submissions/code/1309662131
* */
#include<bits/stdc++.h>
using namespace std;
typedef long l;
typedef long double ld;
int main()
{
vector<l> pre;
for(l i=0;i<=5000000;++i)
{
pre.push_back(1);
}
for(l i=2;i*i<=5000000;++i)
{
if(pre[i]==1)
{
l j=2;
while(i*j<=5000000)
{
pre[i*j]=0;
++j;
}
}
}
for(l i=2;i*i<=5000000;++i)
{
if(pre[i]==1)
{
l j=2;
while(i*j<=5000000)
{
pre[i*j]=pre[i]+pre[j];
++j;
}
}
}
for(l i=2;i<=5000000;++i)
{
pre[i]+=pre[i-1];
}
l t;
cin>>t;
while(t--)
{
l a,b;
cin>>a>>b;
l ans=pre[a]-pre[b];
cout<<ans<<"\n";
}
}