【POJ 2947】Widget Factory(高斯消元+逆元)

【POJ 2947】Widget Factory(高斯消元+逆元)

Widget Factory
Time Limit: 7000MSMemory Limit: 65536K
Total Submissions: 5464Accepted: 1884

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

题目大意:
m道工序,n个产品。
已知每道工序开始和截止日期,还有包含的产品。
已知工序总耗时为产品耗时累加和,工序耗时最少3天最多一天。

拿样例1举例
2 3
2 MON THU
1 2
3 MON FRI
1 1 2
3 MON SUN
1 2 2

工三道工序,两件产品。
工序1:生产产品1一件 2一件 从周一开始到周二结束。
可列方程 x1+x2=2(mod7)
工序2:周一到周五
2x1+x2=5(mod7)
工序3:周一到周天
x1+2x2=0(mod7)

高斯消元时取余即可求解。
有变元或者无解直接输出题目给出语句即可

代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 7;
const double eps = 1e-8;

int x[333],a[333][333];

int getd(char *ch)
{
    switch(ch[0])
    {
        case 'M':
            return 1;
        case 'T':
            return ch[1] == 'U'? 2: 4;
        case 'W':
            return 3;
        case 'F':
            return 5;
        case 'S':
            return ch[1] == 'A'? 6: 7;
    }
}

int gcd(int a,int b)
{
    while(b)
    {
        int t = b;
        b = a%b;
        a = t;
    }
    return a;
}

int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}

LL inv(LL a,LL m)
{
    if(a == 1) return 1;
    return inv(m%a,m)*(m-m/a)%m;
}

int Gauss(int equ,int var)
{
    int max_r,col,k;

    for(k = 0, col = 0; k < equ && col < var; ++k,++col)
    {
        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(a[max_r][col] == 0)
        {
            --k;
            continue;
        }
        if(max_r != k)
            for(int j = col; j < var+1; ++j)
                swap(a[k][j],a[max_r][j]);
        for(int i = k+1; i < equ; ++i)
        {
            if(a[i][col] != 0)
            {
                int LCM = lcm(abs(a[i][col]),abs(a[k][col]));
                int ta = LCM/abs(a[i][col]);
                int 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;
            }
        }
    }
    for(int i = k; i < equ; ++i)
        if(a[i][col] != 0) 
        {
    //        printf("a[%d][%d] %d\n",i,col,a[i][col]);
            return -1;//无解
        }
    if(k < var) return var-k;//多解
    for(int i = var-1; i >= 0; --i)
    {
        int temp = a[i][var];
        for(int j = i+1; j < var; ++j)
        {
            if(a[i][j] != 0)
            {
                temp -= a[i][j]*x[j];
                temp = (temp%mod+mod)%mod;
            }
        }
        x[i] = (temp*inv(a[i][i],mod))%mod;
    }
    return 0;
}

void solve(int equ,int var)
{
    int k = Gauss(equ,var);
    if(k == -1) puts("Inconsistent data.");
    else if(k) puts("Multiple solutions.");
    else
    {
        for(int i = 0; i < var; ++i)
        {
            if(i) putchar(' ');
            printf("%d",x[i] + (x[i] <= 2? 7: 0));
        }
        puts("");
    }
}

int main()
{
    //fread("");
    //fwrite("");

    int n,m;
    int k,d;
    char st[10],en[10];

    while(~scanf("%d%d",&n,&m) && (n+m))
    {
        memset(a,0,sizeof(a));
        memset(x,0,sizeof(x));

        for(int i = 0; i < m; ++i)
        {
            scanf("%d%s%s",&k,st,en);
            a[i][n] = (getd(en)-getd(st)+1+mod)%mod;
            while(k--)
            {
                scanf("%d",&d);
                a[i][d-1]++;
                a[i][d-1] %= mod;
            }
        }

//      for(int i = 0; i < m; ++i)
//      {
//          for(int j = 0; j < n; ++j)
//          {
//              if(j) putchar('+');
//              printf(" %dX%d ",a[i][j],j+1);
//          }
//          printf(" = %d\n",a[i][n]);
//      }

        solve(m,n);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值