UVA 1564 - Widget Factory(高斯消元)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   ar   代碼   

UVA 1564 - Widget Factory

題目連結

題意:n種零件, 給定m個製作時間,每段時間製作k個零件,每種零件有一個製作時間,每段時間用Mon到Sun表示,求每個零件的製作時間,還要判斷一下多解和無解的情況

思路:對於每段時間列出一個方程,這樣一共列出m個方程解n個變元,利用高斯消元去求解,注意每個方程都是MOD 7的,所以在高斯消元過程中遇到除法要求該數字%7的逆元去進行運算

代碼:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 305;char week[7][5] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};int n, m, k, A[N][N], cnt[N];char day1[5], day2[5];int find(char *day) {    for (int i = 0; i < 7; i++)if (strcmp(week[i], day) == 0)    return i;}int inv(int x) {    int ans = 1;    for (int i = 0; i < 5; i++)ans = ans * x % 7;    return ans;}void build() {    for (int i = 0; i < m; i++) {scanf("%d%s%s", &k, day1, day2);A[i][n] = (find(day2) - find(day1) + 8) % 7;int tmp;memset(cnt, 0, sizeof(cnt));while (k--) {    scanf("%d", &tmp);    cnt[tmp]++;}for (int j = 1; j <= n; j++)    A[i][j - 1] = cnt[j] % 7;    }}int gauss() {    int i = 0, j = 0;    while (i < m && j < n) {int r;for (r = i; r < m; r++)    if (A[r][j]) break;if (r == m) {    j++;    continue;}for (int k = j; k <= n; k++) swap(A[r][k], A[i][k]);for (int k = 0; k < m; k++) {    if (i == k) continue;    if (A[k][j]) {int tmp = A[k][j] * inv(A[i][j]) % 7;for (int x = j; x <= n; x++)    A[k][x] = ((A[k][x] - tmp * A[i][x]) % 7 + 7) % 7;    }}i++;    }    for (int k = i; k < m; k++)if (A[k][n]) return 2;    if (i < n) return 1;    for (int i = 0; i < n; i++) {int ans = A[i][n] * inv(A[i][i]) % 7;if (ans < 3) ans += 7;printf("%d%c", ans, i == n - 1 ? '\n' : ' ');    }    return 0;}void solve() {    int tmp = gauss();    if (tmp == 1) printf("Multiple solutions.\n");    else if (tmp == 2) printf("Inconsistent data.\n");}int main() {    while (~scanf("%d%d", &n, &m) && n) {build();solve();    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.