zoukankan      html  css  js  c++  java
  • 解模线性方程ax=b(modn)

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 using namespace std;
     5 typedef long long LL;
     6 // 求x和y使得ax+by=d并且|x|+|y|最小。其中d=gcd(a,b)
     7 void exgcd(LL a,LL b,LL& d,LL& x,LL& y){
     8     if(!b) d = a,x = 1,y = 0;
     9     else{
    10         exgcd(b,a % b,d,y,x);
    11         y -= x * (a / b);
    12     }
    13 }
    14 // 用扩展欧几里得解模线性方程 ax=b(mod n)
    15 bool modular_Linear_Equation(LL a,LL b,LL n){
    16     LL x,y,x0,i,d;
    17     exgcd(a,n,d,x,y);
    18     if(b % d) return false; //d不是b的约数时没有解
    19         x0 = x * (b / d) % n; // 得到一个特解
    20     for(i = 1 ; i <= d ; i++)
    21        printf("%d
    ",(x0 + i * (n / d)) % n); // 恰好有d个解
    22     return true;
    23 }
    24 int main(){
    25     modular_Linear_Equation(3,6,6);
    26     return 0;
    27 }
  • 相关阅读:
    mybatis
    队列
    JDK中的Timer和TimerTask详解
    NIOGoodDemo
    24-C#笔记-异常处理
    23-C#笔记-正则表达式
    22-C#笔记-预编译指令
    21-C#笔记-名称空间
    20-C#笔记-接口
    19-C#笔记-多态性
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5807608.html
Copyright © 2011-2022 走看看