TOJ 3929 A3

A3

时间限制(普通/Java):2000MS/6000MS     运行内存限制:65536KByte
 

描述

The island of the Azeroth is so big that people must use the gryphon (one kind of animal like lion and eagle) to go over the island. One day, a paladin has finished a long trip in the Azeroth. He has made a matrix of A. If the city i and city j have a directly gryhon flying road, Ai,j=1, otherwise Ai,j=0.

Then the paladin is good at mathematics, so he immediately calculated A·A(A2), A·A·A(A3),…,A·A·…·A(An). (C=A·B iff  Ci,j=∑Ai,k·Bk,j).


Another day, one priest asked the paladin to give the number of triangles (The citys i, j, k form a triangle if Ai,j=Aj,k=Ak,i=1) which all the gryphon flying roads form. But the paladin only sends a letter of only a matrix of A3.


Can you help the priest to calculate the number of triangles?

输入

It has multiple test cases in this problem. In each case, first is the number of city n (1<=n<=1000). And then the matrix of A3.

The input is terminated by n=0.

输出

You must print the number of triangles. One case per line.

样例输入

3
2 3 3
3 2 3
3 3 2
0

样例输出

1

1个0 1 的矩阵 A

a[i][j] = 1 表示i 到 j可达 或者说 i 到 j 有1条路

例如 A:

0 1 1

1 0 1

1 1 0

而A2 = A* A

a[i][j] 的含义是

从 i 到j走 i 步的路的总数

A的i次方 代表 A[i,j] i到j走i步的路的总数

A^3矩阵里 A[i,i] 就是表示走三步 能回来的路径种类

以上是学长给我普及的知识 膜拜

#include <stdio.h>
int main()
{
	int n,i,j,count,a;
	while(scanf("%d",&n),n)
	{
		count = 0;
		for(i = 1;i <= n; i++)
		{
			for(j = 1; j <= n; j++)
			{
				scanf("%d",&a);
				if(i == j)
					count += a; 
			}
		}
		printf("%d\n",count/6);
	}
		
	return 0;
}



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值