C. Yet Another Counting Problem 转载自:那里是真大佬,代码比我短多了 思路: 反思 小兄弟(博主自嘲)想什么呢,明明f(r)-f(l-1)无论怎样都会把0剪掉,却想不明白,wa2次,qwq。 AC(我看大佬码的,本人的太丑了,就不放了) #include <iostream> #define For(i,x,y) for(register int i=(x); i<=(y); i++) using namespace std; typedef long long ll; ll a,b,q; ll lcm; ll gcd(ll a, ll b) { if(b==0)return a; return gcd(b,a%b); } ll f(ll x) { ll cnt=x/lcm; ll re=x%lcm+1; return x-cnt*a-min(re,a); } int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t; cin>>t; while(t--) { cin>>a>>b>>q; if(a<=b)swap(a,b); lcm=a/gcd(a,b)*b; For(i,1,q) { ll l,r; cin>>l>>r; cout<<f(r)-f(l-1)<<endl; } } return 0; }