Bzoj 2107:spoj2832 Find The determinant III method of dividing

Source: Internet
Author: User

2107:spoj2832 Find The determinant III time limit:1 Sec Memory limit:259 MB
submit:154 solved:46
[Submit] [Status] [Discuss] Descriptionproblem Code:deter3 Given A NxN matrix A, find the determinant of a% P. Give an integer matrix A (NxN) with a size of n≤200, ask for | The value of the a|%p (that is, the determinant of a is divided by the remainder of P). The number in the square and P are 32-bit signed types can accommodate integers inputthe first line of every test case contains two integers, representing n (0 < n < 201) A nd p (0 < P < 1,000,000,001). The following n lines each contain n integers, the j-th number in i-th line represents A[i][j] (-1,000,000,001 < a[i][ J] < 1,000,000,001). Outputfor each test case, print a single line contains the answer. Sample Input3 4
-840419217-895520213-303215897
537496093 181887787-957451145
-305184545 584351123-257712188


Sample Output

2

In fact, the name of the algorithm will probably do it. The Division of Gaussian elimination is essentially equivalent to the method of getting along with each other, and there is no precision error. In order to eliminate one of the two lines, the line is reduced to a similar sub-problem by dividing the rows into large rows.

The first attempt to use Java's bigdecimal, the results found that the accuracy and time can not be met simultaneously.

Matrix determinant can be geometrically understood, the vector (substrate) can be added to each other, without affecting the volume, but the substrate interchange, the direction of the volume inversion.

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineMAXN 210typedefLong LongQword;qword MAT[MAXN][MAXN];intMain () {//freopen ("Input.txt", "R", stdin);        intn,m,x,y,z; intMoD;  while(~SCANF ("%d%d",&n,&MoD)) {                 for(intI=1; i<=n;i++)                {                         for(intj=1; j<=n;j++) {scanf ("%lld",&Mat[i][j]); MAT[I][J]%=MoD; }                }                intrev=1;  for(intI=1; i<=n;i++) {x=-1;  for(intj=i;j<=n;j++)                        {                                if(Mat[j][i]) {x=J;  Break; }                        }                        if(x==-1) Break; if(x!=i) { for(intj=1; j<=n;j++) Swap (mat[x][j],mat[i][j]); Rev=-Rev; }                        if(!mat[i][i]) Break;  for(intj=i+1; j<=n;j++)                        {                                 while(Mat[i][i]) {Qword T=mat[j][i]/Mat[i][i];  for(intk=1; k<=n;k++) Mat[j][k]= (mat[j][k]-mat[i][k]*t)%MoD;  for(intk=1; k<=n;k++) Swap (mat[j][k],mat[i][k]); Rev=-Rev; }                                 for(intk=1; k<=n;k++) Swap (mat[j][k],mat[i][k]); Rev=-Rev; }} qword ans=1;  for(intI=1; i<=n;i++) ans=ans*mat[i][i]%MoD; Ans= (ans*rev+mod)%MoD; printf ("%lld\n", ans); }}

Bzoj 2107:spoj2832 Find The determinant III method of dividing

Related Article

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.