Widget Factory

 

Language:
Widget Factory
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 5407   Accepted: 1863

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 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.”,如果
只有唯一解,则输出每种装饰物需要生产的天数。

思路:高斯消元;

我们可以根据题意写出方程组,(a1x1+a2x2+a3x3+...)%7=(k)%7;

然后高斯消元解方程组;最后结果为(akxk)%7=(s)%7;用扩展欧几里得求下即可;

复杂(n3

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<math.h>
  7 #include<map>
  8 using namespace std;
  9 typedef long long LL;
 10 map<string,LL>my;
 11 LL  ju[400][400];
 12 LL  x[400];
 13 bool ree[400];
 14 char a[100];
 15 char b[100];
 16 LL gcd(LL n,LL m)
 17 {
 18         if(m==0)
 19                 return n;
 20         else if(n%m==0)
 21         {
 22                 return m;
 23         }
 24         else return gcd(m,n%m);
 25 }
 26 LL guss(LL equ,LL var,LL n);
 27 pair<LL ,LL>P(LL n,LL m);
 28 int main(void)
 29 {
 30         string aa;
 31         aa="MON";
 32         my[aa]=1;
 33         aa="TUE";
 34         my[aa]=2;
 35         aa="WED";
 36         my[aa]=3;
 37         aa="THU";
 38         my[aa]=4;
 39         aa="FRI";
 40         my[aa]=5;
 41         aa="SAT";
 42         my[aa]=6;
 43         aa="SUN";
 44         my[aa]=7;
 45         LL i,j,k;LL n,m;
 46         while(scanf("%lld %lld",&n,&m),n!=0&&m!=0)
 47         {
 48                 memset(ju,0,sizeof(ju));
 49                 for(j=0; j<m; j++)
 50                 {
 51                         scanf("%lld",&k);
 52                         scanf("%s",a);scanf("%s",b);
 53                         LL xx=my[a];
 54                         LL yy=my[b];
 55                         for(i=0; i<k; i++)
 56                         {
 57                                 LL xc;
 58                                 scanf("%lld",&xc);
 59                                 xc--;
 60                                 ju[j][xc]++;
 61                                 ju[j][xc]%=7;
 62                         }
 63                         ju[j][n]=(yy-xx+1)%7;
 64                 }
 65                 LL akk=guss(m,n,n);
 66                 if(akk==0)
 67                 {
 68                         for(i=0; i<n; i++)
 69                         {
 70                                 LL uk=gcd(ju[i][i],7);
 71                                 if(ju[i][n]%uk)
 72                                 {
 73                                         akk=-1;
 74                                         break;
 75                                 }
 76                                 else
 77                                 {
 78                                         ju[i][i]/=uk;
 79                                         ju[i][n]/=uk;
 80                                         pair<LL,LL>cc=P(ju[i][i],7/uk);
 81                                         x[i]=((ju[i][n]*cc.first)%(7/uk)+7/uk)%(7/uk);
 82                                         while(x[i]<3)
 83                                         {
 84                                             x[i]+=7/uk;
 85                                         }
 86                                         if(x[i]>9||x[i]<3)
 87                                         {
 88                                                 akk=-1;
 89                                                 break;
 90                                         }
 91                                 }
 92                         }
 93                 }
 94                 if(akk==-1)
 95                 {
 96                         printf("Inconsistent data.\n");
 97                 }
 98                 else if(akk)
 99                 {
100                         printf("Multiple solutions.\n");
101                 }
102                 else
103                 {
104                         printf("%lld",x[0]);
105                         for(i=1; i<n; i++)
106                                 printf(" %lld",x[i]);
107                                 printf("\n");
108                 }
109         }
110         return 0;
111 }
112 LL guss(LL equ,LL var,LL n)
113 {
114         LL i,j,k;
115         LL max_r;
116         LL col;
117         for(i=0; i<=var; i++)
118         {
119                 x[i]=0;
120         }
121         col=0;
122         for(k=0; k<equ&&col<var; k++,col++)
123         {
124                 max_r=k;
125                 for(i=k+1; i<equ; i++)
126                 {
127                         if(fabs(ju[i][col])>fabs(ju[max_r][col]))
128                         {
129                                 max_r=i;
130                         }
131                 }
132                 if(i!=k)
133                 {
134                         for(j=col; j<var+1; j++)
135                         {
136                                 swap(ju[max_r][j],ju[k][j]);
137                         }
138                 }
139                 if(ju[k][col]==0)
140                 {
141                         k--;
142                         continue;
143                 }
144                 for(i=0; i<equ; i++)
145                 {
146                         if(i!=k&&ju[i][col]!=0)
147                         {
148                                 LL xt=gcd((LL)fabs(ju[i][col]),(LL)fabs(ju[k][col]));
149                                 LL yt=fabs(ju[i][col])/xt*fabs(ju[k][col]);
150                                 LL nn,mm;
151                                 nn=yt/(LL)fabs(ju[k][col]);
152                                 mm=yt/(LL)fabs(ju[i][col]);
153                                 if(ju[i][col]*ju[k][col]<0)
154                                 {
155                                        nn=-nn;
156                                 }
157                                 for(j=0; j<=var; j++)
158                                 {
159                                ju[i][j]=(((ju[i][j]%7)*(mm%7)-(nn%7)*(ju[k][j])%7)+7)%7;
160                                 }
161                         }
162                 }
163         }
164         LL sum=0;
165         LL flag=0;
166         for(i=k;i<equ;i++)
167         {
168             if(ju[i][n])
169             {
170                 flag=-1;
171                 return flag;
172             }
173         }
174          if(k<n)
175                 flag=1;
176         return flag;
177 }
178 pair<LL ,LL>P(LL n,LL m)
179 {
180         if(m==0)
181         {
182                 pair<LL,LL>C=make_pair(1,0);
183                 return C;
184         }
185         else
186         {
187                 pair<LL,LL>N=P(m,n%m);
188                 LL x=N.second;
189                 LL y=N.first;
190                 N.first=x;
191                 N.second=y-(n/m)*x;
192                 return N;
193         }
194 }

 

posted @ 2016-05-30 08:48  sCjTyC  阅读(311)  评论(0编辑  收藏  举报