题意分析
观察数据范围,$1\leq n\leq15,1\leq m\leq10^6$,数据范围不大,因此可以考虑枚举 $m$。
两个野人 $i,j$ 在第 $x$ 年相遇即:
\[c_i+x\cdot p_i\equiv c_j+x\cdot p_j\pmod m\]同时,$x$ 需要满足 $x\leq\min(l_i,l_j)$。
若没有野人相遇,则 $n^2$ 个同余方程均没有满足条件的解,使用 exgcd 判断即可。
注意判断 $x\leq\min(l_i,l_j)$ 时,应当取 $x$ 的最小正整数解 $x_{\min}$ 来判断,因为相遇多次只要有一次就不应出现。
可以在 $\left[\max\limits_{i=1}^n c_i,10^6\right]$ 中枚举 $m$。
AC 代码
时间复杂度:$\mathcal O(mn^2\log c)$。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
constexpr const int N=15;
int n,c[N+1],p[N+1],l[N+1];
int gcd(int a,int b){
while(b){
int tmp=a;
a=b;
b=tmp%b;
}
return a;
}
void exgcd(int a,int &x,int b,int &y){
if(!b){
x=1;
y=0;
return;
}
int tmp;
exgcd(b,tmp,a%b,x);
y=tmp-a/b*x;
}
bool check(int m){
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
int A=p[i]-p[j],x0,B=m,y0,C=c[j]-c[i];
int pl=gcd(A,B);
if(C%pl){
continue;
}
exgcd(A,x0,B,y0);
int w=C/pl;
x0*=w,y0*=w;
int xMin,yMax,deltaB=abs(B/pl);
xMin=x0%deltaB;
if(xMin<0){
xMin+=deltaB;
}
if(xMin<=min(l[i],l[j])){
return false;
}
}
}
return true;
}
int main(){
/*freopen("test.in","r",stdin);
freopen("test.out","w",stdout);*/
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
int maxC=1;
for(int i=1;i<=n;i++){
cin>>c[i]>>p[i]>>l[i];
maxC=max(maxC,c[i]);
}
for(int m=maxC;;m++){
if(check(m)){
cout<<m<<'\n';
break;
}
}
cout.flush();
/*fclose(stdin);
fclose(stdout);*/
return 0;
}