poj2891(线性同余方程组)
一个exgcd解决一个线性同余问题,多个exgcd解决线性同余方程组。
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 12001 | Accepted: 3797 |
Description
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.
#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(线性同余方程组)的更多相关文章
- HDU3579:Hello Kiki(解一元线性同余方程组)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...
- HDU1573:X问题(解一元线性同余方程组)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题目解析;HDU就是坑,就是因为n,m定义成了__int64就WAY,改成int就A了,无语. 这题 ...
- HDU1573 X问题【一元线性同余方程组】
题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=1573 题目大意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X ...
- 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 ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- POJ2891:Strange Way to Express Integers(解一元线性同余方程组)
写一下自己的理解,下面附上转载的:若a==b(modk);//这里的==指的是同余,我用=表示相等(a%k=b)a-b=kt(t为整数)以前理解的错误思想:以前认为上面的形式+(a-tb=k)也是成立 ...
- hdu1573(线性同余方程组)
套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- POJ2891Strange Way to Express Integers (线性同余方程组)
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...
- HDU-1573-X问题(线性同余方程组)
链接: https://vjudge.net/problem/HDU-1573 题意: 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1] ...
随机推荐
- docker运行mysql
http://blog.csdn.net/u011492260/article/details/77970445 第一步: 安装Docker:首先到docker官网下载适合自己电脑当前系统的版本,并安 ...
- 文件流:"fopen","fclose",“ftell”"fseek","fgets","fprintf" ,“feof”,"fwrite","fread"
char const* filename="D:/hello.txt"; 路径名使用的是“/”或者使用 转义字符“\\”: "fopen", FILE *fp= ...
- mongodb修改器(转)
MongoDB 修改器 对文档中的某些字段进行更新 $inc 专门用来增加(或减少)数字的,只能用于整数,长整数或双精度浮点型的值$inc键的值必须为数字,不能使用字符串,数组或其他非数字的值如果键不 ...
- CKEditor+SWFUpload实现功能较为强大的编辑器(三)---后台接收图片流程
在前台配置完CKEditor和SWFUpload之后就可以满足基本的需求了 在这里,我配置的接收异步上传的图片的页面为upload.ashx 在这个ashx中对上传的图片处理的流程如下: contex ...
- asp.net使用母版页以及Jquery和prototype要注意的问题
在母版页中引用了js,css或者其他外部文件之后,子页面就不必再重新引用,否则可能出错 prototype.js和jquery.js冲突的解决方案: <script type="tex ...
- sass的高级语法
1. 变量 sass允许使用变量,所有变量以$开头 2.引用父元素 & 这里 "&" 就代表是 a 3.继承 这样 class2 就 拥有了class1的所有属性 ...
- 【共享单车】—— React后台管理系统开发手记:AntD Table基础表格
前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...
- 我与小娜(36):人机大战第五局,AlphaGo必胜!
我与小娜(36):人机大战第五局,AlphaGo必胜! 小娜知道,细致阅读论文"Mastering the game of Go with deep neural network ...
- [SCSS] Create a gradient with a Sass loop
In this lesson, you will learn how to iteratively generate CSS selectors and attributes using Sass l ...
- 在MyEclipse上安装GIT插件EGit
MyEclipse2016 CI 下载地址:http://pan.baidu.com/s/1gfBw9Ab 1.“Help”->"Install from Site" 2.在 ...