Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用矩阵写的,WA了,请教?

Posted by a024014 at 2007-03-21 19:07:46 on Problem 3150
#include <stdio.h>
int n,m,c[512],a[512][512],result[512][512];
/*
处理矩阵A和B的乘机.并将结果存入A中。
*/
void fun(int a[][512],int b[][512])
{
	int w[512][512],i,j,k;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
		{
			w[i][j]=0;
			for(k=0;k<n;k++)
			{
				w[i][j]+=a[i][k]*b[k][j];
				if(w[i][j]>=m) w[i][j]%=m;
			}
		}
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			a[i][j]=w[i][j];
}
int main()
{
	int d,k,i,len,j,pos,digit,s,count;
	while(scanf("%d%d%d%d",&n,&m,&d,&k)!=EOF)
	{
		for(i=0;i<n;i++)
			scanf("%d",&c[i]);
		len=(d<<1)+1;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;a[i][j]=0,result[i][j]=0,j++);
			result[i][i]=1;
		}
         /*生成矩阵*/
		for(i=0;i<n;i++)
			for(j=(i+n-d)%n,count=0;count<len;count++)
			{
				a[j][i]=1;
				j++;
				if(j>=n) j%=n;
			}
         /*求出k的二进制位数,例如,2的二进制数为10,长度为2,这里得出3,比长度多1*/
		pos=digit=1;
		while(k>=digit)
		{
			digit<<=1;
			pos++;
		}

		digit=1;
         /*测试k的每个位,如果为1,则乘以矩阵A,然后矩阵A再做平方*/
		for(i=1;i<pos;i++)
		{
			if(k&digit)
				fun(result,a);
			digit<<=1;
			fun(a,a);
		}
		for(i=0;i<n;i++)
		{
			for(s=j=0;j<n;j++)
			{
				s+=c[j]*result[j][i];
				if(s>=m) s%=m;
			}
			if(i<n-1)
				printf("%d ",s);
			else printf("%d\n",s);
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator