POJ.2891.Strange Way to Express Integers(扩展CRT)
[Upd:]https://www.luogu.org/problemnew/solution/P4774



#include <cstdio>
#include <cctype>
#define gc() getchar()
typedef long long LL;
const int N=1e6+5;
LL n,m[N],r[N];
inline LL read()
{
LL now=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc()) if(c=='-') f=-1;
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now*f;
}
LL Exgcd(LL a,LL b,LL &g,LL &x,LL &y)
{
if(!b) g=a, x=1ll, y=0ll;
else Exgcd(b,a%b,g,y,x),y-=a/b*x;
}
LL Ex_CRT()
{
LL M=m[1],R=r[1],x,y,g,t;
for(int i=2; i<=n; ++i)
{
Exgcd(M,m[i],g,x,y);
if((r[i]-R)%g) return -1ll;
x*=(r[i]-R)/g, t=m[i]/g, x=(x%t+t)%t;//相当于M*(x/((ri-R)/g)) ≡ g(mod mi/g)?不管了就这么理解吧
R+=M*x, M*=t, R%=M;
}
return (R%M+M)%M;
}
int main()
{
while(~scanf("%lld",&n))
{
for(int i=1; i<=n; ++i) m[i]=read(),r[i]=read();
printf("%lld\n",Ex_CRT());
}
return 0;
}
POJ.2891.Strange Way to Express Integers(扩展CRT)的更多相关文章
- POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)
题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...
- poj 2891 Strange Way to Express Integers (扩展gcd)
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...
- 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(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- [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; ...
- POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】
求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...
随机推荐
- Salt Document学习笔记2
配置文件需修改的内容及注意点: Edit the master config file: 1. Uncomment and change the user: root value to your ow ...
- Android Studio 超级简单的打包生成apk
为什么要打包: apk文件就是一个包,打包就是要生成apk文件,有了apk别人才能安装使用.打包分debug版和release包,通常所说的打包指生成release版的apk,release版的apk ...
- NodeJs>------->>第二章:Node.js中交互式运行环境--------REL
第二章:Node.js中交互式运行环境--------REL 一:REPL运行环境概述 C:\Users\junliu>node > foo = 'bar' ; 'bar' > 二: ...
- JS定义一个立即执行的可重用函数
我定义了一个函数表达式 testFun var testFun = (function() { ... //函数内容})(); 测试结果:虽然 testFun 函数有如愿在页面加载后立即被执行,但再次 ...
- hdu1561 树形dp,依赖背包
多重背包是某个物品可以选择多次,要把对物品数的枚举放在对w枚举外面 分组背包是某组的物品只能选一个,要把对每组物品的枚举放在对w枚举内侧 依赖背包是多层的分组背包,利用树形结构建立依赖关系,每个结点都 ...
- python + slenium自动化测试设置元素等待
WebDriver 提供了两种类型的等待:显式等待和隐式等待. 显式等待 显式等待使 WebdDriver 等待某个条件成立时继续执行,否则在达到最大时长时抛出超时异常 (TimeoutExcepti ...
- python 全栈开发,Day40(进程间通信(队列和管道),进程间的数据共享Manager,进程池Pool)
昨日内容回顾 进程 multiprocess Process —— 进程 在python中创建一个进程的模块 start daemon 守护进程 join 等待子进程执行结束 锁 Lock acqui ...
- 2018-2019-2 20165333 《网络对抗技术》 Exp5:MSF基础应用
2018-2019-2 20165333 <网络对抗技术> Exp5:MSF基础应用 实践内容(3.5分) 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路 ...
- C#检查服务状态和启动关闭服务
WinForm 判断服务状态,显示服务名称和状态 https://blog.csdn.net/u013063880/article/details/78626200 C#获得服务,判断服务状态,启动服 ...
- Redis设置Key/value的规则定义和注意事项(附工具类)
对于redis的存储key/value键值对,经过多次踩坑之后,我们总结了一套规则:这篇文章主要讲解定义key/value键值对时的定义规则和注意事项. 前面一篇文章讲了如何定义Redis的客户端和D ...