C. Yet Another Counting Problem(數論&取模)

C. Yet Another Counting Problem(數論&取模)

題目傳送門

思路:
在這裏插入圖片描述
AC代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,q,c;
ll fun(int 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章