ABC186E Throne 扩展gcd

传送门

题意:

扩展gcd经典题目青蛙的约会换皮,注意处理负数

代码:

#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
LL a[200005];
LL exgcd(LL &x,LL &y,LL a,LL b) {
    if(!b) {
        x=1;
        y=0;
        return a;
    }
    LL ans=exgcd(x,y,b,a%b);
    LL t=x;
    x=y;
    y=t-a/b*y;
    return ans;
}
inline LL mod_force_postive(LL a,LL m){
    return (a%m+m)%m;
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        LL n,s,k;
        //n num s start k step
        LL x,y;
        scanf("%lld %lld %lld",&n,&s,&k);
        LL len=n-s;
        LL g=exgcd(x,y,k,n);
        if(len%g==0){
            LL ans=mod_force_postive(len/g*x,n/g);
            printf("%lld\n",ans);
        }else{
            printf("%d\n",-1);
        }
    }
    return 0;
}

 

posted @ 2020-12-20 12:59  Isakovsky  阅读(114)  评论(0编辑  收藏  举报