传送门: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. Epplus导出Excel(DataTable)

    1.先将dataTable转换成流 public Stream DataTableToExcel(DataTable dataTable, string[] columns, string sheet ...

  2. Unity 代码控制游戏对象是父物体的第多少个子对象

    一个canvas下的游戏对象,排列顺序越往下,渲染顺序就越靠后,就会覆盖在先前的图形上.也就是说,运行游戏后,物体的渲染顺序是一个一个计算的. Transform.SetSiblingIndex(in ...

  3. qs.parse()、qs.stringify()、JSON.parse()、JSON.stringify()使用方法

    一.JSON.parse(用于从一个字符串中解析出json 对象)ps:单引号写在{}外,每个属性都必须双引号,否则会抛出异常 let str = '[{"field":" ...

  4. 分享:JAVA和C# 3DES加密解密

    最近 一个项目.net 要调用JAVA的WEB SERVICE,数据采用3DES加密,涉及到两种语言3DES一致性的问题,下面分享一下,这里的KEY采用Base64编码,便用分发,因为Java的Byt ...

  5. ssh免密码登录快速配置方法

    环境需求: 两台Linux主机   A (192.168.3.101)和 B(192.168.3.102),如何使用主机 A 免密码登录 主机B ? 配置步骤: 首页登录主机 A ,在主机A上生成自己 ...

  6. pat1015. Reversible Primes (20)

    1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...

  7. maven module

    通过将一个maven项目拆分成多个module,会引入一定的项目复杂度,但随着后期项目代码的逐渐增多,最直观的感受是,每次build代码,不必build整个项目,可节省很多时间. 如果各个module ...

  8. oracle中时间戳转为Date类型的数据

    问题描述: 一个表中原本应该存放date类型的数据,但是不知道之前哪位大仙把两个字段的类型建成了NUMBER类型的了,这样在后台看时间肯定不方便.现在需要改成date类型,但是现在库中是有数据的,不能 ...

  9. Facebook 爬虫

    title: Facebook 爬虫 tags: [python3, facebook, scrapy, splash, 爬虫] date: 2018-06-02 09:42:06 categorie ...

  10. 小程序 - 图片列表显示lazyload效果

    在做一个短视频平台,涉及到的都是一些列表模块.因为小程序没有提供lazyload api,所以只能自己写一个了... 开发涉及 <scroll-view></scroll-view& ...