bzoj1407: [Noi2002]Savage

传送门:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407

思路:因为M<=10^6,所以可以枚举M进行check。

山洞数量满足要求,就是对于任何两个野人组成的同余方程最小解大于寿命短的野人的寿命,或者没有解。


代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define abs(a) (a<0?-a:a)
const int maxn=1000000;
using namespace std;
typedef long long ll;
int n,c[20],p[20],l[20],st;


ll exgcd(ll a,ll b,ll &x,ll &y){
	if (b==0){x=1,y=0;return a;}
	int z=exgcd(b,a%b,x,y),t=x;
	x=y,y=t-a/b*y;return z;
}

bool check(int m){
	for (int i=1;i<=n-1;i++)
		for (int j=i+1;j<=n;j++){
			ll A=p[i]-p[j],C=c[j]-c[i],x,y,t=exgcd(A,m,x,y);
			if (C%t) continue;
			x=x*C/t,x=x%(m/t);
            if (x<0) x+=abs(m/t);
			//C/=t,m/=t,x=(x*C%m+m)%m;
			if (x<=min(l[i],l[j])) return 0;
		}
	return 1;
}

int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d%d%d",&c[i],&p[i],&l[i]),st=max(st,c[i]);
	for (int i=st;i<=maxn;i++) if (check(i)){printf("%d
",i);return 0;}
	return 0;
}

原文地址:https://www.cnblogs.com/thythy/p/5493525.html