P1516 青蛙的约会 exgcd

Link

思路:

初始位置x, y, 每次跳跃距离x, y, 圈长l,即解方程(n − m) * t ≡ x − y (mod l),即(n − m)t+kl=x−y,设a = n - m, b = l, c = x - y,

即求at + bk = c 的t最小整数解。

设g = gcd(a, b),  可求出at + bk = g的t的特解t0

由x = (x % (b / d) + b / d) % (b / d);找到最小整数解,即为答案

注意如果a小于0,即n-m<0,则要把等式左右都取相反数,即a=-1,c=-c

// Decline is inevitable,
// Romance will last forever.
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define LL long long
#define int long long
#define endl '\n'
const int maxn = 3e2 + 10;
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, -1, 1}; //{0, 0, 1, 1, 1,-1,-1,-1}
const int dy[] = {1, -1, 0, 0}; //{1,-1, 1, 0,-1, 1, 0,-1}
const int P = 998244353;
int a, b, c;
int d, x, y, m, n, l;
int t;
int exgcd(int a, int b, int &x, int &y) {
    if(!b) {x = 1; y = 0; return a;}
    int d =exgcd(b, a%b, x, y);
    int z = x; x = y; y = z - y * (a / b);
    return d;
}
void solve() {
    cin >> x >> y >> m >> n >> l;
    a = n - m; b = l; c = x - y;
    if(a < 0) a=-a, c=-c;
    int g = exgcd(a, b, t, l);
    if(c%g) {
        cout << "Impossible\n";
        return;
    }
    t = (c/g*t%(b/g)+b/g) % (b/g);
    cout << t << endl;
    
}
signed main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值