题目链接

扩展中国剩余定理:1(直观的)2(详细证明)

[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;
}

最新文章

  1. Beginning Scala study note(5) Pattern Matching
  2. ecshop 后台分页功能
  3. 解读vmstat中的ACTIVE/INACTIVE MEMORY
  4. 一个textview实现文字在图片上面的效果
  5. DZ!NT论坛 3.6.711删除用户各种错解决方案
  6. sqlserver 增加表字段
  7. [ES6] for..in &amp;&amp; for..of
  8. 你想不到的IT运维前途
  9. ruby和Python简单对比
  10. Oracle杀死死锁进程
  11. pod install 出现 Unable to find a specification for `xxxxx` 解决方案
  12. python selenium自动化之-环境搭建
  13. 设计模式之Adapter模式
  14. Python Learning: 02
  15. 自然语言推断(NLI)、文本相似度相关开源项目推荐(Pytorch 实现)
  16. php操作redis数据库方法总结
  17. BuildTool
  18. UniGUI的布局使用说明
  19. OK335xS GPMC nand device register hacking
  20. HDU 6187 Destroy Walls

热门文章

  1. grep用法【转】
  2. mysql5.6.13通用二进制格式安装并使用amoeba实现对mysql5.6数据库读写分离
  3. 解决报错SAXNotRecognizedException: Feature &#39;http://javax.xml.XMLConstants/feature/secure-processing&#39; not recognized
  4. ubuntu系统下Python虚拟环境的安装和使用
  5. 微服务(Microservices )简介
  6. Python-html css 盒模型
  7. 基于Golang设计一套微服务架构[转]
  8. ie6 表格td中无内容时不显示边框的解决办法
  9. Windows环境selenium+Python环境配置
  10. JavaScript事件属性event.target