Candy Distribution 分糖果

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;
}

代码简单易懂,点赞收藏关注后离开,谢谢!🌹

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值