Candy Distribution
Time limit : 2sec / Memory limit : 1024MB
文章目录
Problem Statement 题目描述
There are N boxes arranged in a row from left to right. The i-th box from the left contains Ai candies.
You will take out the candies from some consecutive boxes and distribute them evenly to M children.
Such being the case, find the number of the pairs (l,r) that satisfy the following:
l and r are both integers and satisfy 1≤l≤r≤N.
Al+Al+1+…+Ar is a multiple of M.
Constraints
All values in input are integers.
1≤N≤10^5
2≤M≤10^9
1≤Ai≤10^9
文章目录
Format
Input
Input is given from Standard Input in the following format:
N M
A1 A2 … AN
Output
Print the number of the pairs (l,r) that satisfy the conditions.
Note that the number may not fit into a 32-bit integer type.
Sample
Sample Input
3 2
4 1 5
Sample Output
3
Explain
The sum Al+Al+1+…+Ar for each pair (l,r) is as follows:
Sum for (1,1): 4
Sum for (1,2): 5
Sum for (1,3): 10
Sum for (2,2): 1
Sum for (2,3): 6
Sum for (3,3): 5
Among these, three are multiples of 2.
文章目录
CODE 参考代码
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
map<long long,long long>ma;
long long n,m,s,sum=0,ans=0,i;
int main()
{
scanf("%lld%lld",&n,&m);
ma.clear();
ma[0]++;
for(i=1;i<=n;i++)
{
scanf("%lld",&s);
sum+=s;
sum%=m;
ans+=ma[sum];
ma[sum]++;
}
printf("%lld",ans);
return 0;
}
代码简单易懂,点赞收藏关注后离开,谢谢!🌹