Sumit's love for maths

Sumit is doing research in mathematics. After doing lots of research, he struck a problem. He found four numbers n, a, b and c. Now, he wants to know how many numbers exist which are less than or equal to n and are divisible by a,b or c.

Example:

Input:  n = 15, a = 2, b  = 3, c = 5
Output: 11

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long gcd(long long along long b)
{
    if (b == 0)
        return a;
    return gcd(ba % b);
}
long long lcm(long long along long b)
{
    return (a * b) / gcd(ab);
}

long long count(long long nlong long a
long long blong long c)
{
    long long ans = n / a + n / b + n / c;
    long long x1 = n / lcm(ab);
    long long x2 = n / lcm(bc);
    long long x3 = n / lcm(ac);
    long long x4 = n / lcm(alcm(bc));
    ans = ans - x1 - x2 - x3 + x4;
    return ans;
}
int main()
{

    long long n = 15a = 2b = 3c = 5;

    cout << count(nabc<< "\n";

    return 0;
}


No comments:

Post a Comment