Light oj 1134 - Be Efficient

本文介绍了一种算法,用于解决给定数组中寻找所有连续子序列中能被特定整数M整除的问题,并提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://lightoj.com/volume_showproblem.php?problem=1134

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)

题目大意:求是m的倍数并且连续的子序列的个数

解析:根据前缀和求


代码如下:


#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 100009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
int a[N], s[N];

int main()
{
    int t, cnt = 0;
    int i, num;
    cin >> t;
    while(t--)
    {
        memset(s, 0, sizeof(s));
        int n, m;
        scanf("%d%d", &n, &m); a[0] = 0;
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &num);
            a[i] = (a[i - 1] + num) % m;
            s[a[i]]++;
        }
        LL ans = 0;
        for(i = 0; i < m; i++)
            ans += (LL)s[i] * ((LL)s[i] - 1LL) / 2LL;

        printf("Case %d: %lld\n", ++cnt, ans + s[0]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值