数论-GCD、LCM、扩展欧几里得

时间:2022-07-25
本文章向大家介绍数论-GCD、LCM、扩展欧几里得,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

文章目录

  • 最大公约数GCD
  • 最小公倍数LCM
  • 扩展欧几里得
  • 例题
    • HDU-5223
    • HDU-1576

最大公约数GCD


欧几里得算法(辗转相除法)求GCD

int gcd(int x, int y) {
    return y == 0 ? x : gcd(y, x % y);
}

最小公倍数LCM


int lcm(int x, int y) {
    return x / gcd(x, y) * y;
}

扩展欧几里得


ll extend_gcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    }
    ll gcd = extend_gcd(b, a % b, y, x);
    y -= x * (a / b);
    return gcd;
}
  1. 求解不定方程
  2. 求解摸的逆元
  3. 求解同余方程

例题


HDU-5223

HDU-5223 GCD

Problem Description In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.—Wikipedia BrotherK and Ery like playing mathematic games. Today, they are playing a game with GCD. BrotherK has an array A with N elements: A1 ~ AN, each element is a integer in [1, 10^9]. Ery has Q questions, the i-th question is to calculate GCD(ALi, ALi+1, ALi+2, …, ARi), and BrotherK will tell her the answer. BrotherK feels tired after he has answered Q questions, so Ery can only play with herself, but she don’t know any elements in array A. Fortunately, Ery remembered all her questions and BrotherK’s answer, now she wants to recovery the array A. Input The first line contains a single integer T, indicating the number of test cases. Each test case begins with two integers N, Q, indicating the number of array A, and the number of Ery’s questions. Following Q lines, each line contains three integers Li, Ri and Ansi, describing the question and BrotherK’s answer. T is about 10 2 ≤ N Q ≤ 1000 1 ≤ Li < Ri ≤ N 1 ≤ Ansi ≤ 109 Output For each test, print one line. If Ery can’t find any array satisfy all her question and BrotherK’s answer, print “Stupid BrotherK!” (without quotation marks). Otherwise, print N integer, i-th integer is Ai. If there are many solutions, you should print the one with minimal sum of elements. If there are still many solutions, print any of them. Sample Input 2 2 2 1 2 1 1 2 2 2 1 1 2 2 Sample Output Stupid BrotherK! 2 2

给定若干区间的GCD,试还原原数组。 贪心乘最小的数使得区间内每个数是ans[i]的倍数(LCM),最后再检查一遍。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1003;
ll t, n, q;
ll a[maxn], l[maxn], r[maxn], ans[maxn];
ll gcd(ll x, ll y) {
    return y == 0 ? x : gcd(y, x % y);
}
ll lcm(ll x, ll y) {
    return x / gcd(x, y) * y;
}
int main() {
    cin >> t;
    while (t--) {
        cin >> n >> q;
        for (int i = 1; i <= n; i++)a[i] = 1; //初始化原数组1
        for (int i = 1; i <= q; i++) {
            cin >> l[i] >> r[i] >> ans[i];
            for (int j = l[i]; j <= r[i]; j++)
                a[j] = lcm(a[j], ans[i]);
        }
        bool tag = true;
        for (int i = 1; i <= q; i++) {  //检验
        
            ll tmp = a[l[i]];
            for (ll j = l[i] + 1; j <= r[i]; j++)
                tmp = gcd(tmp, a[j]);
            if (tmp != ans[i]) {
                tag = false;
                break;
            }
        }
        if (tag) {
            cout << a[1];
            for (int i = 2; i <= n; i++)cout << " " << a[i];
            cout << "n";
        }
        else cout << "Stupid BrotherK!n";
    }
    return 0;
}

HDU-1576

HDU-1576 A/B

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

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 9973
ll t, n, a, b, x, y;
ll extend_gcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    }
    ll gcd = extend_gcd(b, a % b, y, x);
    y -= x * (a / b);
    return gcd;
}
int main() {
    cin >> t;
    while (t--) {
        cin >> n >> b;
        extend_gcd(b, mod, x, y);
        x *= n;
        x = (x % mod + mod) % mod;
        cout << x << "n";
    }
    return 0;
}

原创不易,请勿转载本不富裕的访问量雪上加霜 ) 博主首页:https://blog.csdn.net/qq_45034708