给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数。
提示:
对于第二组测试数据,集合分别是:{1,10},{2,9},{3,8},{4,7},{5,6},{6,5},{7,4},{8,3},{9,2},{10,1}.满足条件的是第2个和第8个。
第2 - T+1行:每行三个整数N,A,B(1<=N,A,B<=2147483647)
2 5 2 4 10 2 3
1 2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
long long t;
long long n, a, b;
long long gcd(long long x, long long y)
{
return y == 0 ? x : gcd(y, x%y);
}
long long extgcd(long long a, long long b, long long &x, long long &y)
{
if (b == 0)
{
x = 1;y = 0;
return a;
}
long long r = extgcd(b, a%b, x, y);
long long t = x;
x = y;
y = t - a / b*y;
return r;
}
int main()
{
scanf("%lld", &t);
while (t--)
{
scanf("%lld %lld %lld", &n, &a, &b);
long long x0, y0;
n++;
long long ans = gcd(a,b);
long long res = 0;
if (n%ans == 0)
{
long long a1 = a / ans;
long long b1 = b / ans;
long long n1 = n / ans;
extgcd(a1, b1, x0, y0);
x0 *= n1;
y0 *= n1;
if (x0 <= 1)
{
long long aaa = (1 - x0) % b1;
x0 = 1-aaa + (aaa == 0 ? 0 : b1);
}
else
x0 = 1 + (x0-1)%b1;
long long end = n / a - (n%a == 0 ? 1 : 0);
if(x0<=end)
res = 1+ (end - x0) / b1;
}
printf("%lld\n", res);
}
return 0;
}