678C - Joty and Chocolate - CodeForces Solution


implementation math number theory *1600

Please click on ads to support us..

Python Code:

import math, sys
input = lambda: sys.stdin.readline().strip()

def lcm(a, b):
    return a * b // math.gcd(a, b)

n, a, b, p, q = [int(x) for x in input().split()]
num_div_a = n // a
num_div_b = n // b
num_div_ab = n // (lcm(a, b))
print((num_div_a - num_div_ab) * p + (num_div_b - num_div_ab) * q + num_div_ab * max(p, q))

C++ Code:


#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265359
#define dmin 1e-9
#define dd double
#define ll long long
#define pq priority_queue
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define in insert
#define vpi vector<pair<int,int>>
#define fori for(int i=0;i<n;i++)
#define srt v.begin(),v.end()
#define all(v) v.begin(),v.end()
#define nl "\n"
#define fast  ios_base::sync_with_stdio(false),cin.tie(NULL);


void mdmerazulislam()
{

}

int main()
{
    fast;
    
    ll n,a,b,p,q;
    cin>>n>>a>>b>>p>>q;
    
    ll ans=0;
    ans += ((n/a) *p);
    ans += ((n/b) *q);

    ll gcd = __gcd(a,b);
    ll lcm = (a/(gcd)*b);

    ll same ;
    same= n/lcm;

    ans -= (same*(p+q));
    ans +=(same*(max(p,q)));
    cout<<ans<<endl;

    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers