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

    对于模板的应用。

    题目描述

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

    输入格式

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

    数据规模:

    对于40%的数据,2≤b≤1,000;

    对于60%的数据,2≤b≤50,000,000;

    对于100%的数据,2≤a, b≤2,000,000,000。

    输出

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

    样例输入

    3 10

    样例输出

    7

     1 #include <stdio.h>
     2 
     3 void gcd(int a,int b,int& d,int& x,int& y){
     4     if(!b){
     5         d = a; x = 1; y = 0;
     6     }else{
     7         gcd(b, a % b, d, y, x);
     8         y -= x * (a / b);
     9     }
    10 }
    11 
    12 int linear_mod_equation(int a,int b,int n,int& d){
    13     // d = gcd(a,n)
    14     int x,y;
    15     gcd(a,n,d,x,y);
    16     if(b % d) d = 0;
    17     else{
    18         return x * (b / d) % n;
    19     }
    20 }
    21 
    22 int sol[1000];
    23 int main(void){
    24     int a,n,b = 1,d,ans;
    25     scanf("%d%d",&a,&n);
    26     ans = linear_mod_equation(a,b,n,d);
    27     while(ans < 0){
    28         ans += n/d;
    29     }
    30     printf("%d
    ",ans);
    31     return  0;
    32 }
  • 相关阅读:
    C++ 编码转换
    获取文件扩展名
    字符串分割(C++)(转载)
    Visual Leak Detector简明使用教程
    Win32 文件拖拽
    IMAP协议命令(详细)
    CreateDirectory 创建文件夹 CC++
    编程习惯总结
    GitHub上整理的一些工具,求补充
    jquery生成qrcode二维码
  • 原文地址:https://www.cnblogs.com/yfs123456/p/5535117.html
Copyright © 2011-2022 走看看