Topic link: https: //vjudge.net/problem/POJ-2947
Meaning of the questions: After the conversion meaning of the questions is known of m congruence equations, find n variables.
Ideas:
It is worth learning to use this template lcm elimination of that piece. Note that the answer to the title output between [3,9].
AC Code:
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> using namespace std; const int maxn=305; int n,m,a[maxn][maxn],x[maxn]; char s1[10],s2[10]; int gcd(int a,int b){ return b?gcd(b,a%b):a; } int lcm(int a,int b){ return a/gcd(a,b)*b; //In addition to the first post-multiplication } // Gaussian elimination method for solving equations (GAUSS-Jordan elimination). ( @ -1 indicates no solution, 0 represents a unique solution, greater than 0 indicates infinite solutions, and returns the number of arguments free) // there equ equations, var an argument. Augmented number of rows of the matrix of equ, respectively, 0 to equ-1, the number of columns of var + 1, respectively 0 to var. Int GAUSS ( int equ, int var ) { int K, max_r, COL = 0 , TA, TB, the LCM, TEMP; for ( int I = 0 ; I < var ; ++ I) { X [I] = 0 ; } for (K = 0 ; K <COL && EQU < var ; K ++, ++ COL) { max_r =k; // find the largest absolute value coefficient k-th row and the row exchange for ( int I = k + . 1 ; I <EQU; ++ I) { IF (ABS (A [I] [COL])> ABS (A [max_r] [COL])) max_r = I; } IF ! (max_r = K) { for ( int I = COL; I < var + . 1 ; ++ I) the swap (A [max_r] [I], A [ K] [I]); } IF (! A [K] [COL]) { - K; Continue ; } for (int i=k+1;i<equ;++i){ if(!a[i][col]) continue; LCM=lcm(abs(a[i][col]),abs(a[k][col])); ta=LCM/abs(a[i][col]); tb=LCM/abs(a[k][col]); if(a[i][col]*a[k][col]<0) tb=-tb; //异号的情况是相加 for(int j=col;j<var+1;++j){ a[i][j]=((a[i][j]*ta-a[k][j]*tb)%7+7)%7; } } } // no solution conditions for ( int I = K; I <EQU; ++ I) { IF (A [I] [COL]) return - . 1 ; } // infinite solutions case where IF (K < var ) { return var -k; // returns the number of arguments consisting } // the only solution, augmented matrix formed strictly upper triangular matrix for ( int I = var - . 1 ; I> = 0 ; - - I) { TEMP = A [I] [ var ]; for(int j=i+1;j<var;++j){ if(!a[i][j]) continue; temp-=a[i][j]*x[j]; temp=(temp%7+7)%7; } while(temp%a[i][i]!=0) temp+=7; x[i]=(temp/a[i][i])%7; } return 0; } int tran(char *s){ if(strcmp(s,"MON")==0) return 1; else if(strcmp(s,"TUE")==0) return 2; else if(strcmp(s,"WED")==0) return 3; else if(strcmp(s,"THU")==0) return 4; else if(strcmp(s,"FRI")==0) return 5; else if(strcmp(s,"SAT")==0) return 6; else return 7; } int main(){ while(scanf("%d%d",&n,&m),n||m){ memset(a,0,sizeof(a)); for(int i=0;i<m;++i){ int k; scanf("%d%s%s",&k,s1,s2); a[i][n]=((tran(s2)-tran(s1)+1)%7+7)%7; while(k--){ int t; scanf("%d",&t); --t; ++a[i][t]; a[i][t]%=7; } } int ans=Gauss(m,n); if(ans==0){ for(int i=0;i<n;++i) if(x[i]<=2) x[i]+=7; for(int i=0;i<n-1;++i) printf("%d ",x[i]); printf("%d\n",x[n-1]); } else if(ans==-1){ printf("Inconsistent data.\n"); } else{ printf("Multiple solutions.\n"); } } return 0; }