題目連結: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;}