编程介的小学生 2016-12-26 03:30 采纳率: 20.5%
浏览 1166
已采纳

Matrix Multiplication 矩阵乘法

Description

Many studies have been done on developing efficient algorithms to calculate matrix multiplication. But it remains as a hard topic today. In this problem you are to calculate the 2006th power of a square Boolean matrix where addition and multiplication are defined as follows:

A B A + B
0 0 0
0 1 1
1 0 1
1 1 1
Truth Table of Addition

A B AB
0 0 0
0 1 0
1 0 0
1 1 1
Truth Table of Multiplication

Let A be a square matrix. The zeroth power of A is the identity matrix. The n-th (n > 0) power of A is the product of A and its (n − 1)-th power.

Input

The input contains multiple test cases. Each test cases consists of some lines:

Line 1: Contains two integers K (K < 1000) and M (0 ≤ M ≤ 10K), indicating the dimension of the matrix is K × K and K + M elements of the matrix are 1’s.
Lines 2 ~ M + 1: Each contains two integers i and j (0 ≤ i, j < K, i ≠ j), indicating the element in the (i + 1)-th row and (j + 1)-th column is 1.
All elements on the primary diagonal of the matrix are 1’s.

Output

For each test case output one line containing the number of elements that are 1’s in the 2006th power of the given matrix.

Sample Input

3 4
1 2
2 1
0 1
0 2
Sample Output

7

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-12-29 15:31
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的