bzoj 1407 扩展欧几里德
思路:枚举洞穴个数,用扩展欧几里德暴力判断没两个人的周期。
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define pii pair<int, int> using namespace std; const int N = 15 + 7; const int M = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 +7; LL n, c[N], p[N], l[N]; LL exgcd(LL a, LL b, LL &x, LL &y) { if(!b) { x = 1; y = 0; return a; } else { LL gcd, t; gcd = exgcd(b, a % b, x, y); t = x; x = y; y = t - (a / b) * y; return gcd; } } bool check(LL m) { for(int i = 1; i < n; i++) { for(int j = i + 1; j <= n; j++) { LL a = p[i] - p[j], b = m, val = c[j] - c[i]; LL x, y; LL gcd = exgcd(a, b, x, y); if(val % gcd) continue; x *= val / gcd; x %= (b / gcd); if(x < 0) x += abs(b / gcd); if(x <= min(l[i], l[j])) return false; } } return true; } int main() { scanf("%lld", &n); LL mx = 1; for(int i = 1; i <= n; i++) { scanf("%lld%lld%lld", &c[i], &p[i], &l[i]); mx = max(mx, c[i]); } for(int i = mx; ; i++) { if(check(i)) { printf("%d\n", i); break; } } return 0; } /* */