POJ.2891.Strange Way to Express Integers(扩展CRT)
2024-10-17 00:13:15
[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;
}
最新文章
- Beginning Scala study note(5) Pattern Matching
- ecshop 后台分页功能
- 解读vmstat中的ACTIVE/INACTIVE MEMORY
- 一个textview实现文字在图片上面的效果
- DZ!NT论坛 3.6.711删除用户各种错解决方案
- sqlserver 增加表字段
- [ES6] for..in &;&; for..of
- 你想不到的IT运维前途
- ruby和Python简单对比
- Oracle杀死死锁进程
- pod install 出现 Unable to find a specification for `xxxxx` 解决方案
- python selenium自动化之-环境搭建
- 设计模式之Adapter模式
- Python Learning: 02
- 自然语言推断(NLI)、文本相似度相关开源项目推荐(Pytorch 实现)
- php操作redis数据库方法总结
- BuildTool
- UniGUI的布局使用说明
- OK335xS GPMC nand device register hacking
- HDU 6187 Destroy Walls
热门文章
- grep用法【转】
- mysql5.6.13通用二进制格式安装并使用amoeba实现对mysql5.6数据库读写分离
- 解决报错SAXNotRecognizedException: Feature &#39;http://javax.xml.XMLConstants/feature/secure-processing&#39; not recognized
- ubuntu系统下Python虚拟环境的安装和使用
- 微服务(Microservices )简介
- Python-html css 盒模型
- 基于Golang设计一套微服务架构[转]
- ie6 表格td中无内容时不显示边框的解决办法
- Windows环境selenium+Python环境配置
- JavaScript事件属性event.target