poj 2947 Widget Factory(高斯消元)
Posted 小小AI,请多多提携
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
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
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 }
以上是关于poj 2947 Widget Factory(高斯消元)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 2947-Widget Factory(高斯消元解同余方程式)