TOJ 3929 A3

来源:互联网 发布:php判断奇数偶数 编辑:程序博客网 时间:2024/05/17 02:14

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.

样例输入

32 3 33 2 33 3 20

样例输出

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;}



 

原创粉丝点击