zoukankan      html  css  js  c++  java
  • P1082 同余方程

    题目描述

    给定(a,b),求最小的正整数(x)满足(a cdot xequiv 1 pmod{b})(a,b le 2 imes 10^9)

    简单口胡

    (ax equiv b pmod{m}) 可变形为(ax + my = b)求解,求到一个解(x_0),其通解即为与(x_0)(dfrac{m}{gcd{a,m}})同余的所有整数。

    将原式变形为解(ax + by = 1),求出来的(x)(b)取膜,即为最小正整数解

    # include <bits/stdc++.h>
    using namespace std;
    
    # define int long long
    
    int a,b;
    int x,y;
    
    int gcd(int x,int y)
    {
        if(x % y == 0) return y;
        else return gcd(y,x % y);
    }
    
    int exgcd(int a,int b,int &x,int &y)
    {
        if(b == 0)
        {
            x = 1,y = 0;
            return a;
        }
        int d = exgcd(b,a % b,x,y);
        int z = x;
        x = y,y = z - y * (a / b);
        return d;
    }
    
    signed main(void)
    {
        scanf("%lld%lld",&a,&b);
        //solve ax + by = 1
        int d = exgcd(a,b,x,y);
        int x0;
        if(x > 0) x0 = x % b;
        else x0 = (x + b * (abs(x) / b + 1)) % b;
        printf("%lld
    ",x0);
        return 0;
    }
    
  • 相关阅读:
    可爱精灵宝贝 DP/爆搜
    那些年留的坑
    吃某种零食ing
    NOIP模拟测试13
    NOIP模拟测试12
    NOIP模拟测试11
    大佬 (数学)
    BZOJ3331 BZOJ2013 压力
    LOJ2586 APIO2018 选圆圈
    BZOJ3398 牡牛和牝牛
  • 原文地址:https://www.cnblogs.com/luyiming123blog/p/14678977.html
Copyright © 2011-2022 走看看