集训队专题(8)1008 The Evaluation of Determinant

来源:互联网 发布:知乐油烟机 编辑:程序博客网 时间:2024/05/29 18:57

The Evaluation of Determinant

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 851    Accepted Submission(s): 215


Problem Description
The determinant is quite important in Linear Algebras, but I think that almost everyone who has ever learnt Linear Algebras is tired of the complicated and tedious calculations of determinant. Actually, it’s not the job we should do, isn’t it? As an outstanding Geek, why don’t we just ask computers to do these?


Give you a determinant D (it’s ensured that the result of it is an integer) and m, try to get the result of this determinant mod m, and m = p1 * p2 …… pn, all the pi are different. You can assume 1000 < pi < 10000, aij < 1000, and m can be fit in 32-bit signed integer.
 

Input
Input two integers n and m in the first line, n represents the scale of the determinant. (n <= 100)
Then comes an n * n matrix, the determinant’s component aij means the one in row i and column j.
 

Output
Output the result of the determinant D mod m.
 

Sample Input
2 10091 23 4
 

Sample Output
1007
 

Source
2009 Multi-University Training Contest 2 - Host by TJU
 

此题就是一个求行列式对一个数取模的问题,首先我们在输入的时候就可以对每个数进行取模,达到第一层优化,在求行列式需要用到线性代数的知识--高斯消元……这是小编我自己也比较弱的一项,水水的解出来就好了……

#include <cstdio>#include <cstring>#define LL __int64LL num[102][102],tmp,t;int n,m;LL mod(LL a,LL b){if(a<0) return a+b-(a/b)*b;else return a-(a/b)*b;}int main(){LL i,j,k,ans;while(scanf("%d%d",&n,&m)!=EOF){ans = 1;for(i=1; i<=n; i++){for(j=1; j<=n; j++){scanf("%I64d",&num[i][j]);num[i][j] = mod(num[i][j],m); }}for(i=1; i<=n; i++){for(j=i+1; j<=n; j++){while(num[j][i]){t = num[i][i]/num[j][i];for(k=i; k<=n; k++){num[i][k] = mod(num[i][k]-num[j][k]*t,m);tmp = num[i][k];num[i][k] = num[j][k];num[j][k] = tmp;}ans = -ans;}}if(num[i][i] == 0){ans = 0;break;}ans = mod(ans*num[i][i],m);}printf("%I64d\n",ans);}return 0;}


0 0
原创粉丝点击