https://blog.csdn.net/qq_33266889/article/details/54718626
poj 2891:求方程最小值
//解一元线性同余方程组。
#include <iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll ex_gcd(ll a,ll b,ll& x,ll& y)
{
if(b==0)
{
x=1,y=0;
return a;
}
ll r,tx,ty;
r=ex_gcd(b,a%b,tx,ty);
x=ty;
y=tx-a/b*ty;
return r;
}
int main()
{
ll i,n,a1,r1,a2,r2,ans,a,b,c,d,x0,y0;
while(scanf("%lld",&n)!=EOF){
bool flag = 1;
scanf("%lld%lld",&a1,&r1);
for( i=1;i<n;i++){
scanf("%lld%lld",&a2,&r2);
a = a1;
b = a2;
c = r2-r1;
ll d = ex_gcd(a,b,x0,y0);
if(c%d!=0){
flag = 0;
}
int t = b/d;
printf("%lld\n",d);
printf("%lld\n",x0);
x0 = (x0*(c/d)%t+t)%t;//保证x0为正
r1 = a1*x0 + r1;
a1 = a1*(a2/d);
}
if(!flag){
puts("-1");
continue;
}
printf("%lld\n",r1);
}
return 0;
}
hdu1573:求解的大小小于n的解的个数
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N,m;
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x = 1; y = 0;
return a;
}
ll d = exgcd(b,a%b,x,y);
ll tmp = x;
x = y;
y = tmp - a/b*x;
return d;
}
ll solve(ll A[],ll B[],ll num){
bool ok=true;
ll a1,b1,a2,b2,k1,kk,d,c,t,x,y;
a1 = A[0] , b1 = B[0];
for(ll i=1;i<num;i++){
a2 = A[i] , b2 = B[i];
c = b2 - b1;
d = exgcd(a1,a2,x,y);
if( c % d ){
ok = false; break;
}
k1 = x*c/d;
t = a2/d;
kk = (k1%t+t)%t;
b1 = b1 + a1*kk;
a1 = a1*a2/d;
}
if(!ok || b1>N) return 0;
ll ans = (N-b1)/a1+1;
if(b1==0) ans--;
return ans;
}
int main(){
ll T;
ll A[100],B[100];
scanf("%lld",&T);
while(T--){
scanf("%lld%lld",&N,&m);
for(int i=0;i<m;i++) scanf("%lld",&A[i]);
for(int i=0;i<m;i++) scanf("%lld",&B[i]);
ll ans = solve(A,B,m);
cout<<ans<<endl;
}
return 0;
}