AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 校园
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

AcWing 1298. 曹冲养猪

作者: 作者的头像   只想白嫖 ,  2022-01-15 15:46:47 ,  所有人可见 ,  阅读 7


0


/*
author: A Fei
solution: 同余方程组
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
int a[11], m[11];
int n;

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)
    {
        x = 1, y = 0;
        return a;
    }

    LL d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
int main()
{
    scanf("%d", &n);

    LL M = 1;
    for(int i = 0; i < n; i ++) scanf("%d%d", &m[i], &a[i]), M *= m[i];

    LL res = 0;
    for(int i = 0; i < n; i ++)
    {
        LL mi = M/m[i];
        LL t, y;
        exgcd(mi, (LL)m[i], t, y);
        // t = (t % m[i] + m[i]) % m[i];
        res += (LL)a[i] * t * mi;
    }

    printf("%lld\n", (res % M + M) % M);//对于任意x+kM都是满足要求的解,但目标是输出最小的正整数x,因此取模即可

    return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息