forked from bhavyahoda/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsievePrimes.cpp
More file actions
260 lines (228 loc) · 5.57 KB
/
sievePrimes.cpp
File metadata and controls
260 lines (228 loc) · 5.57 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// || || |____| //
// || || || //
// \\__// || //
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ll, null_type,greater_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long int ll;
typedef long double ld;
#include <bitset>
#define mp make_pair
#define F first
#define S second
#define vll vector <ll>
#define pb push_back
#define M 1000000007
#define INT_BITS 32
#define SIZE_INT sizeof(int) * 8
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test ll t;read(t);while(t--)
#define f(a, b) for (ll i = a; i < b; i++)
#define fr(a, b) for (ll j = a; j >= b; j--)
#define fi(a, b) for (ll j = a; j < b; j++)
#define fii(a, b) for (ll k = a; k < b; k++)
#define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))
using namespace std;
using namespace __gnu_pbds;
template <typename T>
void read(T &x){
cin >> x;
}
template <typename T , typename T0>
void read(T &x,T0 &y){
cin >> x >> y;
}
template <typename T , typename T0 , typename T1>
void read(T &x,T0 &y,T1 &z){
cin >> x >> y >> z;
}
template <typename T , typename T0 , typename T1 , typename T2>
void read(T &x,T0 &y,T1 &z,T2 &w){
cin >> x >> y >> z >> w;
}
template <typename T>
void write1(T &x){
cout << x << " ";
}
template <typename T>
void write(T &x){
cout << x << "\n";
}
template <typename T , typename T0>
void write(T &x,T0 &y){
cout << x << " " << y << "\n";
}
template <typename T , typename T0 , typename T1>
void write(T &x,T0 &y,T1 &z){
cout << x << " " << y << " " << z << "\n";
}
template <typename T , typename T0 , typename T1 , typename T2>
void write(T &x,T0 &y,T1 &z,T2 &w){
cout << x << " " << y << " " << z << " " << w << "\n";
}
//GCD of 2 numbers START
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
//LCM of two numbers STARTS
ll lcm(ll a, ll b){
return a / __gcd(a, b) * b;
}
//Check Prime STARTS
int IsPrime(ll n)
{
if((n%2==0 && n!=2) || n==1)
return 0;
for (ll i=3;i*i<=n;i+=2)
{
if (n%i==0)
return 0;
}
return 1;
}
//SIEVE
int sieve(ll N) {
bool isPrime[N+1];
for(ll i = 0; i <= N;++i) {
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for(ll i = 2; i * i <= N; ++i) {
if(isPrime[i] == true) { //Mark all the multiples of i as composite numbers
for(int j = i * i; j <= N ;j += i)
isPrime[j] = false;
}
}
//Count
/*ll c=0;
for (int p=2; p<=N; p++)
{
if (isPrime[p])
c++;
}
return c;*/
//print
/*for (int p=2; p<=N; p++)
{
if (isPrime[p])
cout << p << " ";
}
return 0;
*/
}
// nCr CAL in O(k) STARTS
int nCr(ll n, ll r)
{
ll res = 1;
if ( r > n - r )
r = n-r;
for (ll i = 0; i < r; ++i)
{
res *= (n - i); //general term (n-x)/(1+x)
res /= (i + 1);
}
return res;
}
//BINARY SEARCH STARTS
int BS(int a[], int l, int r, int find)
{
if (l<=r)
{
int mid=(l + r)/2;
if (a[mid]==find)
return mid ;
if (a[mid]>find)
return BS(a,l,mid-1,find);
if (a[mid]<find)
return BS(a,mid+1,r,find);
}
return -1;
}
//POWER function STARTS
ll power(ll x, ll y)
{
ll temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
//BINARY EXPO
ll binaryExponentiation(ll x,ll n)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return binaryExponentiation(x*x,n/2);
else //n is odd
return x*binaryExponentiation(x*x,(n-1)/2);
}
//MODULAR EXPO
ll modularExponentiation(ll x,ll n,ll Mod)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return modularExponentiation((x*x)%Mod,n/2,Mod);
else //n is odd
return (x*modularExponentiation((x*x)%Mod,(n-1)/2,Mod))%Mod;
}
//FERMAS LITTLE THEORM
ll Ferma(ll A,ll Mod)
{ // works only if M is prime or else go for Euclid.
return modularExponentiation(A,Mod-2,Mod);
}
//BINARY TO DECIMAL STARTS
int binaryToDecimal(ll n)
{
ll num = n, dec_value = 0,base = 1;
ll temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
//Count set Bits function STARTS
ll countSetBits(ll n) {
if (n == 0)
return 0;
else
return 1 + countSetBits(n & (n - 1));
}
//START CODE
int main()
{
fast;
ll primefac[100001]={0};
for(ll i=2;i<100001;i++)
{
if(primefac[i]==0)
{
for(ll j=i;j<100001;j+=i)
primefac[j]++;
}
}
test
{
ll a,b,k; read(a,b,k);
ll c=0;
for(int i=a;i<=b;i++)
{
if(primefac[i]==k)
c++;
}
write(c);
}
return 0;
}