POJ 2891 Strange Way to Express Integers(擴充套件GCD)
題意就是給你k組數,每組是m,a.有一個數字x對每組的m取餘得到的餘數為a。讓你求出滿足條件的最小的x。
分析假設只有一組資料那最小的就是餘數a。如果有兩組的話:x%m1 = a1,x%m2 = a2. m1*y+a1 = m2*z+a2。化簡得到:m1*y-m2*z=a2-a1.通過擴充套件GCD可以得到一組解y,z。所以可以得到一個x = y*m1+a1。滿足條件。如果x=(y*m1+a1)+k*LCM(m1,m2)(m1,m2的最小公倍數).可以滿足所有的解集。所以這兩個公式可以合併為一個公式:T%LCM(m1,m2) = y*m1+a1.這裡的T是x=(y*m1+a1)+k*LCM(m1,m2)中的某一個數字。
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9127 | Accepted: 2760 |
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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 10001000
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 101000;
using namespace std;
LL x, y;
LL exit_gcd(LL a, LL b)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL p = exit_gcd(b, a%b);
LL t = x;
x = y;
y = t-a/b*y;
return p;
}
LL find_x(LL a, LL b, LL m)
{
LL d;
d = exit_gcd(a, m);
if(b%d)
return -1;
return ((x*(b/d)%m)+m)%m;
}
int main()
{
int k;
LL m1, m2, a1, a2;
LL y;
while(cin >>k)
{
cin >>m1>>a1;
int flag = 0;
k--;
while(k--)
{
cin >>m2>>a2;
if(flag)
continue;
y = find_x(m2, a1-a2, m1);
if(y == -1)
{
flag = 1;
continue;
}
m1 = m1*m2/__gcd(m1,m2);//求出最小公倍數
a1 = a2+m2*y;//算出之前滿足條件的x,即為現在的餘數
a1 = (a1%m1+m1)%m1;//防止有越界的可能
}
if(flag)
cout<<"-1"<<endl;
else
cout<<a1<<endl;
}
return 0;
}
相關文章
- POJ 2891 Strange Way to Express IntegersExpress
- POJ 2891-trange Way to Express Integers(解線性同餘方程組)Express
- Strange Way to Express Integers(中國剩餘定理+不互質)Express
- POJ1061擴充套件歐幾里得定理套件
- POJ 1733 Parity【擴充套件域並查集】套件並查集
- kotlin 擴充套件(擴充套件函式和擴充套件屬性)Kotlin套件函式
- C Looooops(poj2115+擴充套件歐幾里德)OOP套件
- WCF擴充套件:行為擴充套件Behavior Extension套件
- 【Kotlin】擴充套件屬性、擴充套件函式Kotlin套件函式
- Sanic 擴充套件套件
- ORACLE 擴充套件Oracle套件
- 擴充套件工具套件
- 擴充套件歐幾里得套件
- DOM擴充套件套件
- 擴充套件ACL套件
- Lua擴充套件套件
- 照片擴充套件套件
- 擴充套件篇套件
- disable or 擴充套件套件
- 擴充套件表套件
- Mybatis擴充套件MyBatis套件
- POJ 2492 A bug's life【擴充套件域 | 邊帶權並查集】套件並查集
- 【poj3468】A Simple Problem with Integers
- JMeter 擴充套件開發:擴充套件 TCP 取樣器JMeter套件TCP
- ?用Chrome擴充套件管理器, 管理你的擴充套件Chrome套件
- ASP.NET Core擴充套件庫之Http通用擴充套件ASP.NET套件HTTP
- php7安裝redis擴充套件和memcache擴充套件PHPRedis套件
- POJ 1182 食物鏈【擴充套件域 | 邊帶權並查集】套件並查集
- 分類擴充套件套件
- 擴充套件表示式套件
- 新增php擴充套件PHP套件
- swift擴充套件ExtensionsSwift套件
- iOS 通知擴充套件iOS套件
- 可擴充套件性套件
- 19-擴充套件套件
- Nmap 擴充套件(四)套件
- Flask 自建擴充套件Flask套件
- 擴充套件包上傳套件