HDU

来源:互联网 发布:阿里云服务器配置推荐 编辑:程序博客网 时间:2024/06/05 03:53

求逆元的写法

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <set>#include <map>#include <stack>#include <queue>#include <ctype.h>#include <vector>#include <algorithm>#include <sstream>#define PI acos(-1.0)#define in freopen("in.txt", "r", stdin)#define out freopen("out.txt", "w", stdout)using namespace std;typedef long long ll;const int maxn = 500 + 7, INF = 0x3f3f3f3f;const ll mod = 9973;int T;ll b, n;void exgcd(ll a, ll b, ll &d, ll &x, ll &y) {    if(b == 0) { d = a; x = 1; y = 0; }    else {        exgcd(b, a%b, d, y, x);        y -= (a/b)*x;    }}int main() {    scanf("%d", &T);    while(T--) {        scanf("%lld%lld", &n, &b);        ll x, y, d;        exgcd(b, mod, d, x, y);        printf("%lld\n", (n*x%mod+mod)%mod )    ;    }    return 0;}


原创粉丝点击