扩展欧几里德第二题~
这个题真是搞了好长时间才懂啊~~
题目大意:
有一个数mod ri 等于ai ,求这个数,若求不出来输出“-1”。
解题思路:
对于 x=r1(mod a1)
x=r2(mod a2)
相当于解不定方程:x*a1+y*a2=r2-r1
先求解方程:x*a1+y*a2=r2-r1=gcd(a1,a2)
得出解x,则方程x*a1+y*a2=r2-r1的解x0=x*(r2-r1)/gcd(a1,a2)=x*c/d
令s=a2/d,那么x0的最小解为:x0=(x0%s+s)%s即可得出解x=r1+x0*a1
然后将x赋值给r1,lcm(a1,a2)赋值给a1,继续求解。直到最后
下面是代码:
- #include <set>
- #include <map>
- #include <queue>
- #include <math.h>
- #include <vector>
- #include <string>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <iostream>
- #include <algorithm>
- #define eps 1e-6
- #define pi acos(-1.0)
- #define inf 107374182
- #define inf64 1152921504606846976
- #define lc l,m,tr<<1
- #define rc m + 1,r,tr<<1|1
- #define iabs(x) ((x) > 0 ? (x) : -(x))
- #define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
- #define clearall(A, X) memset(A, X, sizeof(A))
- #define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
- #define memcopyall(A, X) memcpy(A , X ,sizeof(X))
- #define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
- #define min( x, y ) ( ((x) < (y)) ? (x) : (y) )
- using namespace std;
- long long exgcd(long long m,long long n,long long &x,long long &y)
- {
- long long x1,y1,x0,y0;
- x0=1; y0=0;
- x1=0; y1=1;
- x=0; y=1;
- long long r=m%n;
- long long q=(m-r)/n;
- while(r)
- {
- x=x0-q*x1; y=y0-q*y1;
- x0=x1; y0=y1;
- x1=x; y1=y;
- m=n; n=r; r=m%n;
- q=(m-r)/n;
- }
- return n;
- }
- int main()
- {
- int n;
- long long a1,r1,a2,r2,c,d,x,y;
- bool flat;
- while(scanf("%d",&n)!=EOF)
- {
- flat=false;
- scanf("%lld%lld",&a1,&r1);
- for(int i=1;i<n;i++)
- {
- scanf("%lld%lld",&a2,&r2);
- if(flat)continue;
- c=r2-r1;
- d=exgcd(a1,a2,x,y);
- if(c%d)
- {
- flat=true;
- continue;
- }
- x*=c/d;
- a2/=d;
- x=(x%a2+a2)%a2;
- r1=r1+x*a1;
- a1*=a2;
- }
- if(flat)puts("-1");
- else printf("%lld\n",r1);
- }
- return 0;
- }
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define iabs(x) ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define min( x, y ) ( ((x) < (y)) ? (x) : (y) )
using namespace std;
long long exgcd(long long m,long long n,long long &x,long long &y)
{
long long x1,y1,x0,y0;
x0=1; y0=0;
x1=0; y1=1;
x=0; y=1;
long long r=m%n;
long long q=(m-r)/n;
while(r)
{
x=x0-q*x1; y=y0-q*y1;
x0=x1; y0=y1;
x1=x; y1=y;
m=n; n=r; r=m%n;
q=(m-r)/n;
}
return n;
}
int main()
{
int n;
long long a1,r1,a2,r2,c,d,x,y;
bool flat;
while(scanf("%d",&n)!=EOF)
{
flat=false;
scanf("%lld%lld",&a1,&r1);
for(int i=1;i<n;i++)
{
scanf("%lld%lld",&a2,&r2);
if(flat)continue;
c=r2-r1;
d=exgcd(a1,a2,x,y);
if(c%d)
{
flat=true;
continue;
}
x*=c/d;
a2/=d;
x=(x%a2+a2)%a2;
r1=r1+x*a1;
a1*=a2;
}
if(flat)puts("-1");
else printf("%lld\n",r1);
}
return 0;
}