POJ 2947 Widget Factory(高斯消元解同余线性方程)
作者:互联网
Widget Factory
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 4417 Accepted: 1493
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.
题意:
n个物品,每个物品要生产3~9天。m个人的工作情况,每个生产k种装饰品,从星期X生产到星期Y(未必是同一个星期,一天只能生产一个产品),然后给出这k种物品分别是什么。问是否能求出n个物品分别须要多少天来生产,若有多组解输出Multiple solutions.,无解输出Inconsistent data.。
思路:高斯消元的例题
列出m个方程组成方程组。对于每一个人,设k[i]为生产装饰品 i 多少次,xi为生产装饰品 i 需要多少天,那么可以列出方程
k1 * x1 + k2 * x2 + …… + kn * xn = Y - X + 1(mod 7)。……
注意本题放入取模,模板中的有浮点数的解可以直接不要,但除法取模需要逆元
//可以解同余方程组
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#define nmax 510
using namespace std;
int a[nmax][nmax];
int x[nmax];
int free_x[nmax],mod=7;
int gcd(int a,int b){
if(!b) return a; else return gcd(b,a%b);
}
long long inv(long long a,long long m){
if(a == 1)return 1;
return inv(m%a,m)*(m-m/a)%m;
}
int lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
//-2表示有浮点数解,但无整数解,-1表示无解.0表示唯一解,大于0表示无穷解,并返回自由变元的个数
//有equ个方程,var个变元,增广矩阵行数为equ,分别为0到equ-1,列数为var+1,分别为0到var.
int Gauss(int equ,int var){
//max_r当前这列绝对值最大的行.col:当前处理的列.
int k,max_r,col = 0,ta,tb;
int LCM,temp,num = 0,free_index;
for(int i=0;i<=var;i++){
x[i]=0;
free_x[i]=true;
}
for(k = 0;k < equ && col < var;k++,col++){
// 枚举当前处理的行.
// 找到该col列元素绝对值最大的那行与第k行交换.(为了在除法时减小误差)
max_r=k;
for(int i=k+1;i<equ;i++){
if(abs(a[i][col])>abs(a[max_r][col]))
max_r=i;
}
if(max_r!=k){// 与第k行交换.
for(int j=k;j<var+1;j++)
swap(a[k][j],a[max_r][j]);
}
if(a[k][col]==0){// 说明该col列第k行以下全是0了,则处理当前行的下一列.
free_x[num++] = col;
k--;
continue;
}
for(int i=k+1;i<equ;i++){// 枚举要删去的行.
if(a[i][col]!=0){
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]<0)tb=-tb;//异号的情况是相加
for(int j=col;j<var+1;j++){
a[i][j] = ((a[i][j]*ta-a[k][j]*tb)%mod+mod)%mod;
}
}
}
}
// 1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0).
for (int i = k; i < equ; i++){
if (a[i][col] != 0) return -1;
}
// 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 (int i = var - 1; i >= 0; i--){
temp = a[i][var];
for (int j = i + 1; j < var; j++){
if (a[i][j] != 0)
temp =((temp- a[i][j] * x[j])%mod+mod)%mod;
}
// if (temp % a[i][i] != 0) return -2; // 说明有浮点数解,但无整数解.
while (temp % a[i][i] != 0) //本题的变化,其实直接不加这句也对,为下面的除法取模用了逆元
temp+=mod; // 说明有浮点数解,但无整数解.
x[i] = (temp*inv(a[i][i],mod))%mod;
}
return 0;
}
void init()
{
memset(a, 0, sizeof(a));
memset(x, 0, sizeof(x));
}
map<string,int> mp;
int main()
{
mp["MON"]=1;
mp["TUE"]=2;
mp["WED"]=3;
mp["THU"]=4;
mp["FRI"]=5;
mp["SAT"]=6;
mp["SUN"]=7;
int n,m;
while(scanf("%d%d",&n,&m)!=-1)
{
if(n==0&&m==0) break;
init();
char s1[15],s2[15];
for(int i=0;i<m;i++)
{
int k;
scanf("%d %s %s",&k,s1,s2);
a[i][n]=((mp[s2]-mp[s1]+1)%mod+mod)%mod;
int t;
while(k--)
{
scanf("%d",&t);
t--;
a[i][t]++;
a[i][t]%=mod;
}
}
int flag = Gauss(m,n);
if(flag == -1)
printf("Inconsistent data.\n");
else if(flag > 0)
printf("Multiple solutions.\n");
else
{
for(int i = 0; i < n; i++)
if(x[i] <= 2)
x[i] += 7;///根据题意要求天数必须在3~9之间
for(int i = 0; i < n-1; i++)
printf("%d ",x[i]);
printf("%d\n",x[n-1]);
}
}
return 0;
}
标签:widget,int,线性方程,Widget,days,widgets,include,高斯消,mod 来源: https://blog.csdn.net/sdz20172133/article/details/88923833