COJ 1068: Count the Number of Cycles(向量点积)

 Count the Number of Cycles

 Time Limit:1 Sec  Memory Limit: 128 MB

Description

In information theory, a low-density parity-check (LDPC) code is a linear error correcting code, a method of transmitting a message over a noisy transmission channel, and is constructed using a sparse bipartite graph. LDPC codes are capacity-approaching codes, which means that practical constructions exist that allow the noise threshold to be set very close (or even arbitrarily close on the BEC) to the theoretical maximum (the Shannon limit) for a symmetric memory-less channel.
LDPC codes are defined by a sparse parity-check matrix. This parity-check matrix is often randomly generated and the elements in it are 0 or 1. If we want use LDPC codes, we should make the parity-check matrix have no cycles. When four vertices of the rectangle in the matrix are 1, we say that the matrix has one cycle. Now we want to know how many cycles are in the matrix.
For a given matrix, you are to count the number of cycles in the matrix.

Input

There are several test cases, each test case starts with a line containing two positive integers M and N. M and N is the size of the matrix (1<=M<=100, 1<=N<=100). Next follow a matrix which contains only number 0 and 1. The input will finish with the end of file.

Output

.   For each the case, your program will output the number of cycles in the given matrix on separate line.

Sample Input

1 3
1 1 1
2 3
1 0 1
0 1 1
3 3
1 0 1
0 1 1
1 1 1

Sample Output

0
0
2
 
此题计算的是正矩形的数量,不包含斜着的矩形的数量,采用的方法为向量的点乘积。
对于每一行的N个元素可以看做是一个向量,一共有M个向量。
对于每两个向量,如果两个向量的点乘积为sum,则这两行可以构成C(sum,2)个矩形,把所有的相加即可。
 
代码如下:
 
 
#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn=100+5;
int a[maxn][maxn];
int main()
{
 int m,n,i,j,k,s;
 while(scanf("%d%d",&m,&n)!=EOF)
 {
  int cot=0;
  for(i=0;i<m;i++)
   for(j=0;j<n;j++)
   {
    scanf("%d",&a[i][j]);
   }
  s=m-1;
  for(i=0;i<s;i++)
  {
   int sum=0;
   for(j=i+1;j<m;j++)
   {
    for(k=0;k<n;k++)
    {sum+=a[i][k]*a[j][k];}
    cot+=sum*(sum-1)/2;
    sum=0;
   }
  }
  printf("%d\n",cot);
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值