做了一天多了吧。。。。先是学习大牛们的优化版的程序。。。。不知道哪里写错了。。。就是不过。。。最后还是自己按线性代数的解方程一步步来的。。。。AC是AC了。。效率不是很高的。。。。。。今天网络赛就耽误了一下。。。现在终于写好了。。。泪水呀。。。。
好像是discuss里提到多解的时候如果不是仔细判断的也有可能是无解的情况。。。。这个貌似要注意。。不过我这样直接解的就飘过了。。。。
#include<iostream> #include<cstdio> #include<string> #include<map> using namespace std; map<string, int> q; int a[310][310], ans[310], n, m; inline void init1() { q["MON"] = 0, q["TUE"] = 1, q["WED"] = 2, q["THU"] = 3; q["FRI"] = 4, q["SAT"] = 5, q["SUN"] = 6; } int ex_gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } int d = ex_gcd(b, a % b, x, y), t = x; x = y, y = t - a / b*y; return d; } int gauss() { int i, j, k, t, x, y; for (i = 1, j = 1; i <= m && j <= n; i++, j++) { k = i; while (k <= m && a[k][j] == 0) k++; if (k == m + 1) { i--; continue; } if (k != i) for (t = 1; t <= n + 1; t++) swap(a[k][t], a[i][t]); for (k = 1; k <= m; k++) if (k != i && a[k][j] != 0) { x = a[i][j], y = a[k][j]; for (t = 1; t <= n + 1; t++) { a[k][t] = a[k][t] * x - a[i][t] * y; a[k][t] = (a[k][t] % 7 + 7) % 7; } } } int d, sum = 0; for (i = 1; i <= m; i++) { for (j = i; j <= n && a[i][j] == 0; j++); if (j >= n + 1) { if (a[i][n + 1] == 0) sum++; else return -1; } else { d = ex_gcd(a[i][j], 7, x, y); if (a[i][n + 1] % d != 0) return -1; ans[j] = (x * (a[i][n + 1] / d) % 7 + 7) % 7; while (ans[j] < 3) ans[j] += 7; while (ans[j] > 9) ans[j] -= 7; } } if (m - sum < n) return 1; return 0; } int main() { int i, ret, k, s; string str1, str2; init1(); while (scanf("%d%d", &n, &m) && (n || m)) { memset(a, 0, sizeof (a)); for (i = 1; i <= m; i++) { cin >> s >> str1 >> str2; a[i][n + 1] = (q[str2] - q[str1] + 8) % 7; while (s--) { scanf("%d", &k); a[i][k] += 1, a[i][k] %= 7; } } ret = gauss(); if (ret == -1) printf("Inconsistent data.\n"); else if (ret == 1) printf("Multiple solutions.\n"); else { for (i = 1; i < n; i++) printf("%d ", ans[i]); printf("%d\n", ans[n]); } } return 0; }