zoukankan      html  css  js  c++  java
  • 不定方程与扩展欧几里得

    时间不多了,先把代码放上来。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 
     5 using namespace std;
     6 
     7 typedef long long ll;
     8 
     9 ll gcd(ll a, ll b)
    10 {
    11     return (b == 0)?a:(gcd(b, a%b));
    12  } 
    13 
    14 ll exgcd(ll a, ll b, ll &x, ll &y)
    15 {
    16     if(b == 0){
    17         x = 1; y = 0;
    18         return a;
    19     }
    20     ll t = exgcd(b, a%b, y, x);
    21     y -= (a/b)*x;
    22     
    23     return t;
    24  } 
    25 
    26 int main()
    27 {
    28     ll x, y;
    29     ll a, b, m;
    30     cin>>a>>b>>m;
    31     // 有解的充要条件 
    32     if(m%gcd(a, b) == 0) cout<<"Yes
    "; 
    33     ll s = exgcd(a, b, x, y);
    34     x = x*m/s;
    35     y = y*m/s;
    36     for(int t = -100;t<=300;t++){
    37         printf("%lld*(%lld) + %lld*(%lld) = %lld
    ", a, x, b, y, m);
    38         /*
    39         x -= b/s;
    40         y += a/s;
    41         */
    42         // 通解, 可往左右走 
    43         x -= b/s;
    44         y += a/s;
    45     }
    46     
    47     return 0;
    48 }
  • 相关阅读:
    bzoj 1503: [NOI 2004]郁闷的出纳员
    bzoj 1001: [BeiJing2006]狼抓兔子
    bzoj 1005: [HNOI2008]明明的烦恼
    bzoj 1004: [HNOI2008]Cards
    bzoj 1003: [ZJOI2006]物流运输trans
    06day1
    05day2
    04day2
    04day1
    03day2
  • 原文地址:https://www.cnblogs.com/mabeyTang/p/10504603.html
Copyright © 2011-2022 走看看