Atcoder--Candy Distribution II--前缀和+map

简介: 题目描述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:

题目描述


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≤105

·2≤M≤109

·1≤Ai≤109


输入


Input is given from Standard Input in the following format:
N M
A1 A2 … AN


输出


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.


样例输入


3 2
4 1 5


样例输出


3


提示


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.


题意:


求能够整除m的区间的个数

开始用了一遍 前缀和 ,再用两个for循环遍历,然后T了

再看模数的取值范围,开不了数组,于是就用map来解决


#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
#define start int wuyt()
#define end return 0
#define N 1005
///int num[maxn];
///int sum[maxn];
map<ll,ll>mp;
int n;
ll m;
start{
    n=read,m=read;
    ll num,sum = 0,ans = 0;
        mp.clear();
        mp[0]++;
        for(int i=1; i<=n;i++) {
            num=read;
            sum+=num;
            sum%=m;
            ans+=mp[sum];
            mp[sum]++;
        }
        printf("%lld\n",ans);
    end;
}


目录
相关文章
|
5月前
Leetcode 377. Combination Sum IV
赤裸裸的完全背包,属于动态规划的范畴,大家有兴趣可以在网上搜索下其他资料。个人觉得动态规划还是比较难理解的,更难给别人讲清楚,所以这里我直接附上我的代码供大家参考。
25 0
|
5月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
18 0
|
7月前
|
机器学习/深度学习
CF1552A Subsequence Permutation(string排序大法)
CF1552A Subsequence Permutation(string排序大法)
25 0
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 377. Combination Sum IV
给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数。
72 0
LeetCode 377. Combination Sum IV
|
人工智能 JavaScript BI
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
AtCoder Beginner Contest 222 D - Between Two Arrays(前缀和优化dp)
76 0
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
92 0