AtCoder Beginner Contest 105 D - Candy Distribution [求有多少个区间和是k的倍数]

AtCoder Beginner Contest 105 D - Candy Distribution

题意: 求有多少个[l,r]是k的倍数

思路:$$ (sum[R]-sum[L-1]) \% k==0 $$  , 有 $$ (sum[R] \%k -sum[L-1] \%k ) ==k*t $$

因此,把所有前缀和取膜,只有膜数相同的前缀和才是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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值