poj 2947 Widget Factory(高斯消元)

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.

题意就是给定工人们所造物品及所需时间,求得每个物品完工所需时间

如果将题目给看明白就发现这是一个解多元一次模方程组,高斯消元就好了

要记住高斯消元的细节,做好代码笔记

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<string>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<iostream>
  8 #define mem(a) memset(a,0,sizeof a)
  9 using namespace std;
 10 const int N=310;
 11 typedef long long ll;
 12 int a[N][N];
 13 int n,m;
 14 int cnt[N],ans[N];
 15 char st[10],ed[10];
 16 const char s[7][10]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
 17 int trans(char *x){
 18     for(int i=0;i<7;++i)
 19         if(strcmp(s[i],x)==0)return i;
 20     return 0;
 21 }
 22 void init(){
 23     //n var    m equ
 24     for(int i=0;i<m;++i){
 25         mem(cnt);
 26         int k;scanf("%d",&k);
 27         scanf("%s%s",st,ed);
 28         for(int j=0;j<k;++j){
 29             int x;
 30             scanf("%d",&x);
 31             cnt[x-1]++;
 32         }
 33         a[i][n]=(trans(ed)-trans(st)+8)%7;
 34         for(int j=0;j<n;++j)a[i][j]=cnt[j]%7;
 35     }
 36 }
 37 int FPM(int a,int b){
 38     int res=1;
 39     for(;b;b>>=1,a=a*a%7)
 40         if(b&1)res=res*a%7;
 41     return res;
 42 }
 43 int Lcm(int a,int b){
 44     return a/__gcd(a,b)*b;
 45 }
 46 int Gauss(){
 47     int col=0,k=0;
 48     for(;k<m&&col<n;++k,++col){
 49         int Max=0,row=-1;
 50         for(int r=k;r<m;++r)
 51             if(Max<abs(a[r][col]))
 52                 Max=abs(a[r][col]),row=r;
 53
 54         if(row==-1){--k;continue;}
 55
 56         for(int c=col;c<=n;++c)
 57             swap(a[k][c],a[row][c]);
 58
 59         for(int r=k+1;r<m;++r)
 60             if(a[r][col]){
 61
 62                 int lcm=Lcm(abs(a[r][col]),abs(a[k][col]));
 63                 int ta=lcm/a[r][col],tb=lcm/a[k][col];
 64                 if(a[r][col]*a[k][col]<0)tb=-tb;
 65
 66                 for(int c=col;c<=n;++c){
 67                     a[r][c]=a[r][c]*ta-a[k][c]*tb;
 68                     a[r][c]=(a[r][c]%7+7)%7;
 69                 }
 70
 71             }
 72     }
 73
 74     for(int r=k;r<m;++r)
 75         if(a[r][n])return -1;
 76     if(k<n)return 1;
 77
 78     for(int i=n-1;i>=0;--i){
 79
 80         int tmp=a[i][n];
 81         for(int j=n-1;j>i;--j)
 82             if(a[i][j]!=0)tmp-=a[i][j]*ans[j];
 83
 84         tmp=(tmp%7+7)%7;
 85         ans[i]=tmp*FPM(a[i][i],5)%7;
 86         if(ans[i]<3)ans[i]+=7;
 87     }
 88     return 0;
 89 }
 90 void solve(){
 91     mem(ans);
 92     int F=Gauss();
 93     if(F==-1)puts("Inconsistent data.");
 94     else if(F==1)puts("Multiple solutions.");
 95     else{
 96         for(int i=0;i<n;++i)
 97             printf("%d%c",ans[i],i==n-1?‘\n‘:‘ ‘);
 98     }
 99 }
100 int main(){
101     #ifndef ONLINE_JUDGE
102         freopen("a.in","r",stdin);freopen("a.out","w",stdout);
103     #endif
104     while(~scanf("%d%d",&n,&m)&&(n&&m)){
105         init();
106         solve();
107     }
108     return 0;
109 }
时间: 2024-10-16 23:07:59

poj 2947 Widget Factory(高斯消元)的相关文章

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to

poj 2947 Widget Factory (高斯消元,解模线性方程)

链接:poj 2947 题意:生产一些零件,已知零件种数,记录条数 记录只记录了某次生产从周几开始,周几结束,以及生产了哪些产品. 每件商品生产所需天数为3-9天. 求每样产品需要多少天才能完成. 若无解输出Inconsistent data. 有无穷解输出Multiple solutions. 有唯一解,输出其解 分析:根据题目所给信息,可以列出同余方程组,再根据高斯消元求解, 但还得判断是无解,无穷解,还是唯一解 1.系数矩阵的秩若与增广矩阵的秩不相等,则无解,否则有解 2.若有解,若增广矩

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

题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同种类,也可以不同种类),问每种零件生产一个出来需要的时间? 解题思路: 给出的时间段是从周几到周几,并没有给出具体的时间段,因此在计算过程中要进行取模,还有就是对每个零件要在题目要求的范围内进行枚举. ps:如果求出来的增广矩阵是n*n的,但是某个零件在[3,9]之间没有合理的解,也是无解的. 1

uva 1564 - Widget Factory(高斯消元+逆元)

题目链接:uva 1564 - Widget Factory 题目大意:n种零件,m次工作日程,零件序号从1到n,给出m次工作日程的信息,x,s,e,表示生产了x个零件,从星期s开始到星期e(有可能是多个星期),然后给出生产的x个零件的序号.求每个零件被生产需要多少天(保证在3到10天) 解题思路:因为不能确定每个工作日程具体生产了几天,所以对应列出的方程均为线性模方程(模7),所以在高斯消元的过程中遇到除法要转换成乘上逆元. #include <cstdio> #include <cs

POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是还保留着一些对工人制造工具的记录,对于每个老员工,他的记录包括,他开始工作的时间(在某个星期的星期几),被炒鱿鱼的时间(某个星期的星期几),在第几个星期不知道.....在这段时间里,他正好加工了k件物品,给出了这k件物品的编号.我们要做的就是通过这些记录,来确定每种工具的加工时间是多少. 分析: 对

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

http://poj.org/problem?id=2947 题目大意:有n 种装饰物,m 个已知条件,每个已知条件的描述如下:p start enda1,a2......ap (1<=ai<=n)第一行表示从星期start 到星期end 一共生产了p 件装饰物(工作的天数为end-start+1+7*x,加7*x 是因为它可能生产很多周),第二行表示这p 件装饰物的种类(可能出现相同的种类,即ai=aj).规定每件装饰物至少生产3 天,最多生产9 天.问每种装饰物需要生产的天数.如果没有解,

poj 1830 开关问题 高斯消元

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)

根据题意可构造出方程组,方程组的每个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki; 高斯消元构造上三角矩阵,以最后一个一行为例: C*x9 = sum + 4*k,exgcd求出符合范围的x9,其他方程在代入已知的变量后格式亦如此. 第一发Gauss,蛮激动的. #include <algorithm> #include <iostream> #include <cstring> #include <cstd

poj 2947 Widget Factory(模7环上的高斯消元)

http://poj.org/problem?id=2947 大致题意: 有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天.问每种装饰物需要生产的天数.如果没有解,则输出&q