poj2769

简单题,但容易超时,不要每次memset,而是记录b数组那些位被修改过,记录在set中,然后每次把set中在b里的对应位改回0即可。

View Code
#include <stdio.h>

#define maxn 305
#define maxx 1000005

int n, f[maxn], tot, set[maxn];
int b[maxx];

void input()
{
int i;
scanf(
"%d", &n);
for (i = 0; i < n; i++)
scanf(
"%d", &f[i]);
}

void cover()
{
int i;
for (i = 0; i < tot; i++)
b[
set[i]] = 0;
}

int ok(int a)
{
int i;
tot
= 0;
for (i = 0; i < n; i++)
if (b[f[i] % a])
{
cover();
return 0;
}
else
{
b[f[i]
% a] = 1;
set[tot++] = f[i] % a;
}
cover();
return 1;
}

int main()
{
int t;
int i;
//freopen("D:\\t.txt", "r", stdin);
scanf("%d", &t);
while (t--)
{
input();
for (i = n; i < maxx; i++)
{
if (ok(i))
{
printf(
"%d\n", i);
break;
}
}
}
return 0;
}
原文地址:https://www.cnblogs.com/rainydays/p/2034003.html