Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created ARRFILL.cpp #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ARRFILL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
codechef problem link : https://www.codechef.com/problems/ARRFILL
codechef profile link : https://www.codechef.com/users/santosh_01
*/

#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

int T;
cin >> T;
while(T--) {
long long int n, m;
cin>>n>>m;
vector<pair<int,int>>vp;
for (int i=0; i < m; i++)
{
long long x, y;
cin>>x>>y;
vp.push_back({x,y});
}
sort(vp.begin(), vp.end(), greater<pair<int,int>>());

long long int lcm=1;
long long int rem=n;
long long res=0;

for (int i=0; i<m && rem>0; i++){
long long int a= vp[i].first,b=vp[i].second;
lcm = lcm*b/__gcd(lcm,b);
res += (rem-n/lcm)*a;
rem= n/lcm;
}
cout <<res<<endl;
}
return 0;
}