Mogu Loves Numbers

Mogu loves those numbers that have the following property:-

1.The number of divisors of the number is even.

2.The number of divisors of the number is prime.

Given L and R, Mogu needs to find the count of numbers between L and R inclusive that follow the above properties.

As Mogu was unable to make it to the IUPC round 1 in Plinth '17, he has asked you to report the answer back to him.

Note: Both the properties should hold.

Example:

Input:  l = 2, r = 3
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;
bool prime[1000001];

void primeInitialize()
{
    memset(primetruesizeof(prime));
    prime[0] = false;
    prime[1] = false;
    for (long long i = 2i * i <= 100000i++)
    {
        if (prime[i])
        {
            for (long long j = i * ij <= 100000j += i)
                prime[j] = false;
        }
    }
}
int main()
{

    primeInitialize();
    long long ans[100001];
    ans[0] = 0;
    ans[1] = 0;
    long long d = 0;
    for (long long i = 1i <= 100000i++)
    {
        if (prime[i])
            d++;
        ans[i] = d;
    }

    long long l = 2r = 3;

    long long cnt = 0;
    long long min1 = min(lr);
    long long max1 = max(lr);
    cnt = ans[max1] - ans[min1 - 1];
    cout << cnt << "\n";

    return 0;
}


No comments:

Post a Comment