中国剩余定理(excrt) 模板
2024-08-31 10:47:01
excrt板子题
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define N 100010
#define rint register int
#define ll long long
#define il inline
using namespace std;
//re
int n;
ll A[N],B[N];
ll qadd(ll x,ll y,const ll &mo){ll ans=;while(y){if(y&)ans=(ans+x)%mo;x=(x+x)%mo;y>>=;}return ans;}
ll qpow(ll x,ll y,const ll &mo){ll ans=;while(y){if(y&)ans=(ans*x)%mo;x=(x*x)%mo;y>>=;}return ans;}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==){x=,y=;return a;}
ll ans=exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
return ans;
}
ll excrt()
{
ll x,y,ans=A[],M=B[],bg;
for(int i=;i<=n;i++)
{
ll a=M,b=B[i],c=(A[i]-ans%b+b)%b;
ll gcd=exgcd(M,b,x,y);bg=b/gcd;
if(c%gcd!=) return -;
x=qadd(x,c/gcd,bg);
ans+=x*M;
M*=bg;
ans=(ans%M+M)%M;
}
return ans;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld%lld",&B[i],&A[i]);
printf("%lld\n",excrt());
return ;
}
最新文章
- 优化MySchool数据库设计之【巅峰对决】
- iOS 实用博客整理(连载版)
- Delphi 用文件流读取文本文件字符串的方法
- 电脑无法登陆ftp
- Eclipse Gtk+
- 用setTimeout 代替 setInterval实时拉取数据
- Xcode-Xcode 7.3 解决不能自动联想问题-备
- C# Linq Group By 多个字段并返回给实体类List
- eclipse配置自动提示EXTJS和jQurey
- ural1037 Memory Management
- 洛谷P3459 [POI2007]MEG-Megalopolis(树链剖分,Splay)
- 章节十、3-CSS Selector---用CSS Selector - ID定位元素
- 原生JS插件(超详细)
- Springboot添加定时任务
- jquery遇到的问题
- AutoMapper自动映射
- Transcranial magnetic stimulation (TMS)
- jitter
- .net core2 mvc项目中,加入RazorPages页面
- OSGi 系列(七)之服务的监听、跟踪、声明等
热门文章
- Codevs 1077 多源最短路( Floyd水 )
- 【转】H5 input search 提交事件
- nginx的一些
- Problem 13
- 配置监听器 服务器启动时 检索常用数据 保存在application中 减少数据的查询操作(OA项目)
- Linux Kernel(Android) 加密算法总结(一)(cipher、compress、digest)
- Android 进程常驻(5)----开机广播的简单守护以及总结
- 第一篇、Android Supersu 权限管理定制,隐藏过滤权限,指定APP最高权限
- 利用js在文本框末尾获得焦点
- ContextMenu的使用具体解释