zoukankan      html  css  js  c++  java
  • 扩展欧几里得--hdu1576

    hdu-1576

    Problem Description

    要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。

    Input

    数据的第一行是一个T,表示有T组数据。
    每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。

    Output

    对应每组数据输出(A/B)%9973。

    Sample Input

    2
    1000 53
    87 123456789
    

    Sample Output

    7922
    6060
    

    扩展欧几里得模板

    扩展欧几里得模板

    由题意得

    [ans=(frac{A}{B})\%9973\ n=A\%9973 \ 得ans*B+9973*x = A = n+9973*y \ frac{ans}{n} *B+9973*(x*B-y) = n \ 因gcd(B, 9973) = 1 \ 即形如ax+by = gcd(a, b) ]

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <iomanip>
    #include <cstdio>
    
    using namespace std;
    typedef long long LL;
    typedef pair<double, double> PDD;
    typedef pair<LL, LL> PLL;
    
    const LL N = 1e6+50;
    const LL MOD = 1e9+7;
    const LL INF = 0x3f3f3f3f;
    
    #define lson l, m, rt>>1
    #define rson m+1, r, rt>>1|1
    
    LL exgcd(LL a, LL b, LL &x, LL &y)
    {
        if(!b)
        {
            x = 1;y = 0;
            return a;
        }
        LL ret = exgcd(b, a%b, y, x);
        y -= x*(a/b);
        return ret;
    }
    
    int main()
    {
        int T;cin >> T;
        while(T--)
        {
            LL n, B;cin >> n >> B;
            LL x, y;
            exgcd(B, 9973, x, y);
            cout << ((x*n)%9973+9973)%9973 << endl;//x可能为负数
        }
        return 0;
    }
    
    
  • 相关阅读:
    解决android.os.NetworkOnMainThreadException
    android 模拟器对应键盘快捷键
    Android上解析Json格式数据
    命令行的由来
    Linux测网速
    cacti
    判断端口是否开放
    Linux中运行c程序,与系统打交道
    python 多线程
    Leetcode 编程训练(转载)
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/11678445.html
Copyright © 2011-2022 走看看