TLE:csu 1016 最小公倍数

求删改方案总共多少种,预料之中的超时,因为所有提交的22次中有12次超时。

 1 # include <stdio.h>
2
3 # define MAXN 205
4
5 int gcd(int a, int b);
6
7 int m[MAXN];
8 int f[MAXN][MAXN];
9
10 int main()
11 {
12 int i, j, T, n, lcm, tmp, cnt;
13
14 scanf("%d", &T);
15 while (T--)
16 {
17 cnt = 0;
18 lcm = 1;
19 scanf("%d", &n);
20 for (i = 1; i <= n; ++i)
21 {
22 scanf("%d", &m[i]);
23 lcm /= gcd(lcm, m[i]);
24 lcm *= m[i];
25 }
26 for (i = 1; i < n; ++i)
27 for (j = i+1; j <= n; ++j)
28 f[i][j] = f[j][i] = gcd(m[i], m[j]);
29 for (i = 1; i <= n; ++i)
30 {
31 for (tmp = m[i], j = 1; j <= n; ++j)
32 if(i != j && 1 == f[i][j])
33 tmp *= m[j];
34 if (tmp == lcm)
35 {
36 for (j = 1; j <= n; ++j)
37 if (i != j && 1 == f[i][j])
38 f[j][i] = f[i][j] = -1;
39 ++cnt;
40 }
41 }
42 printf("%d\n", cnt);
43 }
44
45 return 0;
46 }
47
48 int gcd(int x, int y)
49 {
50 while (x != y)
51 {
52 if (x < y) y = y - x;
53 else x = x - y;
54 }
55 return x;
56 }

// 待续

你可能感兴趣的:(su)