POJ2891 Strange Way to Express Integers [中国剩余定理]
不互质情况的模板题
注意多组数据不要一发现不合法就退出
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
inline ll read(){
char c=getchar();ll x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
ll n,a1,m1,a2,m2;
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(b==) d=a,x=,y=;
else exgcd(b,a%b,d,y,x),y-=(a/b)*x;
}
int main(){
freopen("in","r",stdin);
while(scanf("%lld",&n)!=EOF){
int flag=;
m1=read();a1=read();n--;
while(n--){
m2=read();a2=read();
if(flag) continue;
ll d,t1,t2;
exgcd(m1,m2,d,t1,t2);
if((a2-a1)%d){flag=;continue;}
t1*=(a2-a1)/d;
m2/=d;
t1=(t1%m2+m2)%m2;
a1=a1+m1*t1;
m1*=m2;
}
if(flag) puts("-1");
else printf("%lld\n",a1);
}
}
POJ2891 Strange Way to Express Integers [中国剩余定理]的更多相关文章
- 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 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- 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 ...
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
- P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers
P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...
- POJ2891 Strange Way to Express Integers【扩展中国剩余定理】
题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...
随机推荐
- JSON对象添加删除属性
假如目前我们有如下一个Json对象 var jsonObj={ 'param1':22, 'param2' :33 }; 增加属性: 我们现在向该对象jsonObj中添加一个新的属性字段:param3 ...
- 基于.netcore 开发的轻量Rpc框架
Rpc原理详解 博客上已经有人解释的很详细了,我就不在解释了.传送门 项目简介 项目是依赖于.net core2.0版本,内部都是依靠IOC来实现的,方便做自定义扩展.底层的通信是采用socket,s ...
- 简单易用的IT运维服务器管理程序
程序修改历史: 2012.02.04 v1.0 leopku 2012.02.09 v1.1 oldboy(老男孩) QQ:31333741 MAIL:oldboy521@gmail.com 本软件由 ...
- Nginx实战之反向代理WebSocket的配置实例
http://www.jb51.net/article/112183.htm 最近在工作中遇到一个需求,需要使用 nginx 反向代理websocket,经过查找一番资料,目前已经测试通过,所以这篇文 ...
- SQL Server中字段类型对应的C#中的数据类型
数据库 C#程序 int int32 text string bigint int64 binary System.Byte[] bit Boolean cha ...
- 给外行或者刚入门普及一下关于C#,.NET Framework(.NET框架),.Net,CLR,ASP,ASP.Net, VS,以及.NET Core的概念
一.概念 1. C# :C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言. 2..NET Framework(.NET框架):.NET framework ...
- Codeforces 897D. Ithea Plays With Chtholly (交互)
题目链接:D. Ithea Plays With Chtholly 题意: 给你n张纸,在纸上写字(在 1 - c之间)可以写m次数 (,).(主要是交互,让你判断) 题解: 首先,看到m>=n ...
- Java 反射和内省实现spring的IOC和DI
1.构造两个JavaBean package com.spring.model; public class People { private Car car; public Car getCar() ...
- 在windows下进行Kafka单机测试
环境:win10 kafka版本:kafka_2.11-0.10.0.0 zookeeper版本:zookeeper-3.4.11 1. 开启zookeeeper server cmd下 : .\bi ...
- Runtime.addShutdownHook的用法
原文出处:http://kim-miao.iteye.com/blog/1662550.感谢作者的无私分享. 一.Runtime.addShutdownHook理解 在看别人的代码时,发现其中有这个方 ...