一个exgcd解决一个线性同余问题,多个exgcd解决线性同余方程组。

Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 12001   Accepted: 3797

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 ≤ ik) 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 ≤ ik).

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.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define N 10100 long long a[N],r[N]; long long cal_axb(long long a,long long b,long long mod)
{
long long sum=;
while(b)
{
if(b&) sum=(sum+a)%mod;
b>>=;
a=(a+a)%mod;
}
return sum;
} // ax+by = gcd(a,b) ->求解x,y 其中a,b不全为0,可以为负数
// 复杂度:O(log2a)
void extendgcd(long long a,long long b,long long &x,long long &y)
{
if(a%b==)
{
//到了终止条件
x=; y=;
return ;
}
extendgcd(b,a%b,x,y);
long long tmpx;
tmpx=y;
y=x - (a/b)*y;
x=tmpx;
} long long Multi_ModX(long long m[],long long r[],int n)
{
long long m0,r0;
m0=m[]; r0=r[];
for(int i=;i<n;i++)
{
long long m1=m[i],r1=r[i];
long long tmpd=__gcd(m0,m1);
if( (r1 - r0)%tmpd!= ) return -;
long long k0,k1;
extendgcd(m0,m1,k0,k1);
k0 *= (r1-r0)/tmpd;
//k0会不会很大
m1 *= m0/tmpd;
r0 = (cal_axb(k0,m0,m1)+r0)%m1;
m0=m1;
}
return (r0%m0+m0)%m0;
} int main()
{
int k;
while(cin>>k)
{
for(int i=;i<k;i++)
cin>>a[i]>>r[i];
cout<<Multi_ModX(a,r,k)<<endl;
}
return ;
}

poj2891(线性同余方程组)的更多相关文章

  1. HDU3579:Hello Kiki(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...

  2. HDU1573:X问题(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题目解析;HDU就是坑,就是因为n,m定义成了__int64就WAY,改成int就A了,无语. 这题 ...

  3. HDU1573 X问题【一元线性同余方程组】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1573 题目大意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X ...

  4. AcWing 204. 表达整数的奇怪方式 (线性同余方程组)打卡

    给定2n个整数a1,a2,…,ana1,a2,…,an和m1,m2,…,mnm1,m2,…,mn,求一个最小的整数x,满足∀i∈[1,n],x≡mi(mod ai)∀i∈[1,n],x≡mi(mod  ...

  5. poj3708(公式化简+大数进制装换+线性同余方程组)

    刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...

  6. POJ2891:Strange Way to Express Integers(解一元线性同余方程组)

    写一下自己的理解,下面附上转载的:若a==b(modk);//这里的==指的是同余,我用=表示相等(a%k=b)a-b=kt(t为整数)以前理解的错误思想:以前认为上面的形式+(a-tb=k)也是成立 ...

  7. hdu1573(线性同余方程组)

    套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. POJ2891Strange Way to Express Integers (线性同余方程组)

    Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...

  9. HDU-1573-X问题(线性同余方程组)

    链接: https://vjudge.net/problem/HDU-1573 题意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1] ...

随机推荐

  1. [置顶] cAdvisor、InfluxDB、Grafana搭建Docker1.12性能监控平台

    通过cadvisor+influxdb+grafana三者有机结合,打造跨主机容器监控. 优点 1.跨主机监控,可扩展 2.容器自发现 3.历史数据长期保存 4.自定义配置程度高 缺点 1.不能自动隐 ...

  2. 命令行下的“蒙面歌王”rundll32.exe

    在Windows系统中,为了节省内存和实现代码重用,微软在Windows操作系统中实现了一种共享函数库的方式.这就是DLL(Dynamic Link Library)文件,即动态链接库,这种库包含了可 ...

  3. python测试与调试提示

    测试与调试提示 2.1 在交互模式输入一个python语句就会执行一个.在调试程序时,这种模式尤其有用. 2.2 在一个文件调用python解释器后,解释器会在文件中的最后一个语句执行之后推出.然而, ...

  4. zabbix日志监控

    一般情况下,日志最先反映出应用当前的问题,在海量日志里面找到我们异常记录,例如监控系统日志.nginx.Apache.业务日志,然后记录下来,并且根据情况报警. 1.日志监控项介绍 最主要的是监控日志 ...

  5. Some perl tips

    下面是日常工作中一些代码片段的总结,部分注释是后加的,采用了//这种形式,请勿套用. 1.取得用户输入print("Please input the date range:");$ ...

  6. 不依赖Excel是否安装的Excel导入导出类

    本文利用第三方开源库NPOI实现Excel97-2003,Excel2007+的数据导入导出操作. 不依赖Office是否安装.NPOI开源项目地址:http://npoi.codeplex.com/ ...

  7. Angular 学习笔记——标签指令

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...

  8. Python学习笔记(一)类和继承的使用

    一年前就打算学Python了,折腾来折腾去也一直没有用熟练,主要是类那一块不熟,昨天用Python写了几个网络编程的示例,感觉一下子迈进了很多.这几天把学习Python的笔记整理一下,内容尽量简洁. ...

  9. mybatis like写法

    name like concat(concat('%',#{name}),'%') name like concat('%',#{name},'%')

  10. 51单片机 | 并行I/O口扩展实例(74LS244/74LS373/4071)

    并行I/O口扩展实例 //<51单片机原理及应用(第二版)——基于Keil C与Proteus>第四章例4.4 I/O口不能完全用于输入/输出操作,当需要扩展外部存储器时,P0.P2口用作 ...