题意: 问你 l 到 r 这个区间内有多少个数组满足 ((x % a) % b) ≠ ((x % b) % a),总共t组测试,每组测试询问q次l到r的答案。
思路:
- 设x = (m * a) + c = (n * b) + d ,若((x % a) % b) = ((x % b) % a),那么就意味着c和d相等。
- 又因为 m 和 n 都是整数,所以当x = m * lcm(a,b)+ c,(c就是max(a, b) - 1)。求出不符合的数,然后用总数减去它就是答案了。
代码实现:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;
int t, a, b, q, x, y;
int maxx, lc, _x, _y, tmp;
vector<int> ans;
signed main()
{
IOS;
cin >> t;
while(t --){
cin >> a >> b >> q;
ans.clear();
maxx = max(a, b);
lc = (a * b) / __gcd(a, b); //求得a,b的最小公倍数
while(q --){
cin >> x >> y;
_x = (x - 1) / lc * maxx; // 1 ~ (x-1)中不符合的个数
tmp = (x - 1) % lc;
if(tmp >= maxx) _x += maxx;
else _x += tmp + 1;
_y = y / lc * maxx; //1 ~ y中不符合的个数
tmp = y % lc;
if(tmp >= maxx) _y += maxx;
else _y += tmp + 1;
ans.push_back((y - x + 1) - (_y - _x));//可选总数减去x ~ y中不满足的数
}
for(auto it : ans) cout << it << " ";
cout << endl;
}
return 0;
}