math number theory *1600

Please click on ads to support us..

Python Code:

global l
global p
def build(a,b):
    global l,p
    l=a*b
    p=[0]*l
    p[0]=0
    for i in range(1,l):
        p[i]=p[i-1]
        if ((i%a)%b)!=((i%b)%a):
            p[i]+=1
def query(x):
    cnt=x//l
    rem=x%l
        return p[-1]*cnt+p[rem]


for _ in range(int(input())):
    a,b,q=map(int,input().split())
    build(a,b)
    for _ in range(q):
        q,r=map(int,input().split())
        print(query(r)-query(q-1),end=" ")
    print()
    

C++ Code:

// https://codeforces.com/problemset/problem/1342/C
#include<bits/stdc++.h>
/*
Gurmeet Singh
MAIT
2024
*/
using namespace std;
#define int long long int
#define mod (int)(1e9+7)
int findcount(int r,int lcm,int b)
{
    int div=r/lcm;
    int prod=div*lcm;
    int total=r;
    if(div!=0)
    {
        total-=min(r-prod+1,b); 
        total-=(div-1)*b;
    }
    return total-min(r,(b-1));
}
void solve()
{
    int a,b,q;
    cin>>a>>b>>q;
    if(a>b) swap(a,b);
    int lcm=(a*b)/gcd(a,b);
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        if(lcm==b || (r<b)) {
            cout<<0<<" ";
            continue;
        }
        int cnt=findcount(r,lcm,b);
        if(l>1) cnt-=findcount(l-1,lcm,b);
        cout<<cnt<<" ";
    }
    cout<<"\n";
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
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