您的位置:首页 > 其它

BZOJ1407: [Noi2002]Savage

2018-01-16 10:38 399 查看

1407: [Noi2002]Savage

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 2357 Solved: 1056
[Submit][Status][Discuss]

Description

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}

const int INF = 0x3f3f3f3f;
const int MAXN = 15;

int c[MAXN + 5], p[MAXN + 5], l[MAXN + 5], n, ma;

void exgcd(int a, int b, int &x, int &y)
{
if(!b){x = 1;y = 0;return;}
exgcd(b, a % b, y, x);
y -= a / b * x;
return;
}
int gcd(int a, int b)
{
return !b ? a : gcd(b, a % b);
}

bool judge(int m)
{
for(register int i = 1;i <= n;++ i) for(register int j = i + 1;j <= n;++j)
{
//先求方程 (pj - pi)x + my = ci - cj的最小正整数解x
int P = p[j] - p[i], C = c[i] - c[j], g = gcd(P, m), M = m, x = 0, y = 0;
if(C % g != 0) continue;
P /= g, C /= g, M /= g;
exgcd(P, M, x, y);
M = M < 0 ? -M : M;
x = (x * C % M + M) % M;
if(!x) x += M;
if(x <= min(l[i], l[j])) return 0;
}
return 1;
}

int main()
{
read(n);
for(register int i = 1;i <= n;++ i) read(c[i]), read(p[i]), read(l[i]), ma = max(ma, c[i]);
for(register int i = ma;;++ i)
if(judge(i)){printf("%d", i);return 0;}
}


BZOJ1047
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: