Description
The factory is currently in a state of complete chaos: recently, the factory has been bought by a new owner, and the new director has fired almost everyone. The new staff know almost nothing about building widgets, and it seems that no one remembers how many days are required to build each diofferent type of widget. This is very embarrassing when a client orders widgets and the factory cannot tell the client how many days are needed to produce the required goods. Fortunately, there are records that say for each widgeteer the date when he started working at the factory, the date when he was fired and what types of widgets he built. The problem is that the record does not say the exact date of starting and leaving the job, only the day of the week. Nevertheless, even this information might be helpful in certain cases: for example, if a widgeteer started working on a Tuesday, built a Type 41 widget, and was fired on a Friday,then we know that it takes 4 days to build a Type 41 widget. Your task is to figure out from these records (if possible) the number of days that are required to build the different types of widgets.
Input
4 WED SUN
13 18 1 13
Note that the widgeteers work 7 days a week, and they were working on every day between their first and last day at the factory (if you like weekends and holidays, then do not become a widgeteer!).
The input is terminated by a test case with n = m = 0 .
Output
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std; int inv[]; struct MOD7 {
int val;
MOD7() {}
MOD7(int val): val((val + ) % ) {}
MOD7 operator + (const MOD7 &rhs) const {
return MOD7(val + rhs.val);
}
MOD7 operator - (const MOD7 &rhs) const {
return MOD7(val - rhs.val);
}
MOD7 operator * (const MOD7 &rhs) const {
return MOD7(val * rhs.val);
}
MOD7 operator / (const MOD7 &rhs) const {
return MOD7(val * inv[rhs.val]);
}
void operator -= (const MOD7 &rhs) {
val = (val - rhs.val + ) % ;
}
bool isZero() {
return val == ;
}
void inc() {
val = (val + ) % ;
}
void print() {
if(val < ) printf("%d", val + );
else printf("%d", val);
}
}; map<string, int> mymap; void init() {
for(int i = ; i < ; ++i) {
inv[i] = ;
for(int j = ; j < ; ++j) inv[i] = (inv[i] * i) % ;
}
mymap["MON"] = ;
mymap["TUE"] = ;
mymap["WED"] = ;
mymap["THU"] = ;
mymap["FRI"] = ;
mymap["SAT"] = ;
mymap["SUN"] = ;
} const int MAXN = ; MOD7 mat[MAXN][MAXN];
int n, m; int guess_eliminatioin() {
int rank = ;
for(int i = , t = ; i < m && t < n; ++i, ++t) {
int r = i;
for(int j = i + ; j < m; ++j)
if(mat[r][t].val < mat[j][t].val) r = j;
if(mat[r][t].isZero()) { --i; continue;}
else ++rank;
if(r != i) for(int j = ; j <= n; ++j) swap(mat[i][j], mat[r][j]);
for(int j = n; j >= t; --j)
for(int k = i + ; k < m; ++k) mat[k][j] -= mat[i][j] * mat[k][t] / mat[i][t];
}
for(int i = rank; i < m; ++i)
if(!mat[i][n].isZero()) return -;
if(rank < n) return ;
for(int i = n - ; i >= ; --i) {
for(int j = i + ; j < n; ++j)
mat[i][n] -= mat[j][n] * mat[i][j];
mat[i][n] = mat[i][n] / mat[i][i];
}
return ;
} int main() {
init();
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
memset(mat, , sizeof(mat));
int t, p;
string s1, s2;
for(int i = ; i < m; ++i) {
cin>>t>>s1>>s2;
while(t--) {
scanf("%d", &p);
mat[i][p - ].inc();
}
mat[i][n] = MOD7(mymap[s2] - mymap[s1] + );
}
int result = guess_eliminatioin();
if(result == -) puts("Inconsistent data.");
else if(result == ) puts("Multiple solutions.");
else {
for(int i = ; i < n - ; ++i) {
mat[i][n].print();
putchar(' ');
}
mat[n - ][n].print();
puts("");
}
}
}