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
题意:有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.”,如果
只有唯一解,则输出每种装饰物需要生产的天数。
思路:高斯消元法;
由于并没有说明是第几周的星期几,我们不知道具体差了多少天;
我们可以对于方程左边右边取模7,求出解;
至于对于求解x时,需要判断x是否取整
while(temp%a[i][i]!=0) temp+=7;
x[i]=(temp/a[i][i])%7;
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int a[400][400];
int x[400];
int equ,var;
int find(char *c)
{
if(strcmp(c,"MON")==0)
return 1;
if(strcmp(c,"TUE")==0)
return 2;
if(strcmp(c,"WED")==0)
return 3;
if(strcmp(c,"THU")==0)
return 4;
if(strcmp(c,"FRI")==0)
return 5;
if(strcmp(c,"SAT")==0)
return 6;
if(strcmp(c,"SUN")==0)
return 7;
}
int gcd(int x,int y)
{
while(x%y)
{
int temp=x%y;
x=y;
y=temp;
}
return y;
}
int lcm(int x,int y)
{
return x*y/(gcd(x,y));
}
int Gauss()
{
int max_r;
memset(x,0,sizeof(x));
int col,k,temp,ta,tb,LCM,free_x_num,free_index;
for(k=0,col=0;k<equ&&col<var;k++,col++)
{
max_r=k;
for(int i=k+1;i<equ;i++)
if(abs(a[i][col])>abs(a[max_r][col]))
max_r=i;
if(max_r!=k)
for(int j=col;j<=var;j++)
swap(a[k][j],a[max_r][j]);
if(a[k][col]==0)
{
k--;continue;
}
for(int i=k+1;i<equ;i++)
{
if(a[i][col]!=0)
{
LCM=lcm(abs(a[i][col]),abs(a[k][col]));
ta=LCM/abs(a[i][col]),tb=LCM/abs(a[k][col]);
if(a[i][col]*a[k][col]<0) tb=-tb;
for(int j=col;j<=var;j++)
a[i][j]=((a[i][j]*ta-a[k][j]*tb)%7+7)%7;
}
}
}
for(int i=k;i<equ;i++)
if(a[i][var]!=0) return -1;
if(k<var)
return 1;
for(int i=var-1;i>=0;i--)
{
temp=a[i][var];
for(int j=i+1;j<var;j++)
if(a[i][j]!=0)
{
temp-=a[i][j]*x[j];
temp%=7;
}
while(temp%a[i][i]!=0) temp+=7;
x[i]=(temp/a[i][i])%7;
}
return 0;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m) break;
equ=m;var=n;
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
{
int t;
char c1[5],c2[5];
scanf("%d%s%s",&t,c1,c2);
a[i][var]=((find(c2)-find(c1)+1)%7+7)%7;
for(int j=0;j<t;j++)
{
int val;
scanf("%d",&val);
val--;
a[i][val]++;
a[i][val]=a[i][val]%7;
}
}
/*for(int i=0;i<equ;i++)
{for(int j=0;j<=var;j++)
printf("%d ",a[i][j]);
printf("\n");}
*/
int ans=Gauss();
if(ans==-1)
printf("Inconsistent data.\n");
else if(ans==1)
printf("Multiple solutions.\n");
else
for(int i=0;i<var;i++)
{
if(i==var-1)
{
if(x[i]<=2) printf("%d\n",x[i]+7);
else printf("%d\n",x[i]);
break;
}
if(x[i]<=2) printf("%d ",x[i]+7);
else printf("%d ",x[i]);
}
}
}