[NOIP2012] 同余方程(第三次考试大整理)
2017-04-18 21:53
267 查看
1265. [NOIP2012] 同余方程
输入文件:mod.in 输出文件:mod.out 简单对比
时间限制:1
s 内存限制:128 MB
【题目描述】
求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解。
【输入格式】
输入只有一行,包含两个正整数 a, b,用一个空格隔开。
【输出格式】
输出只有一行,包含一个正整数X0,即最小正整数解。输入数据保证一定有解。
【样例输入】
3 10
【样例输出】
7
【数据范围】
对于 40%的数据,2 ≤b≤ 1,000;
对于 60%的数据,2 ≤b≤ 50,000,000;
对于 100%的数据,2 ≤a, b≤ 2,000,000,000。
【思路】
就是裸的欧几里得……然而考试的时候并没有想到……数论学的时候好不容易听懂的一个题,一到考试还是不记得,所以说一定要进行整理!
【代码】
//扩展欧几里得: #include <cstdio> int a, b; int exgcd(int a,int b,int &x,int &y) { if(b==0) { x=1,y=0; return a; } int r=exgcd(b,a%b,x,y),tmp; tmp=x,x=y,y=tmp-a/b*y; return r; } /* void exgcd(int a, int b, int &x, int &y) { if (!b) { x = 1, y = 0; return; } int q = a / b, r = a % b; exgcd(b, r, y, x); y -= q * x; } */ int main() { scanf("%d%d", &a, &b); int x, y; exgcd(a, b, x, y); while (x < 0) x = x + b; printf("%d", x); return 0; }
然而我的代码就是打暴力:
(我都不知道我是怎么打出来的样例过了我就交上了,竟然能够A 6个点)
上代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<cstdlib> using namespace std; long long a,b,k;//对于 40%的数据,2 ≤b≤ 1,000; 对于 60%的数据,2 ≤b≤ 50,000,000; 对于 100%的数据,2 ≤a, b≤ 2,000,000,000。 long long x0=1; void ss(long long b) { while(x0<a*b) { if((a*x0)%b==1) { printf("%lld",x0); return; } else ++x0; } if(x0==a*b) { b+=b; ss(b); } } int main() { freopen("mod.in","r",stdin); freopen("mod.out","w",stdout); scanf("%lld%lld",&a,&b); if(a>b) { a=a%b; } if(a<b) { ss(b); } fclose(stdin); fclose(stdout); return 0; }
相关文章推荐
- NOIP2012 同余方程的两种解法
- noip2012 同余方程 (扩展欧几里得)
- [NOIP 2012] 同余方程 · 拓展欧几里得
- noip2012 同余方程 exgcd
- LuoguP1082[NOIP2012] 同余方程 解题报告【拓展欧几里得】
- [noip2012]同余方程 题解
- [NOIP2012]同余方程
- CJOJ 1029 【NOIP2012】同余方程
- NOIP2012复赛提高组day2(A:同余方程 B:借教室 C:疫情控制)
- C++——拓展欧几里得模板——同余方程【NOIP2012提高组】
- NOIP2012提高组 同余方程
- NOIP2012 同余方程
- [NOIp 2012]同余方程
- [NOIP2012] 提高组 洛谷P1082 同余方程
- 【NOIP2012】同余方程
- 【codevs1200】 NOIP2012—同余方程
- 1265. [NOIP2012] 同余方程
- NOIP 2012 提高组 复赛 day2 mod 同余方程
- 【NOIP2012提高组】同余方程 (扩展欧几里得算法)
- 185.[USACO Oct08] 挖水井 (第三次考试大整理)