exgcd简单题
首先容易想到先枚举m,然后判断。
至于判断只需用联立方程,先给ci-1
\(c_i+p_i \times t \equiv x \pmod m\)
\(c_j+p_j \times t \equiv x \pmod m\)
即
\(c_i-c_j=(p_i-p_j) \times t+my\)
然后再t将调到合适范围
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int N=30;
typedef long long ll;
ll c[N],p[N],l[N],m,a,b,tt,y,d,z;
int n;
void exgcd(ll aa,ll bb,ll &xx,ll &yy){
if (!bb) {
d=aa;
xx=1ll;
return;
}
ll t=aa/bb;
exgcd(bb,aa%bb,yy,xx);
yy-=t*xx;
}
bool check(ll M){
fo(i,1,n) {
fo(j,i+1,n) {
if (!l[i] || !l[j] || p[i]==p[j]) continue;
a=p[j]-p[i];
b=M;
exgcd(a,b,tt,y);
tt%=(b/d);
if ((c[i]-c[j])%d==0) {
tt=(c[i]-c[j])/d*tt;
z=(b/d);
if (z<0) z=-z;
tt=(tt%z+z)%z;
if (tt<=min(l[i],l[j])) return 0;
}
}
}
return 1;
}
int main(){
// freopen("data.in","r",stdin);
scanf("%d",&n);
ll ans=0;
fo(i,1,n) {
scanf("%lld %lld %lld",&c[i],&p[i],&l[i]);
c[i]--;
ans=max(c[i]+1,ans);
}
for (m=max(ans,(ll)n);;m++){
if (check(m)) break;
}
printf("%lld",m);
return 0;
}
标签:include,pmod,times,野人,NOI2002,荒岛,equiv
From: https://www.cnblogs.com/ganking/p/16829733.html