table of Contents
@description@
2 are given integers A, B. You can use any number of times. And then give a period from the X - Y interval T, optionally a plurality of A, B do addition, the interval T may be covered in many different integers.
For example: A = 8, B = 11 , the interval T is 3--20. In 3--20, the integer 8 (8), 11 (11), 16 (8 + 8), 19 (8 + 11). It may be covered in a number of section S, the output 4.
@solution@
First into A, B in [0 ... r] minus the cover A, B in [0 ... l-1] number of coverage. The question becomes seeking A, B is the number of [0 ... N] coverage.
Then A, B, N at the same time by dividing gcd (A, B), but may not be divisible by N, this time rounded down.
When A, B coprime, a certain number X may be expressed as \ (P \ Q + A Times \ Times B \) , where \ (0 \ Leq P <B \) .
Therefore, we enumerate p, to give [0 ... N] number may be the number of covered:
\ [\ sum_ {P ^ = {0}. 1-B and p \ times A \ leq N} (\ lfloor \ frac {N - p \ times A} {B} \ rfloor + 1) \]
This is a classic class Euclid. We can do directly.
@accepted code@
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll x, ll y) {return (y == 0 ? x : gcd(y, x % y));}
ll func(ll n, ll a, ll b, ll c) {
if( a >= c ) return func(n, a % c, b, c) + (a/c)*n*(n + 1)/2;
if( b >= c ) return func(n, a, b % c, c) + (b/c)*(n + 1);
ll m = (a * n + b) / c;
if( a == 0 || m == 0 ) return 0;
return n*m - func(m - 1, c, c - b - 1, a);
}
ll get(ll A, ll B, ll N) {
ll p = min(N / A, B - 1);
return func(p, A, N - p*A, B) + p;
}
void solve() {
ll A, B, X, Y; scanf("%lld%lld%lld%lld", &A, &B, &X, &Y), X--;
ll d = gcd(A, B); A /= d, B /= d, X /= d, Y /= d;
printf("%lld\n", get(A, B, Y) - get(A, B, X));
}
int main() {
int T; scanf("%d", &T);
while( T-- ) solve();
}
@details@
About A, B coverage during prime to - think of "Oscar doubts."
Basically be a board like the European title bar.