NC51080. Widget Factory
描述
输入描述
The input contains several blocks of test cases. Each case begins with a line containing two integers: the number of the different types, and the number 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 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 .
输出描述
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).
示例1
输入:
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
输出:
8 3 Inconsistent data.
C++ 解法, 执行用时: 330ms, 内存消耗: 760K, 提交时间: 2022-01-13 21:42:46
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> using namespace std; typedef int ll; const int N=310; const int mod=7; int n,m,a[N][N]; map<string,int> mp; ll ksm(ll a,ll b) { ll res=1; while(b) { if(b&1) res=res*a%mod; a=a*a%mod; b>>=1; } return res; } int gauss() { int r,c; for(r=1,c=1;c<=n;++c) { int t=r; for(int i=r+1;i<=m;++i) if(abs(a[i][c])>abs(a[t][c])) {t=i;} if(a[t][c]%7==0) continue; if(t!=r) for(int i=c;i<=n+1;++i) swap(a[r][i],a[t][i]); for(int i=r+1;i<=m;++i) if(a[i][c]) for(int j=n+1;j>=c;--j) a[i][j]=((a[i][j]*a[r][c]-a[r][j]*a[i][c])%mod+mod)%mod; r++; } for(int i=r;i<=m;++i) // 比较m个方程式的系数确定秩 if((a[i][n+1]%7)!=0) return 0; if(r<=n) return 1; for(int i=n;i>0;--i) { for(int j=n;j>i;--j) a[i][n+1]=((a[i][n+1]-a[j][n+1]*a[i][j])%mod+mod)%mod; a[i][n+1]=(a[i][n+1]*ksm((a[i][i]+7)%7,mod-2))%mod;//a[i][i]没有消为1,需要除以a[i][i] } return 2; } int main() { mp["MON"]=1;mp["TUE"]=2;mp["WED"]=3;mp["THU"]=4; mp["FRI"]=5;mp["SAT"]=6;mp["SUN"]=7; while(scanf("%d%d",&n,&m)&&(n+m)) { memset(a,0,sizeof a); for(int i=1;i<=m;++i) { int x,y; char sa[4],sb[4]; scanf("%d%s%s",&x,sa,sb); a[i][n+1]=((mp[sb]-mp[sa]+1+7))%mod; while(x--) { scanf("%d",&y); a[i][y]=(a[i][y]+1)%mod; } } int tmp=gauss(); if(tmp==0) puts("Inconsistent data."); else if(tmp==1) puts("Multiple solutions."); else { for(int i=1;i<=n;++i) printf("%d ",a[i][n+1]+(a[i][n+1]<=2?7:0)); puts(""); } } return 0; }