传送门:http://poj.org/problem?id=2947

Widget Factory

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 7109   Accepted: 2496

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次工人的工作记录,每次记录包括 该工人处理了的零件数 、星期几开始 和 星期几结束(可能相隔很多个星期)。求制造每种零件所需要的时间。

解题思路:

N种零件就是N个未知数,M次操作就是M个方程。

根据M次操作构造出增广矩阵,高斯消元,不过求解过程要加上取模操作;

最后输出答案是特判一下答案是否小于等于2,如果是需要+7,因为我们运算的时候是取模运算,根据题意可知零件加工天数范围在【3,9】;

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = ; int a[MAXN][MAXN];//增广矩阵
int x[MAXN];//解集
bool free_x[MAXN];//标记是否是不确定的变元 inline int gcd(int a,int b) //最大公约数
{
int t;
while(b!=)
{
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=;i<=var;i++)
{
x[i]=;
free_x[i]=true;
} //转换为阶梯阵.
col=; // 当前处理的列
for(k = ;k < equ && col < var;k++,col++)
{// 枚举当前处理的行.
// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)
max_r=k;
for(i=k+;i<equ;i++)
{
if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
}
if(max_r!=k)
{// 与第k行交换.
for(j=k;j<var+;j++) swap(a[k][j],a[max_r][j]);
}
if(a[k][col]==)
{// 说明该col列第k行以下全是0了,则处理当前行的下一列.
k--;
continue;
}
for(i=k+;i<equ;i++)
{// 枚举要删去的行.
if(a[i][col]!=)
{
LCM = lcm(abs(a[i][col]),abs(a[k][col]));
ta = LCM/abs(a[i][col]);
tb = LCM/abs(a[k][col]);
if(a[i][col]*a[k][col]<)tb=-tb;//异号的情况是相加
for(j=col;j<var+;j++)
{
a[i][j] = ((a[i][j]*ta-a[k][j]*tb)%+)%;
}
}
}
} // 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
for (i = k; i < equ; i++)
{ // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
if ( a[i][col] != ) return -;
} if (k < var)
{
return var - k; // 自由变元有var - k个.
}
// 3. 唯一解的情况
// 计算出Xn-1, Xn-2 ... X0.
for (i = var - ; i >= ; i--)
{
temp = a[i][var];
for (j = i + ; j < var; j++)
{
if (a[i][j] != ) temp -= a[i][j] * x[j];
temp=(temp%+)%;
}
while (temp % a[i][i] != ) temp+=;
x[i] =( temp / a[i][i])% ;
}
return ;
} int sts(char *s)
{
if(strcmp(s, "MON") == ) return ;
else if(strcmp(s, "TUE") == ) return ;
else if(strcmp(s, "WED") == ) return ;
else if(strcmp(s, "THU") == ) return ;
else if(strcmp(s, "FRI") == ) return ;
else if(strcmp(s, "SAT") == ) return ;
else if(strcmp(s, "SUN") == ) return ;
} int main()
{
int N, M, xx, t;
char str1[], str2[];
while(~scanf("%d%d", &N, &M) && (N+M)){
memset(a, , sizeof(a));
for(int i = ; i < M; i++){
scanf("%d%s%s", &xx, str1, str2);
a[i][N] = ((sts(str2)-sts(str1)+)%+)%; for(int j = ; j < xx; j++){
scanf("%d", &t);
t--;
a[i][t]++;
a[i][t] = a[i][t]%;
}
}
int ans = Gauss(M, N);
if(ans == -) puts("Inconsistent data.");
else if(ans == ){
for(int i = ; i < N-; i++){
if(x[i] <= ) printf("%d ", x[i]+);
else printf("%d ", x[i]);
}
if(x[N-] <= ) printf("%d\n", x[N-]+);
else printf("%d\n", x[N-]);
}
else{
puts("Multiple solutions.");
}
}
return ;
}

POJ Widget Factory 【求解模线性方程】的更多相关文章

  1. POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))

    d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...

  2. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  3. POJ 2115 简单的模线性方程求解

    简单的扩展欧几里得题 这里 2^k 不能自作聪明的用 1<<k来写 , k >= 31时就爆int了 , 即使定义为long long 也不能直接这样写 后来老老实实 for(int ...

  4. poj_2115C Looooops(模线性方程)

    题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. 模线性方程&&中国剩余定理及拓展

    一.求解模线性方程 由ax=b(mod n) 可知ax = ny + b 就相当于ax + ny = b 由扩展欧几里得算法可知有解条件为gcd(a, n)整除d 可以直接套用扩展欧几里得算法 最终由 ...

  6. POJ 2142 TheBalance 模线性方程求解

    题目大意: 就是将两种砝码左右摆放,能够在物品放置在天平上时保持平衡 很容易得到 ax + by = t的模线性方程 按题目要求,希望首先满足 |x| + |y| 最小 , 如果有多种情况,再满足所有 ...

  7. poj 2947 Widget Factory

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

  8. POJ 1061 青蛙的约会(拓展欧几里得算法求解模线性方程组详解)

    题目链接: BZOJ: https://www.lydsy.com/JudgeOnline/problem.php?id=1477 POJ: https://cn.vjudge.net/problem ...

  9. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

随机推荐

  1. Oracle RAC集群搭建(zero)--全是报错

    1. 提示Check if the DISPLAYvariable is set.    Failed<<<< 解决方案: #xhost +  //切换到root用户输入 #s ...

  2. Ba Gua Zhen

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5544 学习链接:https://www.cnblogs.com/qscqesze/p/4902518. ...

  3. PHP unlink删除本地中文名称的文件

    由于编码不一样,用unlink()方法删除本地中文名称的材料之前,必须先转码,才能删除成功. 核心代码如下: //删除本地的议题材料(本地上传的材料)             if($local_ma ...

  4. jenkins~管道Pipeline里使用公用类库

    Pipeline使用了groovy语法,同时可以使用所有jenkins插件在groovy里进行调用,可以说通过UI可以实现的功能使用pipeline也可以实现,这一点我在上一篇文章里已经说明,今天主要 ...

  5. Markdown简易使用

    Markdown 笔记 标题 1.一级标题 2.二级标题 3.三级标题 列表 这是 一个 无序列表 这是 一个 有序列表 引用 这是一条引用 图片与链接 图片 链接 Baidu 粗体与斜体 粗体 斜体 ...

  6. 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11

    这篇博文,我们编写一个C语言的小程序,来解析数据段或者代码段描述符的各个字段.这样我们阅读原书的代码就会方便一点,只要运行这个小程序,就可以明白程序中定义的数据段或者代码段的描述符了. 这段代码,我用 ...

  7. nyoj 1205——简单问题——————【技巧题】

    简单问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 给你一个n*m的矩阵,其中的元素每一行从左到右按递增顺序排序,每一列从上到下按递增顺序排序,然后给你一些数x ...

  8. OC与JS交互之WebViewJavascriptBridge

    上一篇文章介绍了通过UIWebView实现了OC与JS交互的可能性及实现的原理,并且简单的实现了一个小的示例DEMO,当然也有一部分遗留问题,使用原生实现过程比较繁琐,代码难以维护.这篇文章主要介绍下 ...

  9. PHP常用的一些数组操作总结

    1.array_values() :返回包含数组中所有键值的数组,不保留键名. 2.array_diff() 函数返回两个数组的差集数组.该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的 ...

  10. 2、Spring之AOP

    AOP术语 通知:定义了切面是什么以及何时使用.除了要描述页面要完成的工作,通知还解决了何时执行这个工作的问题. 连接点:连接点是一个物理的存在.这个点可以是调用方法时.抛出异常时.甚至是修改一个字段 ...