Widget Factory (高斯消元解线性方程组)
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
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
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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <cmath>
using namespace std;
const int maxn = ;
int equ, var; // 有equ个方程,var个变元。增广阵行数为equ, 分别为0到equ - 1,列数为var + 1,分别为0到var.
int a[maxn][maxn];//增广矩阵
int x[maxn]; // 解集.
int free_num; 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*b/gcd(a,b);
}
// 高斯消元法解方程组(Gauss-Jordan elimination).(-2表示有浮点数解,但无整数解,-1表示无解,0表示唯一解,大于0表示无穷解,并返回自由变元的个数)
int change(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 return ;
}
int Gauss(void)
{
int i,j,k;
int max_r; // 当前这列绝对值最大的行.
int col; // 当前处理的列.
int ta, tb;
int LCM;
int temp;
// 转换为阶梯阵.
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)%+)%);
}
}
}
}
//Debug();
// 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
for (i = k; i < equ; i++)
{ // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
if (a[i][col] != ) return -;
}
// 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵.
// 且出现的行数即为自由变元的个数.
if (k < var)
return var - k; // 自由变元有var - k个.
// 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵.
// 计算出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]!=)//外层每次循环都是为了求 a[i][i],因为它是每个方程中唯一一个未知的变量(求该方程时)
temp+=;//因为天数不确定,而a[i][i]必须得为整数才可以,周期为7
x[i]=(temp/a[i][i])%;
}
return ;
} int main(void)
{
int n,m,k,num;
char s[],e[];
while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
{
memset(a,,sizeof(a));
for(int i=;i<m;i++)
{
scanf("%d",&k);
scanf("%s%s",s,e);
a[i][n]=((change(e)-change(s)+)%+)%;
for(int j=;j<=k;j++)//k是他打造的数量
{
scanf("%d",&num);//可能是相同的数
num--;
a[i][num]++;//系数++
a[i][num]%=;//有重复的。
}
}
equ=m;//有m个方程
var=n;//有多少个变量
free_num = Gauss();
if(free_num==)
{
for(int i=;i<n;i++)//根据题意,每个零件的加工时间在3-9天.
if(x[i]<=)
x[i]+=;
for(int i=;i<n-;i++)
cout<<x[i]<<" ";
cout<<x[n-]<<endl;
}
else if(free_num==-)
cout<<"Inconsistent data."<<endl;
else
cout<<"Multiple solutions."<<endl;
}
return ;
}
Widget Factory (高斯消元解线性方程组)的更多相关文章
- Poj 2947 widget factory (高斯消元解同模方程)
题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...
- POJ2947Widget Factory(高斯消元解同模方程)
http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai&l ...
- POJ 2947-Widget Factory(高斯消元解同余方程式)
题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...
- 题解【AcWing883】高斯消元解线性方程组
题面 高斯消元模板题. 这里直接讲述一下高斯消元的算法流程: 枚举每一列 \(c\): 找到第 \(c\) 列绝对值最大的一行: 将这一行换到最上面: 将该行的第一个数变成 \(1\): 将下面所有行 ...
- POJ 2947 2947 Widget Factory 高斯消元
给出组件的数量n,给出记录的数量m(n就是变元数量,m是方程数量).每一个记录代表一个方程,求每个组件的生产天数. 高斯消元即可 #include <cstdio> #include &l ...
- bzoj千题计划187:bzoj1770: [Usaco2009 Nov]lights 燈 (高斯消元解异或方程组+枚举自由元)
http://www.lydsy.com/JudgeOnline/problem.php?id=1770 a[i][j] 表示i对j有影响 高斯消元解异或方程组 然后dfs枚举自由元确定最优解 #in ...
- [置顶] hdu 4418 高斯消元解方程求期望
题意: 一个人在一条线段来回走(遇到线段端点就转变方向),现在他从起点出发,并有一个初始方向, 每次都可以走1, 2, 3 ..... m步,都有对应着一个概率.问你他走到终点的概率 思路: 方向问 ...
- 【BZOJ】2466: [中山市选2009]树 高斯消元解异或方程组
[题意]给定一棵树的灯,按一次x改变与x距离<=1的点的状态,求全0到全1的最少次数.n<=100. [算法]高斯消元解异或方程组 [题解]设f[i]=0/1表示是否按第i个点的按钮,根据 ...
- 【高斯消元解xor方程】BZOJ1923-[Sdoi2010]外星千足虫
[题目大意] 有n个数或为奇数或为偶数,现在进行m次操作,每次取出部分求和,告诉你这几次操作选取的数和它们和的奇偶性.如果通过这m次操作能得到所有数的奇偶性,则输出进行到第n次时即可求出答案:否则输出 ...
随机推荐
- JavaScript学习笔记--语法
代码风格: 每句结尾不用加分号: 单行注释用//,多行注释用/*...需要注释掉的代码....*/ 严格区分大小写 优雅的代码需要注意锁进 基础知识: Number:JavaScript不区分整数和浮 ...
- Linux系统启动排错实验集合
Centos6系统启动流程 1. post 加电自检 检查硬件环境 2. 选择一个硬件类型引导启动 mbr 446字节 grub stage1 3. 加载boot分区的文 ...
- .NET Core 指南 官方
https://docs.microsoft.com/zh-cn/dotnet/core/index
- 遮罩层出现后不能滚动 添加事件@touchmove.prevent
<div class="maskshow" @click="hidden_video" @touchmove.prevent></div> ...
- WIN32,_WIN32_WIN64
MSDN 里说,VC 有 3 个预处理常量,分别是 _WIN32,_WIN64,WIN32. 只要包含了 Windows.h,那么 WIN32 常量是肯定定义了的,所以不能用于判断平台环境(如果x64 ...
- rest_framework的认证系统
1.认证模块 必须用户登录之后才能访问所有图书,才能修改图片,才能查询单个图书 2.怎么使用 其实本质上就是携带token字符串,然后后台拿到数据再取数据库进行校验,看是否有这个用户 先手写一个认证模 ...
- one order 处理流程
- Java 基础 接口和多态
接口 接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成.这样将功能的 ...
- Centos7系统防火墙上开端口
//permanent 永久生效 没有此参数重启失效 firewall -cmd --zone=public --add -port=80/tcp --permanent //开 ...
- 利用Tensorflow实现卷积神经网络模型
首先看一下卷积神经网络模型,如下图: 卷积神经网络(CNN)由输入层.卷积层.激活函数.池化层.全连接层组成,即INPUT-CONV-RELU-POOL-FC池化层:为了减少运算量和数据维度而设置的一 ...