算法介绍:
如:
x=b1(mod m1);
x=b2(mod m2);
令m=[m1,m2](最小公倍数);
首先这个方程有解的充分必要条件是(m1,m2)|(b1-b2)(就是b1-b2能够整除m1,m2的最大公约数),此时方程仅有一个小于m的非负整数解,利用扩展欧几里得算法很容易得出:
式1=> x=b1+m1y1;
式2=> x=b2+m2y2;
联立可得: b1+m1y1=b2+m2y2,即m2y2-m1y1=b1-b2;因此小于m的非负整数解即为(b2+m2y2)%m;
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
poj 2891Strange Way to Express Integers 入门题
/************************************************************** Problem:poj 2891 User: youmi Language: C++ Result: Accepted Time:0MS Memory:712K ****************************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <stack> #include <set> #include <sstream> #include <cmath> #include <queue> #include <deque> #include <string> #include <vector> #define zeros(a) memset(a,0,sizeof(a)) #define ones(a) memset(a,-1,sizeof(a)) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d%d",&a,&b) #define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define scs(a) scanf("%s",a) #define sclld(a) scanf("%I64d",&a) #define pt(a) printf("%d ",a) #define ptlld(a) printf("%I64d ",a) #define rep0(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define rep_1(i,n) for(int i=n;i>=1;i--) #define rep_0(i,n) for(int i=n-1;i>=0;i--) #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define lson (step<<1) #define rson (lson+1) #define esp 1e-6 #define oo 0x3fffffff #define TEST cout<<"*************************"<<endl using namespace std; typedef long long ll; int n; ll ex_gcd(ll a,ll b,ll &x,ll &y) { if(b==0) { x=1,y=0; return a; } ll ans=ex_gcd(b,a%b,x,y); ll temp=x; x=y; y=temp-a/b*y; return ans; } void solve() { ll a,b,c,d; ll a1,r1,a2,r2;//线性方程p==r1(mod a1),p==r2(mod a2) ll x,y; sclld(a1); sclld(r1); int flag=1; rep1(i,n-1) { sclld(a2); sclld(r2); a=a1,b=a2,c=r2-r1;//a,b的值都为正数 d=ex_gcd(a,b,x,y); if(c%d!=0) flag=0; ll t=b/d;//如果b为负数,这里应写成 ll t=(ll)fabs(1.0*b/d); x=(x*(c/d)%t+t)%t; r1=a1*x+r1; a1=a*(b/d); } if(!flag) r1=-1; printf("%lld ",r1);//最后a1里保存的所有a1,a2,...an的最小公倍数,r1为满足所有线性方程的答案 } int main() { freopen("in.txt","r",stdin); while(~sc(n)) { solve(); } return 0; }
hdu 1573 X问题 http://acm.hdu.edu.cn/showproblem.php?pid=1573 坑点:x是正整数
/************************************************************** Problem:hdu 1573 User: youmi Language: C++ Result: Accepted Time:15MS Memory:1560K ****************************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <stack> #include <set> #include <sstream> #include <cmath> #include <queue> #include <deque> #include <string> #include <vector> #define zeros(a) memset(a,0,sizeof(a)) #define ones(a) memset(a,-1,sizeof(a)) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d%d",&a,&b) #define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define scs(a) scanf("%s",a) #define sclld(a) scanf("%I64d",&a) #define pt(a) printf("%d ",a) #define ptlld(a) printf("%I64d ",a) #define rep0(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define rep_1(i,n) for(int i=n;i>=1;i--) #define rep_0(i,n) for(int i=n-1;i>=0;i--) #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define lson (step<<1) #define rson (lson+1) #define esp 1e-6 #define oo 0x3fffffff #define TEST cout<<"*************************"<<endl using namespace std; typedef long long ll; int n,m; const int maxn=20; ll xz[maxn],yz[maxn]; ll ex_gcd(ll a,ll b,ll &x,ll &y) { if(b==0) { x=1,y=0; return a; } ll ans=ex_gcd(b,a%b,x,y); ll temp=x; x=y; y=temp-y*(a/b); return ans; } ll solve() { ll a,b,c,d,t; ll a1,a2,r1,r2; ll x,y; a1=xz[1],r1=yz[1]; ll ans=0; for(int i=2;i<=m;i++) { a2=xz[i],r2=yz[i]; a=a1,b=a2,c=r2-r1; d=ex_gcd(a,b,x,y); if(c%d!=0) return 0; t=b/d; x=(x*(c/d)%t+t)%t; r1=a1*x+r1; a1=a*(b/d); } if(r1==0) r1+=a1; //printf("xmin->%I64d lcm->%I64d ",r1,a1); if(n>=r1) ans=(n-r1)/a1+1; return ans; } int main() { //freopen("in.txt","r",stdin); int T_T; scanf("%d",&T_T); for(int kase=1;kase<=T_T;kase++) { sc2(n,m); rep1(i,m) sclld(xz[i]); rep1(i,m) sclld(yz[i]); ptlld(solve()); } return 0; }
hdu 3579 hello kiki http://acm.hdu.edu.cn/showproblem.php?pid=3579 坑点:positive integer
/************************************************************** Problem:hdu 3579 User: youmi Language: C++ Result: Accepted Time:0MS Memory:1564K ****************************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <stack> #include <set> #include <sstream> #include <cmath> #include <queue> #include <deque> #include <string> #include <vector> #define zeros(a) memset(a,0,sizeof(a)) #define ones(a) memset(a,-1,sizeof(a)) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d%d",&a,&b) #define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define scs(a) scanf("%s",a) #define sclld(a) scanf("%I64d",&a) #define pt(a) printf("%d ",a) #define ptlld(a) printf("%I64d ",a) #define rep0(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define rep_1(i,n) for(int i=n;i>=1;i--) #define rep_0(i,n) for(int i=n-1;i>=0;i--) #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define lson (step<<1) #define rson (lson+1) #define esp 1e-6 #define oo 0x3fffffff #define TEST cout<<"*************************"<<endl using namespace std; typedef long long ll; int m; const int maxn=20; ll xz[maxn],yz[maxn]; ll ex_gcd(ll a,ll b,ll &x,ll &y) { if(b==0) { x=1,y=0; return a; } ll ans=ex_gcd(b,a%b,x,y); ll temp=x; x=y; y=temp-y*(a/b); return ans; } ll solve() { ll a,b,c,d,t; ll a1,a2,r1,r2; ll x,y; a1=xz[1],r1=yz[1]; for(int i=2;i<=m;i++) { a2=xz[i],r2=yz[i]; a=a1,b=a2,c=r2-r1; d=ex_gcd(a,b,x,y); if(c%d!=0) return -1; t=b/d; x=(x*(c/d)%t+t)%t; r1=a1*x+r1; a1=a*(b/d); } if(r1==0) r1+=a1; return r1; } int main() { //freopen("in.txt","r",stdin); int T_T; scanf("%d",&T_T); for(int kase=1;kase<=T_T;kase++) { printf("Case %d: ",kase); sc(m); rep1(i,m) sclld(xz[i]); rep1(i,m) sclld(yz[i]); ptlld(solve()); } return 0; }
724C . Ray Tracing http://codeforces.com/problemset/problem/724/C 题解:http://www.cnblogs.com/Cydiater/p/5941359.html
很好的题,推荐
/************************************************************** Problem: User: youmi Language: C++ Result: Accepted Time: Memory: ****************************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") //#include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <stack> #include <set> #include <sstream> #include <cmath> #include <queue> #include <deque> #include <string> #include <vector> #define zeros(a) memset(a,0,sizeof(a)) #define ones(a) memset(a,-1,sizeof(a)) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d%d",&a,&b) #define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c) #define scs(a) scanf("%s",a) #define sclld(a) scanf("%I64d",&a) #define pt(a) printf("%d ",a) #define ptlld(a) printf("%I64d ",a) #define rep(i,from,to) for(int i=from;i<=to;i++) #define irep(i,to,from) for(int i=to;i>=from;i--) #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define lson (step<<1) #define rson (lson+1) #define eps 1e-6 #define oo 1e16 #define TEST cout<<"*************************"<<endl const double pi=4*atan(1.0); using namespace std; typedef long long ll; template <class T> inline void read(T &n) { char c; int flag = 1; for (c = getchar(); !(c >= '0' && c <= '9' || c == '-'); c = getchar()); if (c == '-') flag = -1, n = 0; else n = c - '0'; for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0'; n *= flag; } ll Pow(ll base, ll n, ll mo) { ll res=1; while(n) { if(n&1) res=res*base%mo; n>>=1; base=base*base%mo; } return res; } //*************************** ll n,m,k,sum; ll xx,yy; const int maxn=100000+10; const ll mod=1000000007; ll gcd(ll a,ll b) { if(b==0) return a; return gcd(b,a%b); } ll ex_gcd(ll a,ll b,ll &x,ll &y) { if(b==0) { x=1,y=0; return a; } ll ans=ex_gcd(b,a%b,x,y); ll temp=x; x=y; y=temp-a/b*y; return ans; } ll solve(ll xxx,ll yyy) { ll a,b,c,d; ll a1=2*n,a2=2*m,r1=xxx,r2=yyy; ll x,y; int flag=1; { a=a1,b=a2,c=r2-r1; d=ex_gcd(a,b,x,y); if(c%d!=0) flag=0; ll t=(b/d); x=(x*(c/d)%t+t)%t; r1=a1*x+r1; a1=a*(b/d); } if(!flag||r1<0) return oo; return r1; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif while(~scanf("%I64d%I64d%I64d",&n,&m,&k)) { ll g=gcd(n,m); sum=n*m/g; rep(i,1,k) { read(xx); read(yy); ll ans=oo; ans=min(ans,solve(xx,yy)); ans=min(ans,solve(-xx,yy)); ans=min(ans,solve(xx,-yy)); ans=min(ans,solve(-xx,-yy)); if(ans>sum) puts("-1"); else printf("%I64d ",ans); } } return 0; }