10.19
思路:
exgcd解方程,等差数列求和
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
LL a, b, c;
LL x, y;
LL gcd(LL aa, LL bb, LL &x, LL &y){
if(bb == 0){
x=1; y=0;
return aa;
}
LL d = gcd( bb, aa % bb, y, x);
y -= aa / bb * x;
return d;
}
int main(){
freopen ("pay.in", "r", stdin);
freopen ("pay.out", "w", stdout);
int T, opt; scanf("%d%d", &T, &opt);
while ( T-- ){
LL ans1 = 0, ans2 = 0;
scanf("%I64d%I64d%I64d", &a, &b, &c);
LL d = gcd(a, b, x, y);
if( c % d != 0 ){
}
else{
x = x * c / d; y = y * c / d;
LL x0, x1, y0, y1;
x0 = ((x % abs(b / d)) + abs(b / d)) % abs(b / d);
y0 = (c - a * x0) / b;
y1 = ((y % abs(a / d)) + abs(a / d)) % abs(a / d);
x1 = (c - b * y1) / a;
ans1 = (x1 - x0) / abs(b / d) + 1;
ans2 = (x0 + x1) * ans1 / 2 + (y0 + y1) * ans1 / 2;
}
printf("%I64d ", ans1);
if(opt == 2) printf("%I64d", ans2);
printf("\n");
}
return 0;
}