#include
#include
__int64 min(__int64 a,__int64 b) {
return a<b?a:b;
}
__int64 gcd(__int64 a,__int64 b) {
if(b==0)
return a;
return gcd(b,a%b);
}
__int64 count(__int64 n,__int64 a,__int64 b) {
__int64 now=0,ret=0,cmp,x=0,y=0,ans=0;
while(now<n) {
cmp=min(a-x,b-y);
if(now+cmp>n)
cmp=n-now;
ans+=cmp*abs(x-y);
x=(x+cmp)%a;
y=(y+cmp)%b;
now+=cmp;
}
return ans;
}
int main() {
__int64 i,j,l,x,a,n,b,y,t;
scanf("%I64d",&t);
while(t--) {
scanf("%I64d%I64d%I64d",&n,&a,&b);
l=a*b/gcd(a,b);
if(l>=n)
printf("%I64d\n",count(n,a,b));
else
printf("%I64d\n",count(l,a,b)*(n/l)+count(n%l,a,b));
}
return 0;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章