《牛客2021年儿童节比赛D》

同余方程没推好。。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,set<int>> pii;
const int N = 1e5 + 5;
const int M = 1e5 + 5;
const LL Mod = 998244353;
#define pi acos(-1)
#define INF 1e9
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline int read(){
        int x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
}
using namespace FASTIO;

LL exgcd(LL a,LL b,LL &x,LL &y){
    if(b == 0)
    {
        x = 1,y = 0;
        return a;
    }
    int t = exgcd(b,a%b,y,x);
    y -= a/b*x;
    return t;
}
int n,a,b;
int main() {
    n = read(),a = read(),b = read();
    LL gcd = __gcd(a,b);
    LL p,k;
    LL ta = exgcd(gcd,n,p,k);
    if((n - 1) % ta != 0) printf("-1\n");
    else {
        LL x,y;
        LL g = exgcd(a,b,x,y);
        p =  p * (n - 1) / ta;
        x = x * p;
        y = y * p;
        if(x < 0) x = x + 1LL * (abs(x / n) + 1) * n;
        if(y < 0) y = y + 1LL * (abs(y / n) + 1) * n;
        printf("%lld %lld\n",x,y);
    }
    //system("pause");
    return 0;
}
View Code

 

posted @ 2021-06-05 16:53  levill  阅读(38)  评论(0编辑  收藏  举报