• 【hdu 1576】A/B(数论--拓展欧几里德 求逆元 模版题)


    题意:给出 A%9973 和 B,求(A/B)%9973的值。

    解法:拓展欧几里德求逆元。由于同余的性质只有在 * 和 + 的情况下一直成立,我们要把 /B 转化为 *B-1,也就是求逆元。

       对于 B-1,P为模数9973,那么 B*B-1=1(mod P)  →  把 B-1 看成 x ,就是 Bx+Py=1。也就是求不定方程的解了。x 就是 B-1,答案就是 ((A%9973)*(x%9973))%9973 。

    P.S.关于拓展欧几里德求解不定方程的具体解释请见——【poj 2115】C Looooops(数论--拓展欧几里德 求解同余方程 模版题)

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<cstring>
     4 #include<iostream>
     5 using namespace std;
     6 #define N 10000
     7 #define B (int)1e9+10
     8 #define mod 9973
     9 typedef long long LL;
    10 
    11 LL mabs(LL x) {return x>0?x:-x;}
    12 LL exgcd(LL a,LL b,LL& x,LL& y)
    13 {
    14     if (!b) {x=1,y=0; return a;}
    15     LL d,tx,ty;
    16     d=exgcd(b,a%b,tx,ty);
    17     x=ty,y=tx-(a/b)*ty;
    18     return d;
    19 }
    20 int main()
    21 {
    22     int T; LL n,m;
    23     scanf("%d",&T);
    24     while (T--)
    25     {
    26       scanf("%lld%lld",&n,&m);
    27       LL d,x,y,t;
    28       d=exgcd(m,mod,x,y);
    29       x%=mod;//其实,若d!=1,就无解了。
    30       
    31       t=mabs(mod/d);
    32       x=(x%t+t)%t;//最小非负整数解
    33       
    34       printf("%lld
    ",(n*x)%mod);
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    507.Perfect Number
    441.Arranging Coins
    344.Reverse String
    160.Intersection of Two Linked Lists
    HDU-2521 反素数
    HDU-2710 Max Factor
    HDU-2552 三足鼎立
    HDU-2549 壮志难酬
    HDU-2548 两军交锋
    HDU-2550 百步穿杨
  • 原文地址:https://www.cnblogs.com/konjak/p/6067427.html
Copyright © 2020-2023  润新知