zoukankan      html  css  js  c++  java
  • 【扩展欧几里得】Codevs 1200: [noip2012]同余方程


    Description

      求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解。 

    Input Description

      输入只有一行,包含两个正整数 a, b,用 一个 空格隔开。 

    Output Description

      输出只有一行包含一个正整数x0,即最小正整数解,输入数据保证一定有解。


      裸的exgcd,不多讲了。。
      
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 
     6 typedef long long ll;
     7 
     8 using namespace std;
     9 
    10 ll x,y;
    11 
    12 void exgcd(ll a,ll b)
    13 {
    14     if(b==0)
    15     {
    16         x=1;
    17         y=0;
    18         return;
    19     }
    20     exgcd(b,a%b);
    21     ll 
    22     sb=x;
    23     x=y;
    24     y=sb-(a/b*y);
    25 }
    26 
    27 void solve(ll a,ll b)
    28 {
    29     exgcd(a,b);
    30     printf("%lld",(x+b)%b);//最小正整数解
    31 }
    32 
    33 int main()
    34 {
    35     ll a,b;
    36     scanf("%lld%lld",&a,&b);
    37     solve(a,b);
    38     return 0;
    39 }
    View Code
  • 相关阅读:
    apache 错误日志
    搭建服务器
    vim配置
    临时表增加查询速度
    如何清空$_POST or $_GET
    hdu 2084
    快速幂
    zjut 1176
    rwkj 1091
    zjut 1090 --------同余定理的应用
  • 原文地址:https://www.cnblogs.com/tuigou/p/4628349.html
Copyright © 2011-2022 走看看