AtCoder Beginner Contest 105 D - Candy Distribution
题意: 求有多少个[l,r]是k的倍数
思路: , 有
因此,把所有前缀和取膜,只有膜数相同的前缀和才是k的倍数,注意别忘记%k==0的情况
#include<cstdio>
#include<vector>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5;
const int MOD=1e9+7;
const double EPS=1e-12;
template <class T>
bool sf(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
int sign(double x){
return fabs(x) < EPS ? 0 : x>0 ? 1 : -1 ;
}
ll a[N];
ll sum[N];
map<ll,ll> mp;
int main(void){
ll n,m;
sf(n);sf(m);
for(int i=1;i<=n;i++) sf(a[i]);
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i],mp[sum[i]%m]++;
ll ans=0;
for(auto t : mp){
ans+=t.S*(t.S-1)/2;
// cout <<t.F<<" "<<t.S<<endl;
}
printf("%lld\n",ans+mp[0]);
return 0;
}