閱讀596 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

Central Europe 2005
 
題目大意:有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;
}

最後更新:2017-04-02 15:28:25

  上一篇:go Android判斷網絡是否打開,並打開設置網絡界麵
  下一篇:go ucc 開源編譯器 C語言