• 同余方程(codevs 1200)


    题目描述 Description

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

    输入描述 Input Description

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

    输出描述 Output Description

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

    样例输入 Sample Input

    3 10 

    样例输出 Sample Output

    7

    数据范围及提示 Data Size & Hint

    【数据范围】
    对于 40%  的数据, 2 ≤b≤ 1,000 ;
    对于 60% 的数据, 2 ≤b≤ 50,000,000 
    对于 100%  的数据, 2 ≤a, b≤ 2,000,000,000

    /*
      ax=b(mod c)  =>  ax-c=bn  =>  ax+by=c
      扩展欧几里得
    */
    #include<cstdio> 
    #include<iostream>
    using namespace std;
    int e_gcd(int a,int b,int &x,int &y){
        if(b==0){
            x=1;y=0;
            return a;
        }
        int r=e_gcd(b,a%b,x,y);
        int t=x;
        x=y;
        y=t-a/b*y;
        return r;
    }
    int main(){
        int a,b,x,y;
        cin>>a>>b;
        int gcd=e_gcd(a,b,x,y);
        x=x*1/gcd;
        if(x>0){
            while(x-(b/gcd)>0)x-=(b/gcd);
        }
        else{
            while(x+(b/gcd)<0)x+=gcd;
            x+=(b/gcd);
        }
        cout<<x;
        return 0;
    }    
  • 相关阅读:
    SQL Server 查看物理页存储
    Sql Server 孤立用户解决办法
    大文件下载
    UITableViewCell
    xib文件的使用
    UIToolbar
    UIView常见的属性和方法
    iOS的三种多线程技术:
    网络处理文件上传、获取文件MIMEType、其他HTTP请求方式
    转场动画过渡效果
  • 原文地址:https://www.cnblogs.com/harden/p/5644817.html
Copyright © 2020-2023  润新知