http://poj.org/problem?id=2947
题意:
工人生产不同的部件需要不同的时间,最少需要3天最多需要9天。
给出n种部件和m条记录,每条记录包括该工人生产部件的总数k和他开始生产时间和结束时间(只给出是周几,不给出具体时间),之后给出这K个部件跟的所属的种类。最后求出生产这n种部件分别所需要的时间。
思路:
高斯消元:
例子来讲
首先我们能够列出方程
(x + y)%7 = 4;
(2*x + y)%7 = 5;
(x + 2*y)%7 = 0;
这里直接套高斯消元模板就行,关键是处理%7的情况,我们先按[0,6]计算结果最后再处理到[3,9];
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string> #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a,b) (a) > (b)? (b):(a) #define Max(a,b) (a) > (b)? (a):(b) #define ll long long #define inf 0x7f7f7f7f #define MOD 100000007 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 100007 #define N 307 using namespace std; //freopen("din.txt","r",stdin); int a[N][N],X[N]; int equ,var; int find(char *s){ if (!strcmp(s,"MON")) return 1; if (!strcmp(s,"TUE")) return 2; if (!strcmp(s,"WED")) return 3; if (!strcmp(s,"THU")) return 4; if (!strcmp(s,"FRI")) return 5; if (!strcmp(s,"SAT")) return 6; return 7; } int cha(char *s1,char *s2){ return (find(s2) - find(s1) + 1 + 7)%7; } int GCD(int x,int y){ if (y == 0) return x; return GCD(y,x%y); } int LCM(int x,int y){ return x/GCD(x,y)*y; } void d() { int i,j; for (i = 0; i < equ; ++i){ for (j = 0; j < var + 1; ++j) printf("%d ",a[i][j]); puts(""); } } int Guass(){ int i,j,k,col; CL(X,0); for (k = 0,col = 0; k < equ && col < var; ++k,++col){ int max_r = k; for (i = k + 1; i < equ; ++i){ if (iabs(a[i][col]) > iabs(a[max_r][col])) max_r = i; } if (max_r != k){ for (i = k; i < var + 1; ++i) swap(a[k][i],a[max_r][i]); } if (a[k][col] == 0){ k--; continue; } for (i = k + 1; i < equ; ++i){ //注意处理不等于0 的 if (a[i][col] != 0){ int lcm = LCM(iabs(a[k][col]),iabs(a[i][col]));//注意这里取绝对值 int ta = lcm/iabs(a[i][col]); int tb = lcm/iabs(a[k][col]); if (a[i][col]*a[k][col] < 0) tb = -tb; for (j = col; j < var + 1; ++j){ a[i][j] = ((ta*a[i][j])%7 - (tb*a[k][j])%7 + 7)%7; } } } } for (i = k; i < equ; ++i){ if (a[i][col]%7 != 0) return -1; } if (k < var) return var - k; else { for (i = var - 1; i >= 0; --i){ int tmp = a[i][var]; for (j = i + 1; j < var; ++j){ if (a[i][j] != 0) tmp -= a[i][j]*X[j]; } while (tmp%a[i][i] != 0) tmp += 7; X[i] = tmp/a[i][i]; if (X[i] < 3){ while (X[i] < 3) X[i] += 7; } else if (X[i] > 9){ while (X[i] > 9) X[i] -= 7; } } } return 0; } int main(){ //freopen("din.txt","r",stdin); int i,k; char s1[10],s2[10]; while (~scanf("%d%d",&var,&equ)) { if (!var && !equ) break; CL(a,0); //构造增广矩阵 for (i = 0; i < equ; ++i){ scanf("%d%s%s",&k,s1,s2); a[i][var] = cha(s1,s2); int type; while (k--){ scanf("%d",&type); type--; a[i][type]++; a[i][type] %= 7; } } //d(); int free_num = Guass(); if (free_num == -1) printf("Inconsistent data.\n"); else if (free_num > 0) printf("Multiple solutions.\n"); else{ for (i = 0; i < var - 1; ++i) printf("%d ",X[i]); printf("%d\n",X[i]); } } return 0; }