题目描述 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

题意:求逆元

思路:因为b可能不是素数,所以利用扩展欧几里德求解

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
//#define mod 1000000007
int scan()
{
int res = 0 , ch ;
while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
{
if( ch == EOF ) return 1 << 30 ;
}
res = ch - '0' ;
while( ( ch = getchar() ) >= '0' && ch <= '9' )
res = res * 10 + ( ch - '0' ) ;
return res ;
}
char a[1010];
ll flag[30];
ll mod;
void extend_Euclid(ll a, ll b, ll &x, ll &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
extend_Euclid(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
int main()
{
ll x,y,a,m;
cin>>a>>m;
extend_Euclid(a,m,x,y);
cout<<(x%m+m)%m<<endl;
return 0;
}

  

codevs 1200 同余方程 逆元的更多相关文章

  1. 扩展gcd codevs 1200 同余方程

    codevs 1200 同余方程 2012年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 求关 ...

  2. Codevs 1200 同余方程 2012年NOIP全国联赛提高组

    1200 同余方程 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 求关于 x 同余方程 a ...

  3. [NOIP2012提高组] CODEVS 1200 同余方程(扩展欧几里德算法)

    数论题..所有数论对我来说都很恶心..不想再说什么了.. ------------------------------------------------ #include<iostream&g ...

  4. codevs 1200 同余方程 2012年NOIP全国联赛提高组 x

    /*我在提交的时候发现了一个特别好玩的事,有兴趣的话,可以自己尝试一下:把下面说的地方的y=0改为y=1在codevs里面能够ac,这……数据水?到一定境界……厉害了,吓得我还以为自己对了,结果一讲才 ...

  5. codevs 1200 同余方程 (Extend_Eulid)

    /* 扩展欧几里得 ax%b==1 -> ax-by==1 求不定方程的一组解 使x为最小正整数解 */ #include<iostream> #include<cstdio& ...

  6. 【codevs 1200】【NOIP 2012】同余方程 拓展欧几里德求乘法逆元模板题

    模板,,, #include<cstdio> using namespace std; void exgcd(long long a,long long b,long long & ...

  7. 【扩展欧几里得】Codevs 1200: [noip2012]同余方程

    Description 求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解. Input Description 输入只有一行,包含两个正整数 a, b,用 一个 空格隔开. Outpu ...

  8. 同余方程(codevs 1200)

    题目描述 Description 求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入描述 Input Description 输入只有一行,包含两个正整数 a, b,用 一个 空 ...

  9. codevs 1200:同余方程

    题目描述 Description 求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入描述 Input Description 输入只有一行,包含两个正整数 a, b,用 一个 空 ...

随机推荐

  1. 使用webpy创建一个简单的restful风格的webservice应用

    下载:wget http://webpy.org/static/web.py-0.38.tar.gz解压并进入web.py-0.38文件夹安装:easy_install web.py 这是一个如何使用 ...

  2. MySQL主从复制(Replication for Backup)

    环境:MySQL5.7,VMware-Workstation-12-Pro,Windows-10,CentOS-7.5,Xshell5 1. 基本概念和操作思路 主从复制能做什么 数据库的主从复制,是 ...

  3. angular.element 动态添加和删除元素

    addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...

  4. keras搭建密集连接网络/卷积网络/循环网络

    输入模式与网络架构间的对应关系: 向量数据:密集连接网络(Dense层) 图像数据:二维卷积神经网络 声音数据(比如波形):一维卷积神经网络(首选)或循环神经网络 文本数据:一维卷积神经网络(首选)或 ...

  5. javascript利用jquery-1.7.1来判断是否是谷歌Chrome浏览器

    <!DOCTYPE html> <html> <head>     <meta http-equiv="Content-Type" con ...

  6. C#中DataTable

    .C#中DataTable技术学习 2009-09-10 14:37:18 阅读1496 评论0   字号:大中小 订阅 . 1.在DataTable中执行DataTable.Select(" ...

  7. animation效果

    添加一个颜色灰渐变的动画效果. <!DOCTYPE html><html lang="en"><head> <meta charset=& ...

  8. python+Django框架运用(四)

    Django后台管理 基本配置 默认登录地址:http://127.0.0.1:8000/admin 创建后台管理员: python3  ./manage.py  createsuperuser # ...

  9. linux常用命令:traceroute 命令

    通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...

  10. 浅谈class私有变量

    class 的前世今生 在 es6 之前,虽然 JS 和 Java 同样都是 OOP (面向对象)语言,但是在 JS 中,只有对象而没有类的概念. 在 JS 中,生成实例对象的传统方法是通过构造函数, ...