Light oj 1134 - Be Efficient

來源:互聯網
上載者:User

題目連結:http://lightoj.com/volume_showproblem.php?problem=1134

1134 - Be Efficient

    PDF (English) Statistics Forum
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 100009using 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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.