Problem Link
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100000;
int div1[N + 1];
int dp[N + 1][6];
void sieve()
{
memset(div1, 0, sizeof div1);
for(int i = 2; i <= N; i++)
{
if(div1[i] == 0)
{
for(int j = i; j <= N; j += i)
{
div1[j]++;
}
}
}
// precompute number of k-prime before i
for(int i = 2; i <= N; i++)
{
for(int k = 1; k <= 5; k++)
{
dp[i][k] = dp[i - 1][k] + (div1[i] == k?1:0);
}
}
}
int main()
{
int t, a, b, k, res;
sieve();
cin >> t;
while(t--)
{
cin >> a >> b >> k;
res = 0;
res = dp[b][k] - dp[a - 1][k];
cout << res << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100000;
int div1[N + 1];
int dp[N + 1][6];
void sieve()
{
memset(div1, 0, sizeof div1);
for(int i = 2; i <= N; i++)
{
if(div1[i] == 0)
{
for(int j = i; j <= N; j += i)
{
div1[j]++;
}
}
}
// precompute number of k-prime before i
for(int i = 2; i <= N; i++)
{
for(int k = 1; k <= 5; k++)
{
dp[i][k] = dp[i - 1][k] + (div1[i] == k?1:0);
}
}
}
int main()
{
int t, a, b, k, res;
sieve();
cin >> t;
while(t--)
{
cin >> a >> b >> k;
res = 0;
res = dp[b][k] - dp[a - 1][k];
cout << res << endl;
}
return 0;
}
Comments
Post a Comment