【POJ】2947 Widget Factory(高斯消元)
http://poj.org/problem?id=2947
各种逗啊。。还好1a了。。
题意我就不说了,百度一大把。
转换为mod的方程组,即
(x[1,1]*a[1])+(x[1,2]*a[2])+...+(x[1,n]*a[n])=x[1, n+1] (mod m)
(x[2,1]*a[1])+(x[2,2]*a[2])+...+(x[2,n]*a[n])=x[2, n+1] (mod m)
...
(x[n,1]*a[1])+(x[n,2]*a[2])+...+(x[n,n]*a[n])=x[n, n+1] (mod m)
没有mod就是裸的高斯消元。。。
我们来考虑怎么消元。
显然如果有方程1和方程2,他们都有相同系数不为0的元素y,那么我们消元只需要将他们的系数调成一样即可,即调成公倍数,那么因为mod意义下满足a=b(mod m), a*c=b*c(mod m),所以将两个方程左式和右式都乘上这个公倍数即可。
而回代麻烦一些,减完当前方程其它元素的对应值后,此时假设当前元素的系数为A[i][i],而值是A[i][n+1],那么因为有x*A[i][i]=A[i][n+1](mod m),我们可以用拓展欧几里得或者一直加m,知道出现成立(此时因为一定有解,所以一定找得到一个x使得等式成立)
那么得到的x就是答案。
做的时候注意n和m是方程数还是未知量的数就行了。。
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- using namespace std;
- #define rep(i, n) for(int i=0; i<(n); ++i)
- #define for1(i,a,n) for(int i=(a);i<=(n);++i)
- #define for2(i,a,n) for(int i=(a);i<(n);++i)
- #define for3(i,a,n) for(int i=(a);i>=(n);--i)
- #define for4(i,a,n) for(int i=(a);i>(n);--i)
- #define CC(i,a) memset(i,a,sizeof(i))
- #define read(a) a=getint()
- #define print(a) printf("%d", a)
- #define dbg(x) cout << (#x) << " = " << (x) << endl
- #define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
- #define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
- inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
- inline const int max(const int &a, const int &b) { return a>b?a:b; }
- inline const int min(const int &a, const int &b) { return a<b?a:b; }
- const int N=305;
- typedef int mtx[N][N];
- string Dt[7]={"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
- int gauss(mtx A, int n, int m, int MD) {
- int x=1, y=1, pos;
- while(x<=n && y<=m) {
- pos=x;
- while(!A[pos][y] && pos<=n) ++pos;
- if(A[pos][y]) {
- for1(i, 1, m+1) swap(A[pos][i], A[x][i]);
- for1(i, x+1, n) if(A[i][y]) {
- int l=A[x][y], r=A[i][y];
- for1(j, y, m+1) A[i][j]=((A[i][j]*l-A[x][j]*r)%MD+MD)%MD;
- }
- ++x;
- }
- ++y;
- }
- for1(i, x, n) if(A[i][m+1]) return -1;
- if(x<=m) return m-x+1;
- for3(i, m, 1) {
- for1(j, i+1, m) if(A[i][j]) A[i][m+1]=((A[i][m+1]-(A[j][m+1]*A[i][j]))%MD+MD)%MD;
- while(A[i][m+1]%A[i][i]!=0) A[i][m+1]+=MD; //这里可以用拓欧搞掉。。
- A[i][m+1]=(A[i][m+1]/A[i][i])%MD;
- }
- return 0;
- }
- inline int get(string s) { return find(Dt, Dt+7, s)-Dt+1; }
- int main() {
- int n=getint(), m=getint(), t;
- char s[2][5];
- mtx a;
- while(n|m) {
- CC(a, 0);
- for1(i, 1, m) {
- read(t); scanf("%s%s", s[0], s[1]);
- a[i][n+1]=(get(s[1])-get(s[0])+1+7)%7;
- for1(j, 1, t) ++a[i][getint()];
- for1(j, 1, n) a[i][j]%=7;
- }
- int ans=gauss(a, m, n, 7);
- if(ans==-1) puts("Inconsistent data.");
- else if(ans) puts("Multiple solutions.");
- else {
- for1(i, 1, n) if(a[i][n+1]<3) a[i][n+1]+=7;
- for1(i, 1, n-1) printf("%d ", a[i][n+1]); printf("%d\n", a[n][n+1]);
- }
- n=getint(), m=getint();
- }
- return 0;
- }
Description
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
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
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
Source
【POJ】2947 Widget Factory(高斯消元)的更多相关文章
- Poj 2947 widget factory (高斯消元解同模方程)
题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...
- POJ 2947 2947 Widget Factory 高斯消元
给出组件的数量n,给出记录的数量m(n就是变元数量,m是方程数量).每一个记录代表一个方程,求每个组件的生产天数. 高斯消元即可 #include <cstdio> #include &l ...
- POJ 2947 Widget Factory(高斯消元)
Description The widget factory produces several different kinds of widgets. Each widget is carefully ...
- POJ 2947-Widget Factory(高斯消元解同余方程式)
题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...
- POJ2947Widget Factory(高斯消元解同模方程)
http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai&l ...
- poj 2947 Widget Factory
Widget Factory 题意:有n件装饰品,有m组信息.(1 <= n ,m<= 300)每组信息有开始的星期和结束的星期(是在mod 7范围内的)并且还包括num种装饰品的种类(1 ...
- POJ 1830 开关问题 高斯消元,自由变量个数
http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...
- A - The Water Bowls POJ - 3185 (bfs||高斯消元)
题目链接:https://vjudge.net/contest/276374#problem/A 题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置, ...
- POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)
依据题意可构造出方程组.方程组的每一个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki; 高斯消元构造上三角矩阵,以最后一个一行为例: C*x9 = ...
- POJ 2065 SETI(高斯消元)
题目链接:http://poj.org/problem?id=2065 题意:给出一个字符串S[1,n],字母a-z代表1到26,*代表0.我们用数组C[i]表示S[i]经过该变换得到的数字.给出一个 ...
随机推荐
- Activity生命周期 与 Activity 之间的通信
一. Activity生命周期 上图 1. Activity状态 激活状态 : Activity出于前台 , 栈顶位置; 暂停状态 : 失去了焦点 , 但是用户仍然可以看到 , 比如弹出一个对话框 , ...
- php添加扩展 在phpinfo能看到该扩展,但在cli用php -m 却看不到,为什么呢,求指教
1. 没有出现的原因是:执行时添加上php.ini的文件就可以了 $ /usr/local/php/bin/php -c /usr/local/php/etc/php.ini -m | grep ...
- 制作QQ微信支付宝三合一收款码
转载:http://blog.mambaxin.com/article/56 发现很多博客都带了打赏功能,虽说打赏的人可能很少,但始终是一份心意,能让博主知道自己写的文章有用,能够帮助到人.所以,我也 ...
- Atom IDE开发工具, ASCII艺术评论, ninimap 插件
1 ASCII Art Comments One neat trick is to use ASCII art to create huge comments visible in the minim ...
- 第53天:鼠标事件、event事件对象
-->鼠标事件-->event事件对象-->默认事件-->键盘事件(keyCode)-->拖拽效果 一.鼠标事件 onclick ---------------鼠标点击事 ...
- 限制玻尔兹曼机(Restricted Boltzmann Machine)RBM
假设有一个二部图,每一层的节点之间没有连接,一层是可视层,即输入数据是(v),一层是隐藏层(h),如果假设所有的节点都是随机二值变量节点(只能取0或者1值)同时假设全概率分布满足Boltzmann 分 ...
- BZOJ4237 稻草人(分治+树状数组+单调栈)
如果要询问的某个纵坐标为inf的点左边是否有点能与其构成所要求的矩形,只要用个单调栈就可以了.可以想到用分治来制造单调性. 按横坐标排序,每次考虑跨过分治中心的矩形.考虑右边的每个点能与左边的哪些点构 ...
- VS2017常用快快捷键
VS2017常用快快捷键 VS中代码对齐等快捷键 在VS中,选定代码后,按Ctrl+K+F组合键,可以自动进行代码对齐. 注意:要先按下Ctrl和K,再按下F,因为Ctrl ...
- 【luogu2181】对角线
首先由于不会有三条对角线交于一点,所以过某一个交点有且只能有2条对角线 而这两条对角线实质上是确定了4个顶点(也可以看做是一个四边形的两条对角线交于一点,求四边形的数量). 因此我们只需要确定4个顶点 ...
- java.util.Stack类简介(栈)
Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来 Deque(双端队列)比起stack具有更好的完整性和一致性,应该被优先使用 ...