POJ 2947 高斯消元

来源:互联网 发布:linux如何查看内核日志 编辑:程序博客网 时间:2024/06/01 12:10

题目

Widget Factory
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 5331 Accepted: 1842
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.

题意

工厂破产了,有n个零件m个工人。每个工人加工一些零件,只有记录工人开始工作和被开除的时间是星期几。问每种零件的生产时间是多久

题解

对每个工人
a1*x1+a2*x2+…an*xn = b (mod 7)
由此可以列出一组方程,然后用线性代数里的高斯消元求出一组解。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>#include <string>#include <set>#include <cmath>#include <map>#include <queue>#include <sstream>#include <vector>#include <iomanip>#define m0(a) memset(a,0,sizeof(a))#define mm(a) memset(a,0x3f,sizeof(a))#define m_1(a) memset(a,-1,sizeof(a))#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)#define lowbit(a) ((a)&(-a))#define FFR freopen("data.in","r",stdin)#define FFW freopen("data.out","w",stdout)#define INF 0x3f3f3f3ftypedef long long ll;typedef long double ld;const ld PI = acos(-1.0);using namespace std;#define SIZE ( )map<string, int>ma;int a[400][400];int ans[400];int N,M;int extend_gcd(int A, int B, int &x, int &y) {    if (B == 0) {        x = 1, y = 0;        return A;    } else {        int r = extend_gcd(B, A%B, x, y);        int t = x;        x = y;        y = t - A / B*y;        return r;    }}int lcm(int A, int B) {    int x = 0, y = 0;    return A*B / extend_gcd(A, B, x, y);}void Guass() {    int i, j, row, col;    for (row = 1, col = 1; row <= M && col <= N; row++, col++) {        for (i = row; i <= M; i++)            if (a[i][col]) break;        if (i == M+1) {            row--;            continue;        }        if (i != row)            for (j = 1; j <= N+1; j++) swap(a[row][j], a[i][j]);        for (i = row + 1; i <= M; i++) {            if (a[i][col]) {                int LCM = lcm(a[row][col], a[i][col]);//利用最小公倍数去化上三角                int ch1 = LCM / a[row][col], ch2 = LCM / a[i][col];                for (j = col; j <= M; j++)                    a[i][j] = ((a[i][j] * ch2 - a[row][j] * ch1) % 7 + 7) % 7;            }        }    }    for (i = row; i <= M; i++)//无解    {        if (a[i][N+1] != 0) {            printf("Inconsistent data.\n");            return;        }    }    if (row < N+1)//无穷多解    {        printf("Multiple solutions.\n");        return;    }    //唯一解时    for (i = N; i >= 1; i--) {        int ch = 0;        for (j = i + 1; j <= N; j++) {            ch = (ch + ans[j] * a[i][j] % 7) % 7;        }        int last = ((a[i][N+1] - ch) % 7 + 7) % 7;        int x = 0, y = 0;        int d = extend_gcd(a[i][i], 7, x, y);        x %= 7;        if (x < 0) x += 7;        ans[i] = last*x / d%7;        if (ans[i] < 3) ans[i] += 7;    }    for (int i = 1; i <= N; i++) {        if (i == 1)            printf("%d", ans[i]);        else            printf(" %d", ans[i]);    }    printf("\n");}int main() {    //ios_base::sync_with_stdio(false); cin.tie(0);    //`MON', `TUE', `WED', `THU', `FRI', `SAT' and `SUN'.    ma["MON"] = 1; ma["TUE"] = 2;    ma["WED"] = 3; ma["THU"] = 4;    ma["FRI"] = 5; ma["SAT"] = 6;    ma["SUN"] = 7;    while (~scanf("%d%d", &N, &M)&&(N||M)) {        int k;        char s[5],e[5];        int i, j;        m0(a);        m0(ans);        f(i, 1, M) {            scanf("%d %s %s", &k, s, e);            f(j, 1, k) {                int b;                scanf("%d", &b);                a[i][b]++;                a[i][b] %= 7;            }            a[i][N + 1] = ((ma[e] - ma[s]+1) % 7+7)%7;        }        Guass();    }    return 0;}
0 0
原创粉丝点击