您的位置:首页 > 其它

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

2014-07-14 22:04 337 查看

CounttheNumberofCycles

TimeLimit:1SecMemoryLimit:
128MB

Description

Ininformationtheory,alow-densityparity-check(LDPC)codeisalinearerrorcorrectingcode,amethodoftransmittingamessageoveranoisytransmissionchannel,andisconstructed
usingasparsebipartitegraph.LDPCcodesarecapacity-approachingcodes,whichmeansthatpracticalconstructionsexistthatallowthenoisethresholdtobesetveryclose(orevenarbitrarilycloseontheBEC)tothetheoreticalmaximum(theShannonlimit)
forasymmetricmemory-lesschannel.
LDPCcodesaredefinedbyasparseparity-checkmatrix.Thisparity-checkmatrixisoftenrandomlygeneratedandtheelementsinitare0or1.IfwewantuseLDPCcodes,weshould
maketheparity-checkmatrixhavenocycles.Whenfourverticesoftherectangleinthematrixare1,wesaythatthematrixhasonecycle.Nowwewanttoknowhowmanycyclesareinthematrix.
Foragivenmatrix,youaretocountthenumberofcyclesinthematrix.

Input

Thereareseveraltestcases,eachtestcasestartswithalinecontainingtwopositiveintegersMandN.MandNisthesizeofthematrix(1<=M<=100,1<=N<=100).Nextfollowa
matrixwhichcontainsonlynumber0and1.Theinputwillfinishwiththeendoffile.

Output

.Foreachthecase,yourprogramwilloutputthenumberofcyclesinthegivenmatrixonseparateline.

SampleInput

13
111
23
101
011
33
101
011
111

SampleOutput

0
0
2


此题计算的是正矩形的数量,不包含斜着的矩形的数量,采用的方法为向量的点乘积。

对于每一行的N个元素可以看做是一个向量,一共有M个向量。

对于每两个向量,如果两个向量的点乘积为sum,则这两行可以构成C(sum,2)个矩形,把所有的相加即可。


代码如下:



#include<iostream>
#include<stdio.h>
usingnamespacestd;
constintmaxn=100+5;
inta[maxn][maxn];
intmain()
{
intm,n,i,j,k,s;
while(scanf("%d%d",&m,&n)!=EOF)
{
intcot=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++)
{
intsum=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);
}
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: