POJ Widget Factory 【求解模线性方程】
传送门:http://poj.org/problem?id=2947
Widget Factory
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 7109 | Accepted: 2496 |
Description
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
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
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
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 【求解模线性方程】的更多相关文章
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...
- POJ 2115 简单的模线性方程求解
简单的扩展欧几里得题 这里 2^k 不能自作聪明的用 1<<k来写 , k >= 31时就爆int了 , 即使定义为long long 也不能直接这样写 后来老老实实 for(int ...
- poj_2115C Looooops(模线性方程)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 模线性方程&&中国剩余定理及拓展
一.求解模线性方程 由ax=b(mod n) 可知ax = ny + b 就相当于ax + ny = b 由扩展欧几里得算法可知有解条件为gcd(a, n)整除d 可以直接套用扩展欧几里得算法 最终由 ...
- POJ 2142 TheBalance 模线性方程求解
题目大意: 就是将两种砝码左右摆放,能够在物品放置在天平上时保持平衡 很容易得到 ax + by = t的模线性方程 按题目要求,希望首先满足 |x| + |y| 最小 , 如果有多种情况,再满足所有 ...
- poj 2947 Widget Factory
Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...
- POJ 1061 青蛙的约会(拓展欧几里得算法求解模线性方程组详解)
题目链接: BZOJ: https://www.lydsy.com/JudgeOnline/problem.php?id=1477 POJ: https://cn.vjudge.net/problem ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
随机推荐
- oracle 基础知识(三)--SCN
一,SCN的介绍 SCN(System Change Number),也就是通常所说的系统改变号或者系统提交号,是数据库中非常重要的一个数据结构. SCN用以标识数据库在某个确切时刻提交的版本 ...
- 图解 TCMalloc
https://zhuanlan.zhihu.com/p/29216091 图解 TCMalloc hellocode 永远年轻 693 人赞了该文章 前言 TCMalloc 是 Google 开 ...
- 图解CSS的padding,margin,border属性(详细介绍及举例说明)
图解CSS的padding,margin,border属性 W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层 ...
- bzoj 4574: [Zjoi2016]线段树
Description 小Yuuka遇到了一个题目:有一个序列a_1,a_2,?,a_n,q次操作,每次把一个区间内的数改成区间内的最大值,问 最后每个数是多少.小Yuuka很快地就使用了线段树解决了 ...
- 【VMware】VMware的安装和更改虚拟机默认存储路径
1.VMware Workstation 14中文破解版安装 网盘下载链接:https://pan.baidu.com/s/1pbDXpgpNQTdTRzzKNWfE1A linux镜像(CentOS ...
- UML建模—EA的使用起步
Enterprise Architect(EA) 是一个功能比较强悍的建模工具. 对于一个软件设计者来说,从需求分析到业务设计.类模型设计.数据库设计到测试.发布.部署等一系列软件设计必须的操作都可以 ...
- SQL Server与Oracle有什么区别?
1.可操作平台上: Oracle可在所有主流平台上运行,Oracle数据库采用开放的策略目标,它使得客户可以选择一种最适合他们特定需要的解决方案.客户可以利用很多种第三方应用程序.工具.而SQL Se ...
- Vue1.0基础学习笔记整理
最近一直在使用Vue.js开发项目,现将在学习过程中遇到的一些学习小细节总结如下: 1.只处理单次插值,今后的数据变化就不会再引起插值更新了 <span>This will never c ...
- java冒泡排序 常规排序和优化排序
冒泡排序原理在于两两比较,看你需要把大值放最前面还是最后面, 我们看看把大值放在后面:比如数组[7, 5, 6] 开始排序第1个数字 :7 arr:[7, 5, 6] 开始排序第2个数字 :5 arr ...
- js之可迭代对象
遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...