ZOJ1569
n m
a1 a2 a3 a4 a5 a6 a7 a8 ...
S1 = a1
S2 = a1 + a2
S3 = a1 + a2 + a3
S4 = a1 + a2 + a3 + a4
S5 = a1 + a2 + a3 + a4 + a5
任何一段连续数和可用Sj - Si (i < j)求得
若Sj % m == Si % m,可以知道a[i] + a[i+1] + ...+ a[j]%m == 0,
即此段和可以被m除尽 (对零做特殊处理)
#include <iostream>
using namespace std;
const int MAX_SIZE = 5005;
//这里有问题,当改为5000时,提交一直是Segmentation Fault
//而照题目所言 ,m (m <= 5000).汗!
int Count[MAX_SIZE];
int n,m;
void Init()
{
for(int i = 0; i < m; i++)
Count[i] = 0;//S[x] % m 为m,其计数为这样的部分和个数
}
int main()
{
int cnt,sum,tmp;;
while(cin>>n>>m)
{
cnt = 0;
sum = 0;
Init();
while(n--)
{
cin>>tmp;
sum =(sum + tmp)%m;
if(!sum)cnt++;//对零特殊处理
Count[sum]++;
}
for(int i = 0; i < m; i++)
cnt += Count[i]*(Count[i] - 1) / 2;
cout<<cnt<<endl;
}
return 0;
}
using namespace std;
const int MAX_SIZE = 5005;
//这里有问题,当改为5000时,提交一直是Segmentation Fault
//而照题目所言 ,m (m <= 5000).汗!
int Count[MAX_SIZE];
int n,m;
void Init()
{
for(int i = 0; i < m; i++)
Count[i] = 0;//S[x] % m 为m,其计数为这样的部分和个数
}
int main()
{
int cnt,sum,tmp;;
while(cin>>n>>m)
{
cnt = 0;
sum = 0;
Init();
while(n--)
{
cin>>tmp;
sum =(sum + tmp)%m;
if(!sum)cnt++;//对零特殊处理
Count[sum]++;
}
for(int i = 0; i < m; i++)
cnt += Count[i]*(Count[i] - 1) / 2;
cout<<cnt<<endl;
}
return 0;
}
Segmentation Fault :
- 1.buffer overflow --- usually caused by a pointer reference out of range.
- 2.stack overflow --- please keep in mind that the default stack size is 8192K.