扩展欧几里得求逆元:
鉴定为easy
记得求最小正整数解时不能为 0
#define _CRT_SECURE_NO_WARNINGS 1 #pragma GCC target ("avx") #pragma GCC optimize (2, 3, "Ofast", "inline", "-ffast-math") #include <bits/stdc++.h> using namespace std; #define int long long typedef long long ll; const int INF = 1e18; typedef pair<int,int> PII; const int maxn = 2005, N = 1e5 + 5,mod = 998244353; int exgcd(int a,int b,int &x,int &y) { if (!b) { x = 1, y = 0; return a; } int d = exgcd(b, a % b, y, x); y -= a / b * x; return d; } inline void solve() { int s, t; cin >> s >> t; int a, b, x, y; a = s, b = t; int gcd = exgcd(a, b, x, y); gcd = abs(gcd); if (gcd == 1) { cout << ((x % b + b) % b ? (x % b + b) % b : b) << endl; } else cout << "Not Exist\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while(cin >> t){ while (t--) solve(); } return 0; }
TZOJ 4295: Modular Inverse 扩欧模板
最新推荐文章于 2024-07-25 12:55:52 发布