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; ...
随机推荐
- 【总结】前端框架:react还是vue?
之前写了一篇前端框架的大汇总,主要介绍了当下主流的框架和其特性.最近除了bootstrap,就属react和vue最为热门,这篇就主要拿这两个框架来做一下详细对比. 究竟如何正确使用?作为小白的我们从 ...
- Groovy/Spock 测试导论
Groovy/Spock 测试导论 原文 http://java.dzone.com/articles/intro-so-groovyspock-testing 翻译 hxfirefox 测试对于软件 ...
- VMware虚拟机 安装centos7并设置静态ip 连接外网
首先下载VMware虚拟机:http://xzc.197746.com/VMware-Workstation-12.5.9.zip 然后下载centos7镜像:http://vault.centos. ...
- PHP返回Json数据函数封装
/** * 返回Json数据 * @param int $code * @param string $message * @param array $data * @return string */ ...
- [转]计算机视觉之跟踪算法——相关滤波器Correlation Filter
https://blog.csdn.net/victoriaw/article/details/62416759 ASEF相关滤波器: Average of Synthetic Exact Filte ...
- 【Python项目】爬取新浪微博个人用户信息页
微博用户信息爬虫 项目链接:https://github.com/RealIvyWong/WeiboCrawler/tree/master/WeiboUserInfoCrawler 1 实现功能 这个 ...
- Linux输入子系统:多点触控协议 -- multi-touch-protocol.txt【转】
转自:http://blog.csdn.net/droidphone/article/details/8434768 Multi-touch (MT) Protocol --------------- ...
- ADB常用命令(二)
参考 http://adbshell.com/commands 常用命令 查看adb 版本 adb version 打印所有附加模拟器/设备的列表 adb devices 设备序列号 adb get ...
- java基础42 File类的构造函数及其方法
本文知识点(目录): 1.IO流(Input Output) 2.IO流技术的运用场景 3.File类的构造函数(方法) 4.File类的一些常用方法 5.实例(解析File类 ...
- Java内存优化和性能优化的几点建议
1.没有必要时请不用使用静态变量 使用Java的开发者都知道,当某个对象被定义为stataic变量所引用,这个对象所占有的内存将不会被回收.有时,开发者会将经常调用的对象或者变量定义为static,以 ...