Light oj 1134 - Be Efficient(dp)

1134 - Be Efficient
Time Limit: 2 second(s)Memory Limit: 32 MB

You are given an array with N integers, and another integer M. You have to find the number of consecutive subsequences which are divisible by M.

For example, let N = 4, the array contains {2, 1, 4, 3} and M = 4.

The consecutive subsequences are {2}, {2 1}, {2 1 4}, {2 1 4 3}, {1}, {1 4}, {1 4 3}, {4}, {4 3} and {3}. Of these 10 'consecutive subsequences', only two of them adds up to a figure that is a multiple of 4 - {1 4 3} and {4}.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case contains two integers N (1 ≤ N ≤ 105) and M (1 ≤ M ≤ 105). The next line contains N space separated integers forming the array. Each of these integers will lie in the range [1, 105].

Output

For each case, print the case number and the total number of consecutive subsequences that are divisible by M.

Sample Input

Output for Sample Input

2

4 4

2 1 4 3

6 3

1 2 3 4 5 6

Case 1: 2

Case 2: 11

Note

Dataset is huge. Use faster i/o methods.


PROBLEM SETTER: MOHIUL ALAM PRINCE
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 500005

int a[N];

int n,m;
int vis[N];

int main()
{
    int i,j,t,ca=0;
    scanf("%d",&t);

    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);

       ll ans=0;
       memset(vis,0,sizeof(vis));
       for(i=1;i<=n;i++)
          a[i]=(a[i-1]+a[i])%m;

       for(i=1;i<=n;i++)
       {
           ans+=vis[a[i]];
           vis[a[i]]++;
           if(a[i]==0) ans++;
       }
       printf("Case %d: %lld\n",++ca,ans);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值