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(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;
}
原创文章 151 获赞 134 访问量 6858

猜你喜欢

转载自blog.csdn.net/weixin_45750972/article/details/105788891