传送门: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. 抱歉最近朋友结婚去浪了几天~未来几天会正常更新.今天带来XML文件解析

    三种解析方法 DOM,SAX,XMLPullParse;你以为我要讲三种嘛 错 ,我只讲一种 ,其他两种我只是说下优缺点, 一.DOM解析器 优点: DOM解析器在解析XML文档时,会把文档中的所有元 ...

  2. jquery 添加关键字小插件

    模块化封装,兼容seajs /** * User: c3t * Date: 14-3-25 * Time: 下午4:16 */ define(function (require, exports, m ...

  3. java io 学习笔记(三) 字符流读写

    1.字符流读取 字符流读取的所有类都是从Reader这个超类继承的,都是用于读取字符的,这些类分别是InputSteamReader(从字符流读取).FileReader(继承与InputStream ...

  4. MYSQL冷知识——ON DUPLICATE KEY 批量增删改

    一 有个需求要批量增删改,并且是混合的,也就是仅不存在才增. 删简单,因为有个deleteStaute之类的字段,删除本质上就是就是一个修改 所以就是实现批量混合增改,然而组长说mysql不支持混合增 ...

  5. 获取全球dns统计信息

    # -*- coding:UTF-8 -*- import requests, time import json from bs4 import BeautifulSoup as bp t3 = ti ...

  6. 简单springboot及springboot cloud环境搭建

    springboot使用特定的方式,简化了spring的各种xml配置文件,并通过maven或者gradle,完成所需依赖,使用springboot maven插件,可直接输出可运行的jar包,省去了 ...

  7. 网站部署中遇到的问题-编译器错误信息: CS0016

    问题描述: 访问站点出现错误win7 x64+iis7.5 配置错误:CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\ ...

  8. nodejs 实践:express 最佳实践(四) express-session 解析

    nodejs 实践:express 最佳实践(四) express-session 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs ...

  9. Etcd入门教程

    etcd是一个类似于zk的工具,用于保存值,节点-值这种映射关系的.节点组织结构类似unix文件系统结构,从/最开始.比如一个/test/name节点,值为guanxianseng.可以通过etcdc ...

  10. iOS开发之GCD基础

    重新回顾.学习GCD.Block.先贴出一篇不错的讲解GCD基础使用的文章 原文地址:http://blog.csdn.net/aolan1108/article/details/17283415 做 ...