http://poj.org/problem?id=2947
大致题意:
有n种装饰物,m个已知条件,每个已知条件的描述如下:
p start end
a1,a2......ap (1<=ai<=n)
第一行表示从星期start到星期end一共生产了p件装饰物(工作的天数为end-start+1+7*x,加7*x是因为它可能生产很多周),第二行表示这p件装饰物的种类(可能出现相同的种类,即ai=aj)。规定每件装饰物至少生产3天,最多生产9天。问每种装饰物需要生产的天数。如果没有解,则输出“Inconsistent data.”,如果有多解,则输出“Multiple solutions.”,如果只有唯一解,则输出每种装饰物需要生产的天数。
思路:依旧是高斯消元。这里的m个已知条件就是m个方程组,n为n个未知数。设生产第i个装饰物需要的天数为x(i),生产第i个装饰物的件数是A(i)件,那么对于每一个方程有x(1)*A(1) +x(2)*A(2) + ... x(n)*A(n)= end-start+1+7*x。
注意:
因为是模线性方程,形成增广阵时要不断的模7.
注意模7环上的求解方法.
当有唯一解时要求每件装饰物的生产天数为[3,9]。所以要判断是否满足条件,否则加7.
#include <stdio.h> #include <iostream> #include <map> #include <set> #include <stack> #include <vector> #include <math.h> #include <string.h> #include <queue> #include <string> #include <stdlib.h> #include <algorithm> #define LL long long #define _LL __int64 #define eps 1e-12 #define PI acos(-1.0) using namespace std; int equ,var; int x[310],a[310][310]; int trans(char s[]) { if(strcmp(s,"MON") == 0) return 1; else if(strcmp(s,"TUE") == 0) return 2; else if(strcmp(s,"WED") == 0) return 3; else if(strcmp(s,"THU") == 0) return 4; else if(strcmp(s,"FRI") == 0) return 5; else if(strcmp(s,"SAT") == 0) return 6; else if(strcmp(s,"SUN") == 0) return 7; } void init() { memset(x,0,sizeof(x)); memset(a,0,sizeof(a)); } int gcd(int a, int b) { if(b == 0) return a; return gcd(b,a%b); } int lcm(int a, int b) { return a/gcd(a,b)*b; } int Gauss() { int row,col,max_r,i,j,tmp; row = 0; col = 0; while(row < equ && col < var) { max_r = row; for(i = row+1; i < equ; i++) { if(abs(a[i][col]) > abs(a[max_r][col])) //加abs max_r = i; } if(max_r != row) { for(j = col; j <= var; j++) swap(a[row][j],a[max_r][j]); } if(a[row][col] == 0) { col++; continue; } for(i = row+1; i < equ; i++) { if(a[i][col] == 0) continue; //加判断条件 int Lcm = lcm(abs(a[row][col]), abs(a[i][col])); //加abs int tb = Lcm/a[row][col], ta = Lcm/a[i][col]; if(a[i][col] * a[row][col] < 0) tb = -tb; //符号互异时tb取负号 for(j = col; j <= var; j++) a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%7+7)%7; //模7 } row++; col++; } for(i = row; i < equ; i++) if(a[i][var] != 0) return -1; if(var - row > 0) return var - row; for(i = var-1; i >= 0; i--) { tmp = a[i][var]; for(j = i+1; j < var; j++) tmp = ( (tmp-x[j]*a[i][j])%7+7 )%7; //模7 while(tmp % a[i][i] != 0) tmp += 7; x[i] = tmp/a[i][i]%7; } return 0; } int main() { int n,m; int num,tmp; char start[5],end[5]; while(~scanf("%d %d",&n,&m)) { if(n == 0 && m == 0) break; init(); for(int i = 0; i < m; i++) { scanf("%d %s %s",&num,start,end); int s = trans(start); int e = trans(end); a[i][n] = ( (e-s+1)%7+7)%7; for(int j = 0; j < num; j++) { scanf("%d",&tmp); tmp--; a[i][tmp] += 1; a[i][tmp] %= 7; //模7 } } equ = m; var = n; int res = Gauss(); if(res == -1) printf("Inconsistent data.\n"); else if(res > 0 ) printf("Multiple solutions.\n"); else { for(int i = 0; i < var-1; i++) { if(x[i] < 3) x[i] += 7; printf("%d ",x[i]); } if(x[var-1] < 3) x[var-1] += 7; printf("%d\n",x[var-1]); } } return 0; }