You are given two integers aa and bb , and qq queries. The i -th query consists of two numbers li and ri , and the answer to it is the number of integers x such that li≤x≤ri , and ((xmoda)modb)≠((xmodb)moda). Calculate the answer for each query.
Recall that ymodz is the remainder of the division of y by z . For example, 5mod3=2 , 7mod8=7 , 9mod4=1 , 9mod9=0 .
The first line contains one integer t (1≤t≤100 ) — the number of test cases. Then the test cases follow.
The first line of each test case contains three integers aa , bb and qq (1≤a,b≤200 ; 1≤q≤500 ).
Then q lines follow, each containing two integers lili and riri (1≤li≤ri≤1018 ) for the corresponding query.
For each test case, print q integers — the answers to the queries of this test case in the order they appear.
2 4 6 5 1 1 1 3 1 5 1 7 1 9 7 10 2 7 8 100 200
0 0 0 2 4 0 91
#include <bits/stdc++.h> using namespace std; #define ll long long const int mod=1e9+7; const int N=4e4+5; int cnt[N]; int main(){ ios::sync_with_stdio(0); ll t,a,b,q,l,r; cin>>t; while(t--){ cin>>a>>b>>q; ll n=a*b/__gcd(a,b); for(ll i=0;i<n;i++){ cnt[i+1]=cnt[i]; if(i%a%b!=i%b%a) cnt[i+1]++; } //cout<<cnt[n]<<endl; while(q--){ cin>>l>>r; r++; cout<<(r/n-l/n)*cnt[n]-cnt[l%n]+cnt[r%n]<<' '; } cout<<endl; } return 0; }