pay 解方程 (exgcd)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值