I am using the idea, x < b and when x = r * lcm + [0, b — 1] ==> (x % a) % b = (x % a) % a So, I am using this idea to solve the problem, but my code is failing I don't know why it is missing some cases please help me with this Problem link, Submission Link
#include <iostream>
using namespace std;
#define ws ' '
#define ll long long
ll gcd(ll a, ll b) {
if (b > a) swap(a, b);
while (b != 0) {
ll temp = b;
b = a % b;
a = temp;
}
return a;
}
ll f(ll _lcm, ll b, ll r) {
ll ans = min(r, b - 1);
ll d = (r - b + 1) / _lcm;
if (d != 0) {
ans += d * b;
if ((d + 1) * _lcm <= r) ans += (r - (d + 1) * _lcm + 1);
}
return ans;
}
void solve() {
ll a, b, q;
cin >> a >> b >> q;
ll _lcm = (a * b) / (gcd(b, a));
while (q--) {
ll l, r;
cin >> l >> r;
ll ans = r - l + 1;
ans -= f(_lcm, b, r);
if (l > 0) ans += f(_lcm, b, l - 1);
cout << ans << ws;
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false); cin.tie(__null);
int t = 1;
cin >> t;
for (int _ = 0; _ < t; _++) solve();
return 0;
}
I have changed the code a bit but, this is also not working
Submission