POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891
题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数……
https://blog.csdn.net/HownoneHe/article/details/52186204
这个博客提供了互质情况下的代码以及由此递推出的(另一个版本的)非互质情况下的代码。
假如给出m[n],a[n]分别代表要求的除数和余数:
互质情况下:
( 做n次 ) 对不包含m[i]的所有m求积 ( 互质的数的最小公倍数 ) , exgcd求出来逆元后*a[i], 然后对这n个结果求和取模.
设M为不包含m[i]的所有m的积,
那么 (M*x)%m[i]=1 --> (M*x)=1+m[i]*y --> (M*x)-m[i]*y=1
exgcd后的M*x就是单位1 ( 逆元 ) 了.
非互质情况下:
M被分解质因子,重复质因子的指数都变为1(最小公倍数),然后做同样操作。
我这里写的是一个相对优化的代码(博客中的第二个版本),使用的空间更小,求最小公倍数更加方便,也更不容易过程中溢出,嗯读一下代码应该就明白了。
我总觉得我以前写过中国剩余定理啊为什么现在这么详细解释……
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define LL long long
const LL maxn=;
LL n;
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
x=;y=;d=a;
return;
}exgcd(b,a%b,d,x,y);
LL z=x;x=y;
y=z-(LL)(a/b)*y;
}
int main(){
while(~scanf("%lld",&n)){
LL mc=,ac=,m1=,a1=,f=;
for(int i=;i<=n;i++){
scanf("%lld%lld",&m1,&a1);
if(f)continue;
if(i==){mc=m1;ac=a1;continue;}
LL x,y,d,c=a1-ac; exgcd(mc,m1,d,x,y);
if(c%d){f=;continue;}
x=(((x*(c/d))%m1)+m1)%m1;
ac+=mc*x;
mc=mc/d*m1;
ac=ac%mc;
}
if(f)printf("-1\n");
else printf("%lld\n",ac);
}
}
POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd的更多相关文章
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- POJ 2891 Strange Way to Express Integers(中国剩余定理)
题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
随机推荐
- C# Func的同步、异步调用
using System; namespace ActionDemo { class Program { static void Main(string[] args) { Console.Write ...
- 关于root
1.root优缺 android的root跟苹果越狱很类似,可以享受"解禁"后的很多自由. 1.删除系统中不需要的一些app,特别是一些厂商强制安装的app. 2.美化系统,例如修 ...
- 20165320 第四次实验 Android开发
实验内容: Android程序设计-1 实验要求: - 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: - 参 ...
- 【codeforces】【比赛题解】#950 CF Round #469 (Div. 2)
剧毒比赛,至少涨了分对吧.: ( [A]Left-handers, Right-handers and Ambidexters 题意: 有\(l\)个右撇子,\(r\)个左撇子,\(a\)个双手都惯用 ...
- 冲量:momentum
参见:http://www.jianshu.com/p/58b3fe300ecb,这个博客里有冲量的python实现的代码和讲解 “冲量”这个概念源自于物理中的力学,表示力对时间的积累效应. 在普通的 ...
- NTC热敏电阻认识及功率型热敏电阻选型
-------------------记公司一款已经量产的产品,在最新加工的批次上出现:刚上电就炸毁功率型热敏电阻的问题------------------------- ************写在 ...
- 查看gcc的默认宏定义命令【转】
转自:http://blog.csdn.net/cywosp/article/details/10730931 有些时候我们在编写代码或者阅读开源项目时经常会遇到一些陌生的宏定义,在找遍所有源代码都没 ...
- strcpy unsigned char
http://bbs.csdn.net/topics/250068243 char *strcpy(char* dest, const char *src); 用unsigned char编译会出错 ...
- cvpr densnet论文
- 在SQL中有时候我们需要查看现在正在SQL Server执行的命令
在SQL中有时候我们需要查看现在正在SQL Server执行的命令.在分析管理器或者Microsoft SQL Server Management Studio中,我们可以在"管理-SQL ...