poj 2947 Widget Factory (高斯消元)

Widget Factory
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 5833 Accepted: 2025

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:

生产一些零件,已知有n种零件,m条记录,

记录只记录了某次生产从周几开始,周几结束,以及生产了哪些产品。

每件商品生产所需天数为3-9天。

求每样产品需要多少天才能完成。

题解:高斯消元求解同余方程组

这道题与之前的高斯消元略有不同,因为有m条信息,其中某一个信息可能对当前列没有贡献(该列的所以系数为0),但是有可能使后面的某一列出现唯一解(之后不会再从这一行中寻找),所以我们要把这一行清空,然后放到所以信息的最后。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 603
#define Mod 7
using namespace std;
int a[N][N],b[N],n,m,ans[N];
int pow(int num,int x)
{
	int base=num; int ans=1;
	while (x){
		if (x&1) ans=(ans*base)%7;
		base=(base*base)%7;
		x>>=1;
	}
	//cout<<ans<<endl;
	return ans%7;
}
int calc(char s[])
{
	if (s[1]=='W') return 3;
	if (s[1]=='F') return 5;
	if (s[1]=='M') return 1;
	if (s[1]=='T')
	 if (s[2]=='H') return 4;
	 else return 2;
	if (s[1]=='S')
	 if (s[2]=='U') return 7;
	 else return 6;
}
int gauss()
{
    int tot=0;
	for (int i=1;i<=n;i++){
		int num=i;
		for (int j=i+1;j<=m;j++)
		 if (abs(a[j][i])>abs(a[num][i])) num=j;
		if (!a[num][i]) {
			//mak=-1; 
			tot++; ++m;
			for (int j=1;j<=n;j++) swap(a[i][j],a[m][j]);
		    swap(b[m],b[i]);
		}
		else {
		 for (int j=1;j<=n;j++) swap(a[i][j],a[num][j]);
		 swap(b[num],b[i]);
	    }
		for (int j=i+1;j<=m;j++)
		 if (j!=i&&a[j][i]) {
		 	int t=abs(a[j][i])*pow(abs(a[i][i]),5)%7;
		 	if (a[j][i]*a[i][j]<0) 
		 	 t=-t;
		 	for (int k=1;k<=n;k++)
		 	 a[j][k]-=t*a[i][k],a[j][k]=(a[j][k]%7+7)%7;
		 	b[j]-=t*b[i]; b[j]=(b[j]%7+7)%7;
		 }
	}
	for (int i=n;i>=1;i--){
		if (!a[i][i]&&b[i]%7!=0) return -1;
        else if (!a[i][i]&&b[i]%7==0) {
         tot=1;
		 continue;
	    }
		ans[i]=((b[i]*pow(a[i][i],5))%7+7)%7;
		if (ans[i]<3) ans[i]+=7;
		for (int j=i-1;j>=1;j--)
		 if (a[j][i])
		 b[j]-=ans[i]*a[j][i];
	}
	for (int i=n+1;i<=m;i++){
		int size=0;
		for (int j=1;j<=n;j++)
		 size+=a[i][j]*ans[j];
		size=(size%7+7)%7;
		if (size!=b[i]) return -1;
	}
	if (tot) return 2;
	else return 1;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	while (true) {
		scanf("%d%d",&n,&m);
		if (n==0&&m==0) break;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for (int i=1;i<=m;i++) {
		    char s[10],s1[10]; int x;
		    scanf("%d%s%s",&x,s+1,s1+1);
		    for (int j=1;j<=x;j++){
		    	int y; scanf("%d",&y);
		    	a[i][y]++; a[i][y]%=7;
			}
			int st=calc(s); int end=calc(s1);
			b[i]=((end-st+1)%7+7)%7;
		}
		int opt=gauss();
		if (opt==2) printf("Multiple solutions.\n");
		if (opt==1) {
			for (int i=1;i<=n;i++) printf("%d ",ans[i]);
			printf("\n");
		}
		if (opt==-1) printf("Inconsistent data.\n");
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值