POJ2947高斯消元+同余方程

Widget Factory
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 3453 Accepted: 1139

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

 
题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:
p start end
a1,a2......ap (1<=ai<=n)
第一行表示从星期start 到星期end 一共生产了p 件装饰物(工作的天数为end-start+1+7*x,
加7*x 是因为它可能生产很多周),第二行表示这p 件装饰物的种类(可能出现相同的种类,
即ai=aj)。规定每件装饰物至少生产3 天,最多生产9 天。问每种装饰物需要生产的天数。
如果没有解,则输出“Inconsistent data.”,如果有多解,则输出“Multiple solutions.”,如果
只有唯一解,则输出每种装饰物需要生产的天数。
解题思路:高斯消元。设每种装饰物需要生产的天数为xi(1<=i<=n)。每一个条件就相当于
给定了一个方程式,假设生产1 类装饰物a1 件、2 类装饰物a2 件、i 类装饰物ai 件所花费
的天数为b,则可以列出下列方程:
a1*x1+a2*x2+...an*xn = b (mod 7)
这样一共可以列出m 个方程式,然后使用高斯消元来解此方程组即可。
(源自林大陈宇老师)
 
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
#define maxn 305
int equ,var,prime;
//char st[80];
char data[7][5]= {"MON","TUE","WED","THU","FRI","SAT","SUN"};
int aa[maxn][maxn],x[maxn];
inline int abs1(int x)
{
    if (x>=0) return x;
    else
        return -1*x;
}
inline int gcd(int a, int b)
{
    int t;
    while (b != 0)
    {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}
inline int lcm(int a, int b)
{
    return a * b / gcd(a, b);
}
int extgcd(int a, int b, int & x, int & y)
{
    if (b == 0)
    {
        x=1;
        y=0;
        return a;
    }
    int d = extgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - a / b * y;
    return d;
}
int Gauss()
{
    int i,j,k;
    int max_r , col , temp;
    int LCM , GCD;
    int ta,tb;
    col = 0;
    for(k=0 ; k<equ && col < var ; k++,col++)
    {
        max_r = k;
        for(i=k+1 ; i<equ ; i++)
        {
            if(abs1(aa[i][col]) > abs1(aa[max_r][col]))max_r = i;
        }
        if(max_r != k)
        {
            for(j=k ; j<var+1 ; j++)swap(aa[k][j],aa[max_r][j]);
        }
        if(aa[k][col] == 0)
        {
            k--;
            continue;
        }
        for(i=k+1 ; i<equ ; i++)
        {
            if(aa[i][col] != 0)
            {
                LCM = lcm(abs1(aa[i][col]) , abs1(aa[k][col]));
                ta = LCM/abs1(aa[i][col]) ;
                tb = LCM/abs1(aa[k][col]);
                if(aa[i][col] * aa[k][col] < 0)tb = -tb;
                for(j=col ; j<var+1 ; j++)
                {
                    aa[i][j] = (aa[i][j]*ta-aa[k][j]*tb) % prime;
                    aa[i][j]=(aa[i][j]%7+7)%7;
                }
            }
        }//for
    }
    for(i=k; i<equ; i++)
    {
        if (aa[i][col]!=0) return -1;//无解
    }
    if (k<var) return var-k;// 无穷多解
    for(i=var-1 ; i>=0 ; i--)
    {
        temp = aa[i][var];
        for(j=i+1 ; j<var ; j++)
        {
            if(aa[i][j] != 0) temp =(temp - aa[i][j]*x[j]%prime) ;
//temp=temp-aa[i][j]*x[j];
        }
        temp = (temp%prime + prime) % prime;
        GCD = extgcd(aa[i][i] , prime , x[i] , k);
        x[i] = ( (x[i]*(temp/GCD) % prime) + prime) % prime;
// while(temp%aa[i][i])
// temp+=7;
//x[i]=temp/aa[/i][i];
//x[i]=(x[i]%7+7)%7;
        while(x[i]<3) x[i]=x[i]+7;
//while(x[i]>9) x[i]=x[i]-7;
    }
    return 0;
}
//void init()
//{
// int n=var;
//}
int comp_1(char a[])
{
    int j=0;
    for(int i=0; i<7; i++)
        if (strcmp(a,data[i])==0) j=i;
    cout<<"s="<<j<<endl;
    return j;
}
int main()
{
//`MON', `TUE', `WED', `THU', `FRI', `SAT' and `SUN'.
    int m,n,k,typ;
    int num1,num2;
    char s1[5],s2[5];
    prime=7;
    while(scanf("%d%d",&m,&n))
    {
        if (m==0&&n==0) break;
        var=m; //这个要注意,var!=equ 啊
        equ=n;
//var=equ=m;
        memset(aa,0,sizeof(aa));
        memset(x,0,sizeof(x));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&k);
            scanf("%s %s",s1,s2);
            for(int i=0; i<7; i++)
            {
                if (strcmp(s1,data[i])==0)
                    num1=i;
                if (strcmp(s2,data[i])==0)
                    num2=i;
            }
            int day=(num2-num1+1+7)%7;
// cout<<day<<endl;
            for(int j=0; j<k; j++)
            {
                scanf("%d",&typ);
                aa[i][typ-1]++;
                aa[i][typ-1]%=7;
            }
            aa[i][m]=day;
        }
        int free_num=Gauss();
        if (free_num==-1) cout<<"Inconsistent data."<<endl;
        if (free_num>0) cout<<"Multiple solutions."<<endl;
        if (free_num==0)
        {
            for(int i=0; i<var-1; i++)
                cout<<x[i]<<" ";
            cout<<x[var-1]<<endl;
        }
    }
//cout << "Hello world!" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值