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).
 
题目大意:有n个装饰品,每个装饰品要生产3~9天。给出m种作业,每个作业生产k种装饰品,从星期X生产到星期Y(未必是同一个星期,一天只能生产一个产品),然后给出这k种装饰品分别是什么。问是否能求出n个装饰品分别须要多少天来生产,若有多组解输出Multiple solutions.,无解输出Inconsistent data.。
思路:可以列出m个方程组成方程组。对于每一个作业,设ki为生产装饰品 i 多少次(用输入数据统计一下就好),xi为生产装饰品 i 需要多少天,那么可以列出方程
k1 * x1 + k2 * x2 + …… + kn * xn = Y - X + 1(mod 7)。
得出方程组后,用高斯消元解,其中需要保证每个系数的范围都在0~6中,除法可以对7求逆元。
消掉下三角之后,若存在k1, k2, …… , kn = 0而右边系数不为0的方程,则无解。
若矩阵的秩rank < n则方程有多组解。
否则算出每一个xi,若xi < 3则 xi += 7
PS:白书训练指南上的模板居然有BUG!
 
代码(2360MS):
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std; int inv[]; struct MOD7 {
int val;
MOD7() {}
MOD7(int val): val((val + ) % ) {}
MOD7 operator + (const MOD7 &rhs) const {
return MOD7(val + rhs.val);
}
MOD7 operator - (const MOD7 &rhs) const {
return MOD7(val - rhs.val);
}
MOD7 operator * (const MOD7 &rhs) const {
return MOD7(val * rhs.val);
}
MOD7 operator / (const MOD7 &rhs) const {
return MOD7(val * inv[rhs.val]);
}
void operator -= (const MOD7 &rhs) {
val = (val - rhs.val + ) % ;
}
bool isZero() {
return val == ;
}
void inc() {
val = (val + ) % ;
}
void print() {
if(val < ) printf("%d", val + );
else printf("%d", val);
}
}; map<string, int> mymap; void init() {
for(int i = ; i < ; ++i) {
inv[i] = ;
for(int j = ; j < ; ++j) inv[i] = (inv[i] * i) % ;
}
mymap["MON"] = ;
mymap["TUE"] = ;
mymap["WED"] = ;
mymap["THU"] = ;
mymap["FRI"] = ;
mymap["SAT"] = ;
mymap["SUN"] = ;
} const int MAXN = ; MOD7 mat[MAXN][MAXN];
int n, m; int guess_eliminatioin() {
int rank = ;
for(int i = , t = ; i < m && t < n; ++i, ++t) {
int r = i;
for(int j = i + ; j < m; ++j)
if(mat[r][t].val < mat[j][t].val) r = j;
if(mat[r][t].isZero()) { --i; continue;}
else ++rank;
if(r != i) for(int j = ; j <= n; ++j) swap(mat[i][j], mat[r][j]);
for(int j = n; j >= t; --j)
for(int k = i + ; k < m; ++k) mat[k][j] -= mat[i][j] * mat[k][t] / mat[i][t];
}
for(int i = rank; i < m; ++i)
if(!mat[i][n].isZero()) return -;
if(rank < n) return ;
for(int i = n - ; i >= ; --i) {
for(int j = i + ; j < n; ++j)
mat[i][n] -= mat[j][n] * mat[i][j];
mat[i][n] = mat[i][n] / mat[i][i];
}
return ;
} int main() {
init();
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
memset(mat, , sizeof(mat));
int t, p;
string s1, s2;
for(int i = ; i < m; ++i) {
cin>>t>>s1>>s2;
while(t--) {
scanf("%d", &p);
mat[i][p - ].inc();
}
mat[i][n] = MOD7(mymap[s2] - mymap[s1] + );
}
int result = guess_eliminatioin();
if(result == -) puts("Inconsistent data.");
else if(result == ) puts("Multiple solutions.");
else {
for(int i = ; i < n - ; ++i) {
mat[i][n].print();
putchar(' ');
}
mat[n - ][n].print();
puts("");
}
}
}

POJ 2947 Widget Factory(高斯消元)的更多相关文章

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

    题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...

  2. POJ 2947 2947 Widget Factory 高斯消元

    给出组件的数量n,给出记录的数量m(n就是变元数量,m是方程数量).每一个记录代表一个方程,求每个组件的生产天数. 高斯消元即可 #include <cstdio> #include &l ...

  3. POJ 2947-Widget Factory(高斯消元解同余方程式)

    题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...

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

    http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai&l ...

  5. poj 2947 Widget Factory

    Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...

  6. POJ 1830 开关问题 高斯消元,自由变量个数

    http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...

  7. A - The Water Bowls POJ - 3185 (bfs||高斯消元)

    题目链接:https://vjudge.net/contest/276374#problem/A 题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置, ...

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

    依据题意可构造出方程组.方程组的每一个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki; 高斯消元构造上三角矩阵,以最后一个一行为例: C*x9 = ...

  9. POJ 2065 SETI(高斯消元)

    题目链接:http://poj.org/problem?id=2065 题意:给出一个字符串S[1,n],字母a-z代表1到26,*代表0.我们用数组C[i]表示S[i]经过该变换得到的数字.给出一个 ...

随机推荐

  1. BLE蓝牙的广播类型

    广播的类型一般分为四种,见如下表格: 1. 可连接的非定向广播(Connectable Undirected Event Type): 这是一种用途最广的广播类型,包括广播数据和扫描响应数据,它表示当 ...

  2. linux epoll 简单demo

    一个简单的epoll demo ,同时接受多个客户端连接,并把接收到的字符串转化为大写字母返回给客户端 #include<stdio.h> #include<arpa/inet.h& ...

  3. zepto源码--init--学习笔记

    先展示init函数,由于笔记本屏幕太小,删掉了部分源码注释,才能在一屏内截图. 当我们调用$()的时候,便会直接调用zepto.init()生成zepto对象,跟jquery生成jquery对象类似. ...

  4. Tea---hdu5881(规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5881 题意: 现在有一壶水,体积在[L, R]范围内,现有两个空杯子,现想要把这壶水倒入这两个杯子中去 ...

  5. Truncate table

    Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用.

  6. 将Excel导入SQL Server 只能导入数字,其他数据变为NULL怎么解决?

    先新建一个TXT文件,把数据粘贴进去 再新建一个Excel文件,在菜单栏中选Data再选From Text 找到txt文件,点import 一定要选Text 点Finish,点OK. 接下来在往数据库 ...

  7. imx6 关闭 otg host

    参考文档: http://www.cnblogs.com/zengjfgit/p/4711336.html make menuconfig 去掉Support for DR host port on ...

  8. Webapp的display-name问题

    临时需要做一个webapp,就按myeclipse缺省的web工程做了,web.xml也没改,本地测试没问题就放到服务器上去了. 测试发现,走 http://服务器ip:8080/appname居然出 ...

  9. Python-S13作业-day4-之登陆,管理后台

    Python-S13作业-day4-之登陆,管理后台 需求: 本节作业,用户管理程序:          普通用户: 登录,注册,修改密码,查看本用户信息 管理员用户: 查看所有普通用户,按照指定关键 ...

  10. nohup和&的区别

    nohup和&的区别http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4241330&fromuid=21288388 ...