Widget Factory
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 5173   Accepted: 1790

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. 

题意是给出了n件商品加工时间,m个同余方程。每个方程右边的值自然是时间差,左边就是第i种商品有多少个在这个期间加工。

感觉之前模板里面的写得有一些小问题。对于同余方程,还是最后枚举下来更正确,套用之前的模板对于POJ1166输出有问题。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std; int n, m;
int x[305];//解集
int val[305][305];//增广矩阵 inline int gcd(int a, int b)
{
int t;
while (b != 0)
{
t = b;
b = a%b;
a = t;
}
return a;
} inline int lcm(int a, int b)
{
return a / gcd(a, b)*b;//先除后乘防溢出
} int Gauss(int equ, int var)
{
int i, j, k;
int max_r;//当前这列绝对值最大的行
int col;//当前处理的列
int ta, tb;
int LCM;
int temp;
int free_x_num;
int free_index; for (int i = 0; i <= var; i++)
{
x[i] = 0;
}
//转换为阶梯阵
col = 0;//当前处理的列
for (k = 0; k < equ&&col < var; k++, col++)
{
//枚举当前处理的行
//找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减少误差)
max_r = k;
for (i = k + 1; i < equ; i++)
{
if (abs(val[i][col])>abs(val[max_r][col]))
max_r = i;
}
if (max_r != k)
{//与第k行交换
for (j = k; j < var + 1; j++)
swap(val[k][j], val[max_r][j]);
}
if (val[k][col] == 0)
{
k--;
continue;
}
for (i = k + 1; i < equ; i++)
{//枚举要删去的行
if (val[i][col] != 0)
{
LCM = lcm(abs(val[i][col]), abs(val[k][col]));
ta = LCM / abs(val[i][col]);
tb = LCM / abs(val[k][col]);
if (val[i][col] * val[k][col] < 0)
tb = -tb;
for (j = col; j < var + 1; j++)
{
val[i][j] = ((val[i][j] * ta - val[k][j] * tb) % 7 + 7) % 7;
}
}
}
} //无解的情况
for (i = k; i < equ; i++)
{
if (val[i][col] != 0)
return -1;
} if (k < var)
{
return var - k;
}
for (i = var - 1; i >= 0; i--)
{
temp = val[i][var];
for (j = i + 1; j < var; j++)
{
if (val[i][j] != 0)
{
temp = temp - val[i][j] * x[j];
temp = (temp % 7 + 7) % 7;
}
}
for (x[i] = 0; x[i] < 7; x[i]++)
{
if ((x[i] * val[i][i] + 7) % 7 == temp)
{
break;
}
}
}
return 0;
} int tran(char *s)
{
if (strcmp(s, "MON") == 0)
return 1;
else if (strcmp(s, "TUE") == 0)//"MON","TUE", "WED","THU", "FRI","SAT","SUN"
return 2;
else if (strcmp(s, "WED") == 0)
return 3;
else if (strcmp(s, "THU") == 0)
return 4;
else if (strcmp(s, "FRI") == 0)
return 5;
else if (strcmp(s, "SAT") == 0)
return 6;
else if (strcmp(s, "SUN") == 0)
return 7;
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, j, num, temp_num, ans;
char temp1[20], temp2[20];
while (scanf("%d%d", &n, &m) != EOF)
{
if (n == 0 && m == 0)
break;
memset(val, 0, sizeof(val));
for (i = 0; i < m; i++)
{
scanf("%d%s%s", &num, temp1, temp2); val[i][n] = ((tran(temp2) - tran(temp1) + 1) % 7 + 7) % 7;
for (j = 0; j < num; j++)
{
scanf("%d", &temp_num);
val[i][temp_num - 1]++;
val[i][temp_num - 1] = val[i][temp_num - 1] % 7;
}
}
ans = Gauss(m, n);
if (ans == 0)
{
for (int h = 0; h < n; h++)
{
if (x[h] < 3)x[h] = x[h] + 7;
if (h == 0)
printf("%d", x[h]);
else
{
printf(" %d", x[h]);
}
}
printf("\n");
}
else if (ans == -1)
{
printf("Inconsistent data.\n");
}
else
{
printf("Multiple solutions.\n");
}
}
//system("pause");
return 0;
}

POJ 2947:Widget Factory 求同余方程的更多相关文章

  1. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  2. poj 2947 Widget Factory

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

  3. poj 2947 Widget Factory (高斯消元解同余方程组+判断无解、多解)

    http://poj.org/problem?id=2947 血泪史: CE:poj的string类型要加string库,swap不能直接交换数组 WA: x[m-1]也有可能<3啊O(≧口≦) ...

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

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

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

    题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是 ...

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

    http://poj.org/problem?id=2947 各种逗啊..还好1a了.. 题意我就不说了,百度一大把. 转换为mod的方程组,即 (x[1,1]*a[1])+(x[1,2]*a[2]) ...

  7. POJ 2947 2947 Widget Factory 高斯消元

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

  8. POJ Widget Factory 【求解模线性方程】

    传送门:http://poj.org/problem?id=2947 Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  9. [Gauss]POJ2947 Widget Factory

    题意: 有n种小工具要加工,每种工具的加工时间为3到9天,给了m条加工记录.  每条记录 X $s_1$ $s_2$ 分别代表 这个工人在$s_1$到$s_2$(前闭后闭)的时间里加工了X件小工具   ...

随机推荐

  1. 5_5 集合栈计算机(UVa12096)<stack与STL其他容器的综合运用>

    有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始化为空的栈,并支持以下操作:( 维护 N(1≤N≤2000) 个操作, 可能的五种操作如下:) ■PUSH: 在栈顶加入一个空集合 A= ...

  2. Unity2018编辑器脚本趟坑记录

    解除预制体问题:(这个例子是解除游戏中的Canvas与Asset中的预制体的关系) if( PrefabUtility.IsAnyPrefabInstanceRoot(GameObject.Find( ...

  3. ResultSet指针回到初始位置的方法及ResultSet介绍

    原文地址: https://blog.csdn.net/walkerjong/article/details/7023872 仅供学习参考使用. 结果集(ResultSet)是数据中查询结果返回的一种 ...

  4. [02]Sort选择排序

    选择排序 算法速度:通过大O表示法表示,O(n),n是操作数,表示算法执行的次数: 数组:是有序的元素序列:若将有限个类型相同的变量的集合命名,那么这个名称为数组名: 链表:是一种物理存储单元上非连续 ...

  5. Update(stage3):第1节 redis组件:7、持久化

    7.redis的持久化 由于redis是一个内存数据库,所有的数据都是保存在内存当中的,内存当中的数据极易丢失,所以redis的数据持久化就显得尤为重要,在redis当中,提供了两种数据持久化的方式, ...

  6. (0)Lora及LoraWAN

    Lora和LoraWAN的区别 LoRa经常被误用来描述整个LPWAN通信系统,其实Lora是Semtech拥有的专有调制格式. SX1272和SX1276 LoRa芯片使用称为chirp扩频(CSS ...

  7. location练习!(重点)

    写location最重要的就是hosts文件,添加一次域名跳转就得添加一条hosts文件 hosts文件: 192.168.200.120 www.a.com 192.168.200.119 www. ...

  8. windows下安装elasticsearch-6.4.3和elasticsearch-head插件

    windows下安装elasticsearch-6.4.3和elasticsearch-head插件 博客分类: elasticsearch es  ElasticSearch下载地址:https:/ ...

  9. 5种JVM调优配置方法概览

    1 堆设置 -Xms:初始堆大小 -Xmx:最大堆大小 -XX:NewSize=n:设置年轻代大小 -XX:NewRatio=n:设置年轻代和年老代的比值.如:为3,表示年轻代与年老代比值为1:3,年 ...

  10. C/C++网络编程6——实现基于UDP的服务器端/客户端

    通过前面几节的内容,我们已经可以实现基本的C/S结构的程序了,但是当多个客户端同时向服务器端请求服务时,服务器端只能按顺序一个一个的服务,这种情况下,客户端的用户是无法忍受的.所以虚实现并发的服务器端 ...