标题: k倍区间
给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍区间。
你能求出数列中总共有多少个K倍区间吗?
输入
-----
第一行包含两个整数N和K。(1 <= N, K <= 100000)
以下N行每行包含一个整数Ai。(1 <= Ai <= 100000)
输出
-----
输出一个整数,代表K倍区间的数目。
例如,
输入:
5 2
1
2
3
4
5
程序应该输出:
6
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 2000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include <xxx>
不能通过工程设置而省略常用头文件。
提交程序时,注意选择所期望的语言类型和编译器类型。
1 #include<iostream> 2 using namespace std; 3 int IJSUM(int a[],int i,int j)//求数组a中从下标i到下标j得和 4 { 5 int sum=0; 6 for(int m=i;m<=j;m++) 7 { 8 sum+=a[m]; 9 } 10 return sum; 11 } 12 int main() 13 { 14 int n,k; 15 cin>>n; 16 cin>>k; 17 int A[n]; 18 int i; 19 for(i=0;i<n;i++) 20 { 21 cin>>A[i]; 22 23 } 24 int sum,num=0; 25 for(i=0;i<n;i++) 26 { 27 for(int j=i;j<n;j++) 28 { 29 sum=IJSUM(A,i,j); 30 if (sum%k==0)num++; 31 } 32 } 33 cout<<num<<endl; 34 return 0; 35 }