POJ 2947 Widget Factory

Source: Internet
Author: User

Widget Factory

Test instructions: There are n pieces of decorations, there is m group information. (1 <= N, m<=) Each group of information has the beginning of the week and the end of the week (is within the MoD 7 range) and also includes the type of num decorations (1~n), of which each ornament takes 3 <= x[i] <= 9; The input of the kind can be repeated;

Ideas:

1. Based on the input to establish the augmented matrix a[][], but in the process of establishing and solving because it is the mod sense , so the number of inputs and the final time to use the MoD 7; (analysis shows that when the number is 7 of the same class, the beginning of the same week is the same as the end of the week)

2. In front of a few Gaussian elimination, I use Free_var to determine whether there is a free variable, which is the number of input equations and the solution of the number of elements equal to the case is established. In the case of sample 1 will find that the number of equations originally more than the variable 1, at this time calculated Free_var = 1, but it is not going to have a dimension of free variable. It depends on the number of useful equations, the relationship between Row and Var;

3. To get the upper triangular array to solve the variable x[i], we need to solve a modal linear equation,a[i][i]*x[i] + 7*y = RET (mod 7);ret for A[i][col] with known x[j] Removal of a[i][i] obtained;

At this point the call EXGCD () can be solved, and finally the solution should be within the 3~9 range;

PS: Time performance is not very good, unexpectedly used 1782ms ... The shortest is 297ms. Gap Ah!!!

#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<map>#include<queue>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))inta[305][305];intEquvar;intx[305];Const intMOD =7;voidDebug () {Puts ("********"); inti,j; Rep0 (i,0, Equ) {Rep1 (J,0,var) cout<<a[i][j]<<" "; cout<<Endl; }puts ("********");}int__GCD (intAintb) {    returnB?__GCD (b,a%b): A;}intLCM (intAintb) {    returnA/__GCD (A, b) *b;}voidEXGCD (intAintBint& D,int& X,int&y) {    if(!B) {d = a;x =1; y =0;} Else{EXGCD (b,a%b,d,y,x); Y-= x* (A/b); }}intGuass () {intI,j,k,free_var =0, Row,col;  for(row =0, col =0; row < equ && Col <var; row++,col++){        intMX =Row; Rep0 (J,row+1, equ)if(ABS (A[j][col]) > abs (A[mx][col])) MX =J; if(A[mx][col] = =0) {row--;//The number of free arguments cannot be recorded here, only the useless col can be recorded .            Continue; }        if(MX! =row) Rep1 (K,col,var) Swap (a[row][k],a[mx][k]); Rep0 (J,row+1, Equ) {            if(A[j][col]) {intLCM =LCM (ABS (A[row][col]), ABS (A[j][col)); intRation_row = Lcm/a[row][col],ration_j = lcm/A[j][col]; if(A[row][col]*a[j][col] <0) Ration_row =-ration_row;//The symbolic opposite becomes addition;REP1 (K,col,var) A[j][k]= ((a[j][k]*ration_j-a[row][k]*ration_row)%7+7)%7; }        }    }    //debug ();rep0 (i,row,equ)if(a[i][var] !=0)return-1;//No solution    if(Row <var)return var-row;//row represents a useful equation for the number of equations, but it is necessary to judge the premise of a solution to say that there are many groups of solutions;Rep_1 (I,var-1,0){//* * * If the only solution, in fact, is the Var matrix        intRET = a[i][var];  for(j = i+1; J <var; j + +)//using the obtained variable to eliminate the element behind Col in row rows, get Yi Yuanfang;RET-= x[j]*A[i][j]; RET= ((ret%7)+7)%7; intD,x1,y; //constructs a[i][i]*x[i] + 7*y = RET (mod 7), and gcd (a[row][col],7) = 1) because A[row][col]! = 0EXGCD (A[i][i],7, d,x1,y);//then take the RET to get 3~9 range;X[i] = ((ret*x1)%7+7)%7; if(X[i] <3) X[i] + =7; }    return 0;}Const Charstr[7][5] = {{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"},{"SUN"}};intDATE_ID (Char*c) {     for(inti =0; I <7; i++)        if(strcmp (str[i],c) = =0)returni;}intMain () {inti,j,n,m; Chars[5],t[5];  while(SCANF ("%d%d", &n,&m) = =2&& n +m)        {MS0 (a); Equ= m;var=N; intKind,num; Rep0 (i,0, M) {scanf ("%d%s%s",&num,s,t); a[i][var] = date_id (t)-date_id (s) +1; if(a[i][var] <0) a[i][var] +=7; Rep0 (J,0, num) {scanf ("%d",&kind); a[i][--kind]++; } rep1 (J,0,var) A[i][j]%=7; }        //debug ();        intRET =Guass (); if(ret = =-1) puts ("inconsistent data."); Else if(Ret >0) puts ("multiple solutions."); Else{rep0 (i,0,var) printf ("%d%c", X[i],i = =var-1?'\ n':' '); }    }    return 0;}
View Code

POJ 2947 Widget Factory

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.