题目描述

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

输入输出格式

输入格式:

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

输出格式:

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

输入输出样例

输入样例#1:

3 10
输出样例#1:

7

说明

【数据范围】

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

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

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

NOIP 2012 提高组 第二天 第一题

**************************************************************

 /*
Problem:
OJ:
User:S.B.S.
Time:
Memory:
Length:
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<functional>
#include<bitset>
#include<vector>
#include<list>
#include<map>
#define maxn 100001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define rep(i,j,k) for(int i=j;i<k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define maxm 1001
#define mod 998244353
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void print(int n){
if(n<){putchar('-');n=-n;}
if(n>=) print(n/);
putchar((n%)+'');
return;
}
int n,m;
inline int exgcd(int a,int b,int &x,int &y)
{
if(!b){x=;y=;return a;}
int d=exgcd(b,a%b,x,y);
int temp=x;x=y;y=temp-a/b*y;
return d;
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int a,b;cin>>a>>b;
int x,y;
exgcd(a,b,x,y);
x=(x%b+b)%b;
cout<<x<<endl;
return ;
}

洛谷 P1082 同余方程的更多相关文章

  1. 洛谷——P1082 同余方程

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

  2. 洛谷P1082 同余方程 [2012NOIP提高组D2T1] [2017年6月计划 数论06]

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

  3. 洛谷P1082 同余方程 题解

    题目链接:https://www.luogu.com.cn/problem/P1082 题目大意: 求关于 \(x\) 的同余方程 ax≡1(mod b) 的最小正整数解. 告诉你 \(a,b\) 求 ...

  4. [NOIP2012] 提高组 洛谷P1082 同余方程

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

  5. 洛谷P1082 同余方程

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

  6. 洛谷 P1082 同余方程 —— exgcd

    题目:https://www.luogu.org/problemnew/show/P1082 用 exgcd 即可. 代码如下: #include<iostream> #include&l ...

  7. 洛谷 P1082 同余方程(同余&&exgcd)

    嗯... 题目链接:https://www.luogu.org/problem/P1082 这道题很明显涉及到了同余和exgcd的问题,下面推导一下: 首先证明有解情况: ax + by = m有解的 ...

  8. 洛谷 P1082 同余方程 题解

    每日一题 day31 打卡 Analysis 题目问的是满足 ax mod b = 1 的最小正整数 x.(a,b是正整数) 但是不能暴力枚举 x,会超时. 把问题转化一下.观察 ax mod b = ...

  9. 洛谷 P1082 同余方程(exgcd)

    题目传送门 解题思路: 因为推导过程过于复杂,懒得写,所以题解传送门 AC代码: #include<iostream> #include<cstdio> using names ...

随机推荐

  1. 003 jquery层次选择器

    1.介绍 2.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  2. MySQL 中国省市区SQL表数据

    MySQL 中国省市区SQL表数据   1.查省SELECT * FROM china WHERE china.Pid=02.查市SELECT * FROM chinaWHERE china.Pid= ...

  3. 005.基于docker部署etcd集群部署

    一 环境准备 ntp配置:略 #建议配置ntp服务,保证时间一致性 etcd版本:v3.3.9 防火墙及SELinux:关闭防火墙和SELinux 名称 地址 主机名 备注 etcd1 172.24. ...

  4. python scrapy 调试模式

    scrapy通过命令行创建工程,通过命令行启动爬虫,那么有没有方式可以在IDE中调试我们的爬虫呢? 实际上,scrapy是提供给我们工具的, 1. 首先在工程目录下新建一个脚本文件,作为我们执行爬虫的 ...

  5. UEditor 的使用

    UEditor 的使用 一.UEditor 的使用 官方网站:http://ueditor.baidu.com/website/ 下载地址:http://ueditor.baidu.com/websi ...

  6. Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp

    B. Dreamoon and WiFi 题目连接: http://www.codeforces.com/contest/476/problem/B Description Dreamoon is s ...

  7. 【原】Spring整合Redis(第一篇)—SDR简述

    1.SDR说明 Spring Data Redis(SDR),是SpringFramework提供的一套简化访问Redis的API,是对Jedis的又一层封装. SDR集成了Jedis,JRedis, ...

  8. Delphi 资源管理器套件

    需要个类似资源管理器的东西, 首先试了下 TDriveComboBox.TDirectoryListBox.TFileListBox, 嘿! Win31 时代的东西, 不是一般地丑. 试了下 Vcl. ...

  9. HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  10. 【报错】RSA host key for 192.168.1.xxx has changed and you have requested strict checking.

    执行如下对机拷贝命令 scp .ssh/id_rsa.pub phpgo@192.168.1.35:~ 时,报错 RSA host key for 192.168.1.xxx has changed ...