poj2947 Widget Factory
模意义下整数高斯消元
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, m, k, uu, a[305][305];
char ss[15];
int getDay(){
if(ss[0]=='M') return 1;
if(ss[0]=='T' && ss[1]=='U') return 2;
if(ss[0]=='W') return 3;
if(ss[0]=='T' && ss[1]=='H') return 4;
if(ss[0]=='F') return 5;
if(ss[0]=='S' && ss[1]=='A') return 6;
if(ss[0]=='S' && ss[1]=='U') return 7;
}
int gauss(){
int hmn=1;
for(int i=1; i<=n; i++){
int maxi=0;
for(int j=hmn; j<=m; j++)
if(a[j][i]){
maxi = j;
break;
}
if(maxi){
swap(a[maxi], a[hmn]);
for(int j=hmn+1; j<=m; j++)
if(a[j][i]){
int bei1=a[hmn][i];
int bei2=a[j][i];
for(int l=i; l<=n+1; l++)
a[j][l] = ((a[j][l] * bei1 - a[hmn][l] * bei2) % 7 + 7) % 7;
}
hmn++;
}
}
for(int i=hmn; i<=m; i++)
if(a[i][n+1])
return -1;
if(hmn<=n) return 1;
for(int i=n; i>=1; i--){
for(int j=i+1; j<=n; j++){
a[i][n+1] = ((a[i][n+1] - a[j][n+1] * a[i][j])%7 + 7) % 7;
a[i][j] = 0;
}
while(a[i][n+1]%a[i][i]) a[i][n+1] += 7;
a[i][n+1] /= a[i][i];
a[i][i] = 1;
if(a[i][n+1]<=2) a[i][n+1] += 7;
}
return 0;
}
int main(){
while(scanf("%d %d", &n, &m)!=EOF){
if(!n && !m) break;
memset(a, 0, sizeof(a));
for(int i=1; i<=m; i++){
scanf("%d %s", &k, ss);
int tmp1=getDay();
scanf("%s", ss);
a[i][n+1] = (getDay()-tmp1+1+7) % 7;
while(k--){
scanf("%d", &uu);
a[i][uu] = (a[i][uu] + 1) % 7;
}
}
int re=gauss();
if(re==-1) printf("Inconsistent data.\n");
else if(re==1) printf("Multiple solutions.\n");
else{
for(int i=1; i<n; i++)
printf("%d ", a[i][n+1]);
printf("%d\n", a[n][n+1]);
}
}
return 0;
}
拙いものと思えども、
その手に握る其れこそが、
いつか幻想を生んでいく。