poj2947 Widget Factory(gauss--->≡)

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 writeInconsistent 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.

分析:
我一看题就觉得这是一道很裸的高斯消元
把柿子列出来套模板就行了

然而一周只有7天,但是最复杂的部件需要的时间是9天
显然如果我们只从星期几来看的话
得到的是时间花费时间%7之后的答案

所以这次我们的高斯消元要来求同余方程组了

这道题与之前的高斯消元略有不同
因为有m条信息,其中某一个信息可能对当前列没有贡献(该列的系数为0),
但是有可能使后面的某一列出现唯一解
所以我们要把这一行清空,然后放到所有信息的最后。

//这里写代码片
#include<cstring>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>

using namespace std;

int n,m,cnt;
int a[303][303],ans[303];

int KSM(int a,int b)
{
    int t=1;
    while (b)
    {
        if (b&1)
           t=(t*a)%7;
        b>>=1;
        a=(a*a)%7;
    }
    return t%7;
}

int get(char *s)
{
    if (s[0]=='M') return 1;
    if (s[0]=='W') return 3;
    if (s[0]=='F') return 5;
    if (s[0]=='T')
        if (s[1]=='U') return 2;
        else return 4;
    if (s[0]=='S')
        if (s[1]=='A') return 6;
        else return 7;  
}

int gauss()
{
    cnt=0;

    for (int i=1;i<=n;i++)   //枚举每一个未知量 
    {
        int num=i;
        for (int j=i+1;j<=m;j++)
            if (abs(a[j][i])>abs(a[num][i])) num=j;
        //找到一行当前列的系数不为0 
        if (!a[num][i])    //当前列的系数都为0 
        {
            cnt++;   //自由元 
            m++;
            for (int j=1;j<=n+1;j++)   //这一行有可能对之后的元素有限制作用 
                swap(a[m][j],a[num][j]);
        }
        else
        {
            for (int j=1;j<=n+1;j++)
                swap(a[num][j],a[i][j]);
        }

        for (int j=i+1;j<=m;j++)
            if (j!=i&&a[j][i])
            {
                int t=abs(a[j][i])*KSM(abs(a[i][i]),5);
                // double t=1/a[i][i]*a[j][i]
                //系数化为一,除法变成乘法逆元 
                t%=7;

                if (a[j][i]*a[i][j]<0) t=-t;

                for (int k=1;k<=n+1;k++)
                    a[j][k]-=t*a[i][k],
                    a[j][k]=(a[j][k]%7+7)%7;
            }
    }

    //回带求值
    for (int i=n;i>=1;i--)
    {
        //解的情况的判断 
        if (a[i][i]==0&&a[i][n+1]%7!=0) return -1;
        else if (!a[i][i]&&a[i][n+1]%7==0)   //自由元 
        {
            cnt=1;
            continue;
        }

        //答案的处理 
        ans[i]=((a[i][n+1]*KSM(a[i][i],5))%7+7)%7;  //系数化为一
        if (ans[i]<3) ans[i]+=7;

        //用第i个元素的答案,回带求解1~i-1 
        for (int j=i-1;j>=1;j--)
            if (a[j][i])
               a[j][n+1]-=ans[i]*a[j][i],
               ((a[j][n+1]%=7)+=7)%=7;  //-i的值*系数 
    } 

    for (int i=n+1;i<=m;i++)   //利用剩下的式子判断是否合法 
    {
        int sum=0;
        for (int j=1;j<=n;j++)
            sum+=ans[j]*a[i][j];
        sum=(sum%7+7)%7;
        if (sum!=a[i][n+1]%7) return -1;
    }

    if (cnt) return 2;
    else return 1; 
}

int main()
{
    scanf("%d%d",&n,&m);
    while (n&&m)
    {
        memset(a,0,sizeof(a));
        char s1[5],s2[5];
        for (int i=1;i<=m;i++)
        {
            int x,k,x1,x2;
            scanf("%d",&k);
            scanf("%s",&s1);
            scanf("%s",&s2);
            x1=get(s1);
            x2=get(s2);
            for (int j=1;j<=k;j++)
            {
                scanf("%d",&x);
                a[i][x]++; a[i][x]%=7;
            }
            a[i][n+1]=((x2-x1+1)%7+7)%7;
        }

        int opt=gauss();
        if (opt==2) printf("Multiple solutions.
");
        else if (opt==1)
        {
            for (int i=1;i<n;i++) printf("%d ",ans[i]);
            printf("%d
",ans[n]);
        }
        else printf("Inconsistent data.
");

        scanf("%d%d",&n,&m);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673072.html