【POJ】2947 Widget Factory

列出同余方程,高斯消元。

无穷解的同时,可能出现无解,所以先判无穷解,再判无解。

最后枚举答案,还可能无解。

  1 #include<cstdio>

  2 #include<cstring>

  3 #include<algorithm>

  4 #define MAXN 330

  5 using namespace std;

  6 int n, m;

  7 int g[MAXN][MAXN], x[MAXN];

  8 int Day(char str[]) {

  9     if (strcmp(str, "MON") == 0)

 10         return 1;

 11     if (strcmp(str, "TUE") == 0)

 12         return 2;

 13     if (strcmp(str, "WED") == 0)

 14         return 3;

 15     if (strcmp(str, "THU") == 0)

 16         return 4;

 17     if (strcmp(str, "FRI") == 0)

 18         return 5;

 19     if (strcmp(str, "SAT") == 0)

 20         return 6;

 21     return 7;

 22 }

 23 int Gauss() {

 24     int r, c, i, j, k, tmp;

 25     for (c = k = 0; c < n; c++) {

 26         for (r = k; r < m; r++) {

 27             if (g[r][c])

 28                 break;

 29         }

 30         if (r < m) {

 31             if (r != k) {

 32                 for (i = c; i <= n; i++)

 33                     swap(g[r][i], g[k][i]);

 34             }

 35             for (i = k + 1; i < m; i++) {

 36                 if (g[i][c]) {

 37                     tmp = g[i][c];

 38                     for (j = c; j <= n; j++) {

 39                         g[i][j] = g[i][j] * g[k][c] - g[k][j] * tmp;

 40                         g[i][j] = (g[i][j] % 7 + 7) % 7;

 41                     }

 42                 }

 43             }

 44             k++;

 45         }

 46     }

 47     for (i = tmp = 0; i < m; i++) {

 48         for (j = 0; j < n; j++) {

 49             if (g[i][j])

 50                 break;

 51         }

 52         if (j == n) {

 53             if (g[i][n])

 54                 return 1;

 55             else if (i < n)

 56                 tmp = 1;

 57         }

 58     }

 59     if (tmp || m < n)

 60         return 0;

 61     for (i = n - 1; i >= 0; i--) {

 62         tmp = 0;

 63         for (j = i + 1; j < n; j++) {

 64             tmp += x[j] * g[i][j];

 65             tmp %= 7;

 66         }

 67         for (j = 3; j <= 9; j++) {

 68             if ((tmp + j * g[i][i]) % 7 == g[i][n]) {

 69                 x[i] = j;

 70                 break;

 71             }

 72             if (j > 9)

 73                 return 1;

 74         }

 75     }

 76     return 2;

 77 }

 78 int main() {

 79     int i, j, k, tmp;

 80     char st[MAXN], nd[MAXN];

 81     while (scanf("%d%d", &n, &m), n) {

 82         memset(g, 0, sizeof(g));

 83         for (i = 0; i < m; i++) {

 84             scanf("%d %s %s", &k, st, nd);

 85             while (k--) {

 86                 scanf("%d", &tmp);

 87                 g[i][tmp - 1]++;

 88             }

 89             g[i][n] = Day(nd) - Day(st) + 1;

 90         }

 91         for (i = 0; i < m; i++) {

 92             for (j = 0; j <= n; j++)

 93                 g[i][j] = (g[i][j] % 7 + 7) % 7;

 94         }

 95         tmp = Gauss();

 96         if (tmp == 0)

 97             puts("Multiple solutions.");

 98         else if (tmp == 1)

 99             puts("Inconsistent data.");

100         else {

101             printf("%d", x[0]);

102             for (i = 1; i < n; i++)

103                 printf(" %d", x[i]);

104             putchar('\n');

105         }

106     }

107     return 0;

108 }

你可能感兴趣的:(factory)