C. Yet Another Counting Problem(数论&取模)

题目传送门

思路:

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,q,c;
ll fun(ll x){
	 ll cnt=x/c;
	 ll re=x%c+1;
	 return x-cnt*b-min(re,b);
}
int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>a>>b>>q;
		if(a>b) swap(a,b);
		 c=a/__gcd(a,b)*b;
		while(q--){
			ll l,r;
			cin>>l>>r;
			printf("%lld\n",fun(r)-fun(l-1));
		}
	}
	return 0;
}