POJ2947 Widget Factory
题意
Language:
Widget Factory
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 7197 | Accepted: 2533 |
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.
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 .
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 3 2 MON THU 1 2 3 MON FRI 1 1 2 3 MON SUN 1 2 2 10 2 1 MON TUE 3 1 MON WED 3 0 0
Sample Output
8 3 Inconsistent data.
Hint
Huge input file, 'scanf' recommended to avoid TLE.
Source
生产一些零件,已知零件种数,记录条数。记录只记录了某次生产从周几开始,周几结束,以及生产了哪些产品。每件商品生产所需天数为3-9天。求每样产品需要多少天才能完成。
若无解输出Inconsistent data.有无穷解输出Multiple solutions.有唯一解,输出其解
分析
参照happy_lcj的题解。
根据题目所给信息,可以列出同余方程组,再根据高斯消元求解,但还得判断是无解,无穷解,还是唯一解
- 系数矩阵的秩若小于增广矩阵的秩,则无解,否则有解
- 若有解,若系数矩阵的秩小于未知数的个数,则有无穷解
- 若有解,系数矩阵的秩等于未知数的个数,则有唯一解
有唯一解时,需要用扩展欧几里得(快速幂)求逆元解模线性方程
注:求矩阵的秩,先将矩阵化为阶梯型,有多少个非0行或者非0列,矩阵的秩就为多少.矩阵的行秩等于矩阵的列秩
时间复杂度\(O(n^3)\)
代码
学到了一种厉害的输出方式。
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=301;
map<string,int> mp;
int n,m,a[N][N],b[N];
int exgcd(int a,int b,int&x,int&y){
if(!b) return x=1,y=0,a;
int d=exgcd(b,a%b,y,x);
return y-=a/b*x,d;
}
void Widget_Factory(){
memset(a,0,sizeof a);
for(int i=1,k;i<=m;++i){
static char s1[4],s2[4];
read(k);scanf("%s%s",s1,s2);
b[i]=(mp[s2]-mp[s1]+1)%7;
while(k--) ++a[i][read<int>()]%=7;
}
int w=0;
for(int i=1,t;i<=n;++i){
t=0;
for(int j=w+1;j<=m;++j)
if(a[j][i]) {t=j;break;}
if(!t) continue;
swap(b[++w],b[t]);
for(int j=1;j<=n;++j) swap(a[w][j],a[t][j]);
for(int j=1,x=a[w][i],y;j<=m;++j){
if(w==j||!a[j][i]) continue;
y=a[j][i];
for(int k=1;k<=n;++k)
a[j][k]=(a[j][k]*x-a[w][k]*y)%7;
b[j]=(b[j]*x-b[w]*y)%7;
}
}
for(int i=w+1;i<=m;++i)if(b[i]%=7)
return puts("Inconsistent data."),void();
if(w<n) return puts("Multiple solutions."),void();
for(int i=1,x,y,d;i<=n;++i){
d=exgcd(a[i][i],7,x,y);
printf("%d ",((x*b[i]/d-3)%7+7)%7+3);
}
puts("");
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
mp["MON"]=1,mp["TUE"]=2,mp["WED"]=3,
mp["THU"]=4,mp["FRI"]=5,mp["SAT"]=6,mp["SUN"]=7;
while(read(n)|read(m)) Widget_Factory();
return 0;
}
静渊以有谋,疏通而知事。