POJ 2947-widget Factory (Gaussian elimination solution with congruence equation)

Source: Internet
Author: User

Title Address: POJ 2947

Test instructions: N Kinds of items, m Records, and then write to M line, each line has k,start,end, the expression from the week start to the End of the week, do the K-items, the next number of K for the item numbers. This topic notes that the final result is to be adjusted to 3-9.

Ideas:

It is easy to think of Gaussian elimination. But it's Gaussian elimination with the same equation, and it's time to start building a relationship with MoD 7.
The last solution to solve such equations is to use the idea of extending GCD, for example, if the resulting matrix is:
1 1 4
0 6 4
Then 6 * X2% 7 = 4 7 is equivalent to 6 * X2 + 7 * Y = 4, using the extended gcd to find out X2 is 3, then the first equation is
X1 * 1 7 + 1*3% 7 = 4 7 is equivalent to X1 + 7 * Y = 1 get x1=1;

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs ( -1.0); const double Esp=1e-6;const int maxn=310;int aug[maxn][maxn];<span style= "Background-color:rgb (255, 255, 255) ;" The number of >//augmented matrices is m, 0 to M-1, and the number of columns is n+1, respectively, 0 to n.</span>int x[maxn];//the int free_num;int m,n;//m equations, n variable int gcd (int A,    int b) {int r;        while (b!=0) {r=b;        B=a%b;    A=r; } return A; int LCM (int a,int b) {return a/gcd (b) *b;}    /*void Debug (void) {puts ("");    int i,j;        for (i=0;i<m;i++) {for (j=0;j<n+1;j++) {cout << matrix[i][j] << "";    } cout << Endl; } cout << Endl;}    */int Trans (char *str) {if (strcmp (str, "MON") ==0) return 1; ElSe if (strcmp (str, "TUE") ==0) return 2;    else if (strcmp (str, "WED") ==0) return 3;    else if (strcmp (str, "THU") ==0) return 4;    else if (strcmp (str, "FRI") ==0) return 5;    else if (strcmp (str, "SAT") ==0) return 6; else if (strcmp (str, "SUN") ==0) return 7;} The Gaussian elimination method is the solution of the equation set (Gauss-jordan elimination).    (-1 means no solution,//0 represents the unique solution, greater than 0 represents the infinite solution, and returns the number of free arguments) int Gauss () {int i,j;    int row,col,max_r;//The row with the largest absolute value of the current column;    int LCM;    int TA,TB;    int tmp;        for (row=0,col=0; row<m&&col<n; row++,col++) {//enumerates the currently processed rows. The row that finds the largest absolute value of the col column element is exchanged with the first row line.        (To reduce the error when dividing) Max_r=row;        for (i=row+1; i<m; i++) {if (ABS (Aug[i][col]) >abs (Aug[max_r][col])) max_r=i; } if (Max_r!=row) {//exchange for (J=row; j<n+1; j + +) swap with the row row (aug[row][j],aug[        MAX_R][J]);            if (aug[row][col]==0) {//indicates that the COL column has the row row below all 0, the next column of the current row is processed.            row--;        Continue } for (i=row+1; i<m; i++) {//Enumerate the rows to be deleted.                if (aug[i][col]!=0) {LCM=LCM (ABS (Aug[i][col]), ABS (Aug[row][col]));                Ta=lcm/abs (Aug[i][col]);                Tb=lcm/abs (Aug[row][col]); if (aug[i][col]*aug[row][col]<0) tb=-tb;//The case is added for (j=col; j<n+1; J + +) {aug[i][                J]= (((AUG[I][J]*TA-AUG[ROW][J]*TB)%7+7)%7);    }}}}//debug (); 1.    The absence of solution: the presence of degenerate arrays (0, 0, ..., a) such rows (A! = 0).    for (I=row; i<m; i++) {if (aug[i][col]!=0) return-1; }//2.    The case of an infinite solution: the presence of an augmented array of n * (n + 1) (0, 0, ..., 0) Such a line, that is to say, does not form a strict upper triangular array.    and the number of rows that appear is the number of free arguments.    if (row<n) {return n-row; }//3.    The only solution: the formation of a strict upper triangular array in an augmented array of n * (n + 1). Calculate the Xn-1, Xn-2 ...    X0. for (i=n-1; i>=0; i--) {tmp=aug[i][n];//equation to the right of the number for (j=i+1; j<n; J + +) {if (aug[i][j]!=0) tmp-        =aug[i][j]*x[j];//the known solution into, minus, only left, an unknown solution tmp= (tmp%7+7)%7; } while (tmp%aug[i][i]!=0)//outer layer each cycle is to seek a[i][i], because it is the only unknown variable in each equation (when the equation) tmp+=7;//because the number of days is uncertain, and aug[i][i] must be an integer, the period is 7 x[i]= (    Tmp/aug[i][i])%7; } return 0;}    int main (void) {int nn,mm,i,j,k;    int num;    Char start[5],end[5];        while (~SCANF ("%d%d", &nn,&mm)} {if (nn==0&&mm==0) break;        n=m=0;        memset (Aug,0,sizeof ());            for (i=0; i<mm; i++) {scanf ("%d", &k);            scanf ("%s%s", start,end);            Aug[i][nn]= ((trans (End)-trans (Start) +1)%7+7)%7;                for (j=1; j<=k; J + +) {scanf ("%d", &num);                num--;                aug[i][num]++;        aug[i][num]%=7;//have duplicate}} m=mm;        N=nn;        Free_num = Gauss ();                    if (free_num==0) {for (i=0; i<n; i++) if (x[i]<3)//According to test instructions, the processing time of each part is 3-9 days.            x[i]+=7;            for (i=0; i<n-1; i++) printf ("%d", x[i]);   printf ("%d\n", X[i]);     } else if (Free_num==-1) puts ("inconsistent data.");    Else puts ("multiple solutions."); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2947-widget Factory (Gaussian elimination solution with congruence equation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.