POJ2947 Widget Factory
阿新 • • 發佈:2019-04-05
change see fine test goods holidays take stream bre
Language:Widget Factory
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 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 . 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).
題意
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.
Input
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
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
Central Europe 2005生產一些零件,已知零件種數,記錄條數。記錄只記錄了某次生產從周幾開始,周幾結束,以及生產了哪些產品。每件商品生產所需天數為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;
}
POJ2947 Widget Factory