一个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. [置顶] kubernetes资源类型--DaemonSet

    概念 DaemonSet能够让所有(或者特定)的节点运行同一个pod. 当节点加入到K8S集群中,pod会被(DaemonSet)调度到该节点上运行,当节点从K8S集群中被移除,被DaemonSet调 ...

  2. office2010使用mathtype时,出现未找到MathPage.WLL解决方案--亲测有用

    安装mathtype时,出现如下错误: 解决方案: 参考此网址中的内容:http://www.mathtype.cn/wenti/word-jianrong.html 首先需要找到在Word加载的两个 ...

  3. 16个Linux服务器监控命令

    在不同的Linux发行版中,会有不同的GUI程序可以显示各种系统信息,比如SUSE linux发行版中,就有非常棒的图形化的配置和管理工具YaST,KDE桌面环境里的KDE System Guard也 ...

  4. Hive 性能调优

    避免执行MR select * or select field1,field2 limit 10 where语句中只有分区字段或该表的本地字段 使用本地set hive.exec.mode.local ...

  5. [转载]Elasticsearch Java API总汇

    from: http://blog.csdn.net/changong28/article/details/38445805#comments 3.1 集群的连接 3.1.1 作为Elasticsea ...

  6. ElasticSearch中设置排序Java

    有用的链接:http://stackoverflow.com/questions/12215380/sorting-on-several-fields-in-elasticsearch 有的时候,需要 ...

  7. Java学习之泛型和异常

    泛型 1,设计原则或目的:只要代码在编译的时候没有错误,就不会抛异常.  2,泛型通配符  :类型通配符一般是使用 ? 代替具体的类型实参.注意了,此处是类型实参,而不是类型形参!相当于(父类作用)L ...

  8. [PWA] Show an Error when a POST or DELETE Fails in an Offline PWA

    We're storing JSON data in the cache - but that only applies to HTTP GET requests - and not to POST  ...

  9. 页面加载后累加,自加1&&判断数字是否为两位数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 从零单排之玩转Python安全编程(II)

    转自:http://www.secpulse.com/archives/35893.html 都说Python大法好,作为一名合格的安全从业人员,不会几门脚本语言都不好意思说自己是从事安全行业的. 而 ...