poj2947 Widget Factory (高斯消元解模线性方程组)

来源:互联网 发布:js获取农历日期 编辑:程序博客网 时间:2024/06/05 08:20

题目链接:http://poj.org/problem?id=2947

Widget Factory
Time Limit: 7000MS
Memory Limit: 65536KTotal Submissions: 6261
Accepted: 2207

Description

The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to build a widget depends on its type: the simple widgets need only 3 days, but the most complex ones may need as many as 9 days. 

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

The input contains several blocks of test cases. Each case begins with a line containing two integers: the number 1 ≤ n ≤ 300 of the different types, and the number 1 ≤ m ≤ 300 of the records. This line is followed by a description of the m records. Each record is described by two lines. The first line contains the total number 1 ≤ k ≤ 10000 of widgets built by this widgeteer, followed by the day of week when he/she started working and the day of the week he/she was fired. The days of the week are given bythe strings `MON', `TUE', `WED', `THU', `FRI', `SAT' and `SUN'. The second line contains k integers separated by spaces. These numbers are between 1 and n , and they describe the diofferent types of widgets that the widgeteer built. For example, the following two lines mean that the widgeteer started working on a Wednesday, built a Type 13 widget, a Type 18 widget, a Type 1 widget, again a Type 13 widget,and was fired on a Sunday. 

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

For each test case, you have to output a single line containing n integers separated by spaces: the number of days required to build the different types of widgets. There should be no space before the first number or after the last number, and there should be exactly one space between two numbers. If there is more than one possible solution for the problem, then write `Multiple solutions.' (without the quotes). If you are sure that there is no solution consistent with the input, then write `Inconsistent data.'(without the quotes).

Sample Input

2 32 MON THU1 23 MON FRI1 1 23 MON SUN1 2 210 21 MON TUE 31 MON WED30 0

Sample Output

8 3Inconsistent data.
【解析】:

题意是,有n中零件,m个工人生产。每个工人生产k件,从一个星期的某天到另一天之间。然后输入生产的编号。

问:每种编号的零件各需几天生产。注意是星期,所以题目中天数计算全部%7,并且小于3的答案要加7(题目第一段)

设每种零件的生产天数分别x1,x2,x3.....xn

根据m个工人的生产情况,可以列m个n元方程,构成方程组。

接下来解方程即可。

解方程用高斯消元(线性代数是基础)。

根据方程组的增广矩阵 得到 行阶梯形矩阵,就得出了矩阵的秩,就能判断解的情况。

if 系数矩阵的秩!=增广矩阵的秩 then 无解!

else if 增广矩阵的秩<n        then      无穷解!

else    增广矩阵的秩==n      then      唯一解!

若有唯一解,在根据行阶梯形矩阵(一个严格的上三角矩阵)得出解即可。

【代码】:

#include <stdio.h>#include <string.h>  #include <iostream>  #include <algorithm> #define mset(a,i) memset(a,i,sizeof(a))#define gcd(a,b) __gcd(a,b)using namespace std;const int mod=7;int a[310][310]; char s[5],t[5];int f[310];//存解char week[19][9]={"SUN","MON","TUE", "WED", "THU", "FRI", "SAT"};int xiaoyuan(int n,int m)//n个方程,m个变元,另外有1个结果列{int row=1;for(int col=1;col<=m;col++)//列{int i;for(i=row;i<=n;i++)if(a[i][col])break;if(i>n)continue;//本列不处理 if(i!=row){for(int j=col;j<=m+1;j++)swap(a[i][j],a[row][j]);}for(i=row+1;i<=n;i++){if(a[i][col])//i行需被消掉{int now=a[row][col],next=a[i][col];int lcm=now*next/gcd(now,next);int w=lcm/now,t=lcm/next;for(int j=col;j<=m+1;j++)a[i][j]=((a[i][j]*t-a[row][j]*w)%mod+mod)%mod;} }row++;//别忘了 }//row-1为系数矩阵的秩for(int i=row;i<=n;i++)if(a[i][m+1])return -1;//增广矩阵的秩大,无解 if(row-1<m)return row-1;//存在自由变元,所以,多组解(返回秩)//剩下的,增广矩阵的秩==系数矩阵的秩,唯一解for (int i=m; i>=1; i--)    {        int temp = a[i][m+1];        for (int j=i+1; j<=m; j++)        {            temp -= a[i][j] * f[j];            temp=(temp%mod+mod)%mod;        }        //if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.        //取模处理        while(temp%a[i][i]!=0)temp+=mod;        f[i] = (temp / a[i][i])%mod;            if(f[i]<=2)f[i]+=mod;    }return 0;//唯一解}int main(){int n,m,k;while(scanf("%d%d",&n,&m),n&&m){mset(a,0);for(int i=1;i<=m;i++)//m个方程 {scanf("%d%s%s",&k,s,t);int start=0;while(strcmp(week[start],s))start++;int end=0;while(strcmp(week[end],t))end++;while(k--){int N;scanf("%d",&N);a[i][N]++;a[i][N]%=7;}a[i][n+1]=(end+mod-start+1)%mod;}//增广矩阵构造完毕int z=xiaoyuan(m,n);//消元,并得到秩if(z==-1)    puts("Inconsistent data.");//无解else if(z)puts("Multiple solutions.");//多组解else       //唯一解:n==z{for(int i=1;i<n;i++)printf("%d ",f[i]);printf("%d\n",f[n]);}}return 0;}




原创粉丝点击