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
",ans);
        }else{
            printf("%d
",-1);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/isakovsky/p/14163255.html