题意
给定两个数a、b,有q次查询,每次给出l、r,问在l、r之间有多少个数满足x%a%b!=x%b%a
题解
不考虑l、r,满足 k*(ab)/gcd(a,b) ≤ x < k(a*b)/gcd(a,b) + max(a,b)的就是使上式相等的数,根据这个式子容易知道具有周期性,构造前缀和,用‘/’‘%’来构造答案就好(这个题在做的时候真的是蠢到家,打表都打出周期性来了却直接忽略了这一点现场瞎搞)
#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#define _for(i, a, b) for(int i = a; i < b; ++i)
#define _rep(i, a, b) for(int i = a; i <= b; ++i)
#define _dim(i, a, b) for(int i = a; i >= b; --i)
#define closeIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug cout << "******************" << endl
#define FREE freopen("in.txt", "r", stdin)
#define FREO freopen("out.txt", "w", stdout)
#define ls l, m, rt << 1
#define rs m + 1, r, rt << 1 | 1
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef long double LD;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const LD PI = acos(-1);
const LD eps = 1e-10;
int f[MAXN];
LL solve(LL p, int n) {
return f[n - 1] * (p / n) + f[p%n];
}
int main() {
int T; cin >> T;
while (T--) {
int a, b, q;
cin >> a >> b >> q;
int n = a*b;
_rep(i, 1, n) {
f[i] = f[i - 1];
if (i%a%b != i%b%a) ++f[i];
}
LL l, r;
_rep(cs, 1, q) {
cin >> l >> r;
cout << solve(r, n) - solve(l - 1, n) << " \n"[q == cs];
}
}
return 0;
}