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;
}

UVA 1564 - Widget Factory(高斯消元)的更多相关文章

  1. POJ 2947 2947 Widget Factory 高斯消元

    给出组件的数量n,给出记录的数量m(n就是变元数量,m是方程数量).每一个记录代表一个方程,求每个组件的生产天数. 高斯消元即可 #include <cstdio> #include &l ...

  2. Poj 2947 widget factory (高斯消元解同模方程)

    题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...

  3. POJ2947Widget Factory(高斯消元解同模方程)

    http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai&l ...

  4. POJ 2947-Widget Factory(高斯消元解同余方程式)

    题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...

  5. UVA 1358 - Generator(dp+高斯消元+KMP)

    UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...

  6. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  7. 【POJ】2947 Widget Factory(高斯消元)

    http://poj.org/problem?id=2947 各种逗啊..还好1a了.. 题意我就不说了,百度一大把. 转换为mod的方程组,即 (x[1,1]*a[1])+(x[1,2]*a[2]) ...

  8. UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)

    UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...

  9. uva 1560 - Extended Lights Out(枚举 | 高斯消元)

    题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5∗6的矩阵,每一个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,假设按了这个位置的开关,将会导致周围包含自 ...

随机推荐

  1. SQL数值转字符串保留指定小数位

    IF EXISTS ( SELECT * FROM sysobjects WHERE xtype = 'fn' AND name = 'fn_NumberFormat' ) BEGIN DROP FU ...

  2. Filebeat的下载(图文讲解)

    第一步:进入Elasticsearch的官网 https://www.elastic.co/ 第二步:点击downloads https://www.elastic.co/downloads 第三步: ...

  3. HDU 2281 Square Number Pell方程

    http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...

  4. 洛谷 P1334 瑞瑞的木板

    P1334 瑞瑞的木板 题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了 ...

  5. Arch Linux实体机安装记录

    下面将记录笔者在戴尔笔记本安装arch linux的过程,用于记录,以便下次使用. 本文的内容参考arch linux官方Wiki. 首先,使用Power ISO把镜像安装到U盘,使用U盘安装. 通过 ...

  6. [Angular] Use :host-context and the ::ng-deep selector to apply context-based styling

    If you want to style host component. You can use ':host-context'. // host @Component({ selector: 'my ...

  7. 数学定理证明机械化的中国学派(II)

    所谓"学派"是指:存在一帮人,具有同样或接近的学术观点或学术立场,採用某种特定的"方法"(或途径),在一个学术方向上共同开展工作.而且做出了相当有迎影响的学术成 ...

  8. 24岁程序员, 一个人撑起App开发项目

    "疲惫吾心,怎样躲藏! 四处荒芜,怎话忧伤?"临近中秋,看到艾瑞斯的QQ签名,无尽的伤感.这个年仅24的青年.连续3年没有回家了,近期一个月总是失眠,没有家人的陪伴,就连女朋友都没 ...

  9. hdu 4932

    枚举差和差的1/2 #include <cstdio> #include <cstring> #include <algorithm> using namespac ...

  10. ImportError: No module named tornado.ioloop 记录过程

    ImportError: No module named tornado.ioloop 记录过程 安装 pycurl    pip install pycurl 报错 'curl-config' no ...