poj 2891Strange Way to Express Integers 中国剩余定理(不互质) 数据没读完就结束了程序居然是runtime error。
2017-03-05 19:36
393 查看
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
Sample Output
Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
#include<cstdio>
#include<iostream>
using namespace std;
#define ll long long
ll g;
void exgcd(ll a,ll b,ll& x,ll& y)
{
if(b==0)
{
g=a;
x=1;
y=0;
}
else
{
exgcd(b,a%b,y,x);
y-=x*(a/b);
}
}
int main()
{
ll n,a,m,mt,att,k1,k2,c;
while(cin>>n)
{
int flag=0;
cin>>m>>a;
for(int i=1; i<n; i++)
{
cin>>mt>>att;
if(flag) continue;
c=att-a;
exgcd(m,mt,k1,k2);
if(c%g)
{
flag=1;
cout<<-1<<endl;
continue;
}
k1=k1*c/g;
k1=(k1+mt/g)%(mt/g);
a=k1*m+a;
m=m*mt/g;
}
if(!flag)
{
a=(a+m)%m;
if(a)
cout<<a<<endl;
else
cout<<m<<endl;
}
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 15245 | Accepted: 5018 |
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.
#include<cstdio>
#include<iostream>
using namespace std;
#define ll long long
ll g;
void exgcd(ll a,ll b,ll& x,ll& y)
{
if(b==0)
{
g=a;
x=1;
y=0;
}
else
{
exgcd(b,a%b,y,x);
y-=x*(a/b);
}
}
int main()
{
ll n,a,m,mt,att,k1,k2,c;
while(cin>>n)
{
int flag=0;
cin>>m>>a;
for(int i=1; i<n; i++)
{
cin>>mt>>att;
if(flag) continue;
c=att-a;
exgcd(m,mt,k1,k2);
if(c%g)
{
flag=1;
cout<<-1<<endl;
continue;
}
k1=k1*c/g;
k1=(k1+mt/g)%(mt/g);
a=k1*m+a;
m=m*mt/g;
}
if(!flag)
{
a=(a+m)%m;
if(a)
cout<<a<<endl;
else
cout<<m<<endl;
}
}
return 0;
}
相关文章推荐
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
- POJ 2891 Strange Way to Express Integers解线性同余方程组(中国剩余定理不互质版)
- poj 2891 Strange Way to Express Integers——使用中国剩余定理中处理某些方程模数不互质的方法
- poj 2891 Strange Way to Express Integers (中国剩余定理不互质)
- POJ 2891 Strange Way to Express Integers【模线性方程组】(中国剩余定理非互质套用)
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
- POJ 2891 Strange Way to Express Integers(非互质中国剩余定理)
- POJ2891 Strange Way to Express Integers 不互质中国剩余定理
- POJ_2891_Strange Way to Express Integers(非互质的中国剩余定理)
- Strange Way to Express Integers(中国剩余定理+不互质)
- POJ 2891 Strange Way to Express Integers 【中国剩余定理线性模方程合并(模板)】
- POJ 2891-Strange Way to Express Integers【中国剩余定理】
- 中国剩余定理小结 (互质,非互质) (poj 1006,hdu 3579)
- POJ 2891 Strange Way to Express Integers 中国剩余定理的一般情况
- POJ-2891 Strange Way to Express Integers 中国剩余定理
- poj 2891 Strange Way to Express Integers 模数不互素的中国剩余定理
- PKU 2891:Strange Way to Express Integers(中国剩余定理非互质)
- POJ 2891 Strange Way to Express Integers(中国剩余定理扩展)
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
- poj 2891 Strange Way to Express Integers 中国剩余定理