【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)
Description
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:
Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
Line 1: Contains the integer k.
Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k).
Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
2
8 7
11 9
Sample Output
31
Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
Source
POJ Monthly--2006.07.30, Static
参考代码
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000
#define REP(i,x,n) for(int i=x;i<=n;i++)
#define DEP(i,x,n) for(int i=n;i>=x;i--)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void Out(ll a){
if(a<0) putchar('-'),a=-a;
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=1e6+10;
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(b==0){
x=1;y=0;
d=a;
}else{
exgcd(b,a%b,d,y,x),y-=x*(a/b);
}
}
int a[N],r[N],n;
ll solve(){
ll ta=a[1],tr=r[1],x,y,d;
for(int i=2;i<=n;i++){
exgcd(ta,a[i],d,x,y);
if((r[i]-tr)%d) return -1;
x=(r[i]-tr)/d*x%(a[i]/d);
tr+=x*ta;ta=ta/d*a[i];
tr%=ta;
}
return tr>0?tr:tr+ta;
}
int main(){
while(~scanf("%d",&n)){
REP(i,1,n) a[i]=read(),r[i]=read();
Out(solve());
puts("");
}
return 0;
}
【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)的更多相关文章
- POJ 2891 Strange Way to Express Integers (解一元线性方程组)
求解一元线性同余方程组: x=ri(mod ai) i=1,2,...,k 解一元线性同余方程组的一般步骤:先求出前两个的解,即:x=r1(mod a1) 1x=r2(mod a2) ...
- 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(线性同余方程组)
链接: https://vjudge.net/problem/POJ-2891 题意: Elina is reading a book written by Rujia Liu, which intr ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- 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 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
随机推荐
- Discovering Gold LightOJ - 1030 || 概率与期望求法区别
#include<cstdio>//wrong_codes #include<algorithm> using namespace std; ],anss; ],T,TT,n, ...
- HTML5实现页面自动跳转
初学H5,书上说的是<meta http-equiv="refresh" content="5“ url=”./error.html"/>这样可以实 ...
- sql注入原理及解决方案
sql注入原理 sql注入原理就是用户输入动态的构造了意外sql语句,造成了意外结果,是攻击者有机可乘 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的 ...
- js调用本地程序
前几天,做项目时候用到js调用本地的程序,找了好多资料,一种是写入注册表,一种是写一个浏览器插件,相对来说,写一个注册表更简单一点,因为需求很紧.下面就是我的总结,希望可以对你们有所帮助,具体从哪里找 ...
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- JVM内存各个区域分工简单介绍
JVM内存各个区域简单介绍: 程序计数器:程序计数器是一块较小的内存空间,它可以看作是当前线程所执行的字节码的行号指示器. 在使用多线程时,为了线程切换后能恢复到正确的执行位置,每条线程都需要有个独立 ...
- git 配置免密上传,配置ssh key
1.windows 打开git bash 控制台,linux 直接打开命令控制台,输入 ssh-keygen 一直enter 下一步 2.生成的文件windows 存放在c://users 路径下,l ...
- function calling convention
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=31 February 19, 2013 function calling c ...
- SQL Server中的事务日志管理的阶梯,级别1:事务日志概述
SQL Server中的事务日志管理的阶梯,级别1:事务日志概述 翻译:刘琼滨 谢雪妮 许雅莉 赖慧芳 级别1:事务日志概述 事务日志是一个文件,其中SQL服务器存储了所有与日志文件关联的数据库执行的 ...
- Windows程序设计2(消息机制、菜单)
一 .小记; PostQuitMessage(0); 产生WM_QUIT消息给进程队列,且立即返回,同时使得消息循环退出,使得进程终止.(其实它通过PostMessage(hWnd,WM_QUIT,0 ...