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

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 1009 1 2 3 4
 

Sample Output
  
  
1007
 

Source
 

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

#include <cstdio>
#include <cstring>
#define LL __int64
LL 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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值