【POJ】2947 Widget Factory(高斯消元)

http://poj.org/problem?id=2947

各种逗啊。。还好1a了。。

题意我就不说了,百度一大把。

转换为mod的方程组,即

(x[1,1]*a[1])+(x[1,2]*a[2])+...+(x[1,n]*a[n])=x[1, n+1] (mod m)

(x[2,1]*a[1])+(x[2,2]*a[2])+...+(x[2,n]*a[n])=x[2, n+1] (mod m)

...

(x[n,1]*a[1])+(x[n,2]*a[2])+...+(x[n,n]*a[n])=x[n, n+1] (mod m)

没有mod就是裸的高斯消元。。。

我们来考虑怎么消元。

显然如果有方程1和方程2,他们都有相同系数不为0的元素y,那么我们消元只需要将他们的系数调成一样即可,即调成公倍数,那么因为mod意义下满足a=b(mod m), a*c=b*c(mod m),所以将两个方程左式和右式都乘上这个公倍数即可。

而回代麻烦一些,减完当前方程其它元素的对应值后,此时假设当前元素的系数为A[i][i],而值是A[i][n+1],那么因为有x*A[i][i]=A[i][n+1](mod m),我们可以用拓展欧几里得或者一直加m,知道出现成立(此时因为一定有解,所以一定找得到一个x使得等式成立)

那么得到的x就是答案。

做的时候注意n和m是方程数还是未知量的数就行了。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=305;
typedef int mtx[N][N];
string Dt[7]={"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};

int gauss(mtx A, int n, int m, int MD) {
	int x=1, y=1, pos;
	while(x<=n && y<=m) {
		pos=x;
		while(!A[pos][y] && pos<=n) ++pos;
		if(A[pos][y]) {
			for1(i, 1, m+1) swap(A[pos][i], A[x][i]);
			for1(i, x+1, n) if(A[i][y]) {
				int l=A[x][y], r=A[i][y];
				for1(j, y, m+1) A[i][j]=((A[i][j]*l-A[x][j]*r)%MD+MD)%MD;
			}
			++x;
		}
		++y;
	}
	for1(i, x, n) if(A[i][m+1]) return -1;
	if(x<=m) return m-x+1;
	for3(i, m, 1) {
		for1(j, i+1, m) if(A[i][j]) A[i][m+1]=((A[i][m+1]-(A[j][m+1]*A[i][j]))%MD+MD)%MD;
		while(A[i][m+1]%A[i][i]!=0) A[i][m+1]+=MD; //这里可以用拓欧搞掉。。
		A[i][m+1]=(A[i][m+1]/A[i][i])%MD;
	}
	return 0;
}
inline int get(string s) { return find(Dt, Dt+7, s)-Dt+1; }
int main() {
	int n=getint(), m=getint(), t;
	char s[2][5];
	mtx a;
	while(n|m) {
		CC(a, 0);
		for1(i, 1, m) {
			read(t); scanf("%s%s", s[0], s[1]);
			a[i][n+1]=(get(s[1])-get(s[0])+1+7)%7;
			for1(j, 1, t) ++a[i][getint()];
			for1(j, 1, n) a[i][j]%=7;
		}
		int ans=gauss(a, m, n, 7);
		if(ans==-1) puts("Inconsistent data.");
		else if(ans) puts("Multiple solutions.");
		else {
			for1(i, 1, n) if(a[i][n+1]<3) a[i][n+1]+=7;
			for1(i, 1, n-1) printf("%d ", a[i][n+1]); printf("%d\n", a[n][n+1]);
		}
		n=getint(), m=getint();
	}
	return 0;
}

  



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

Central Europe 2005

时间: 2024-10-09 14:34:53

【POJ】2947 Widget Factory(高斯消元)的相关文章

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to

poj 2947 Widget Factory(高斯消元)

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

poj 2947 Widget Factory (高斯消元,解模线性方程)

链接:poj 2947 题意:生产一些零件,已知零件种数,记录条数 记录只记录了某次生产从周几开始,周几结束,以及生产了哪些产品. 每件商品生产所需天数为3-9天. 求每样产品需要多少天才能完成. 若无解输出Inconsistent data. 有无穷解输出Multiple solutions. 有唯一解,输出其解 分析:根据题目所给信息,可以列出同余方程组,再根据高斯消元求解, 但还得判断是无解,无穷解,还是唯一解 1.系数矩阵的秩若与增广矩阵的秩不相等,则无解,否则有解 2.若有解,若增广矩

Poj 2947 widget factory (高斯消元解同模方程)

题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同种类,也可以不同种类),问每种零件生产一个出来需要的时间? 解题思路: 给出的时间段是从周几到周几,并没有给出具体的时间段,因此在计算过程中要进行取模,还有就是对每个零件要在题目要求的范围内进行枚举. ps:如果求出来的增广矩阵是n*n的,但是某个零件在[3,9]之间没有合理的解,也是无解的. 1

uva 1564 - Widget Factory(高斯消元+逆元)

题目链接:uva 1564 - Widget Factory 题目大意:n种零件,m次工作日程,零件序号从1到n,给出m次工作日程的信息,x,s,e,表示生产了x个零件,从星期s开始到星期e(有可能是多个星期),然后给出生产的x个零件的序号.求每个零件被生产需要多少天(保证在3到10天) 解题思路:因为不能确定每个工作日程具体生产了几天,所以对应列出的方程均为线性模方程(模7),所以在高斯消元的过程中遇到除法要转换成乘上逆元. #include <cstdio> #include <cs

POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是还保留着一些对工人制造工具的记录,对于每个老员工,他的记录包括,他开始工作的时间(在某个星期的星期几),被炒鱿鱼的时间(某个星期的星期几),在第几个星期不知道.....在这段时间里,他正好加工了k件物品,给出了这k件物品的编号.我们要做的就是通过这些记录,来确定每种工具的加工时间是多少. 分析: 对

POJ2947Widget Factory(高斯消元解同模方程)

http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai<=n)第一行表示从星期start 到星期end 一共生产了p 件装饰物(工作的天数为end-start+1+7*x,加7*x 是因为它可能生产很多周),第二行表示这p 件装饰物的种类(可能出现相同的种类,即ai=aj).规定每件装饰物至少生产3 天,最多生产9 天.问每种装饰物需要生产的天数.如果没有解,

poj 1830 开关问题 高斯消元

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)

根据题意可构造出方程组,方程组的每个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki; 高斯消元构造上三角矩阵,以最后一个一行为例: C*x9 = sum + 4*k,exgcd求出符合范围的x9,其他方程在代入已知的变量后格式亦如此. 第一发Gauss,蛮激动的. #include <algorithm> #include <iostream> #include <cstring> #include <cstd

poj 2947 Widget Factory(模7环上的高斯消元)

http://poj.org/problem?id=2947 大致题意: 有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天.问每种装饰物需要生产的天数.如果没有解,则输出&q