light oj 1134
2016-09-10 20:14
246 查看
Description
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
2
4 4
2 1 4 3
6 3
1 2 3 4 5 6
Sample Output
Case 1: 2
Case 2: 11
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
2
4 4
2 1 4 3
6 3
1 2 3 4 5 6
Sample Output
Case 1: 2
Case 2: 11
#include<iostream> #include<cstdio> #include<iostream> #include<cstring> #include<vector> #include <algorithm> using namespace std; const int N = 100005; const int inf = 0x3f3f3f3f; typedef long long LL; int main() { int t, n, m, i, k=1; int a[N]; LL f[N]; scanf("%d",&t); while(t--) { scanf("%d %d", &n, &m); memset(f, 0, sizeof(f)); for(i = 0;i<n;i++) { scanf("%d", &a[i]); a[i]=a[i]%m; } LL ans = 0; LL sum = 0; f[0]=1; for(i=0;i<n;i++) { sum=(a[i]+sum)%m; ans+=f[sum]; f[sum]++; } printf("Case %d: %lld\n", k++, ans); } return 0; }
相关文章推荐
- Javascript中类的实现机制(四)
- HTTP请求报文和HTTP响应报文
- FTP数据入库Hive
- bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
- 了解和使用keystone(二)安装keystone
- 快速排序中的partition函数的枢纽元选择,代码细节,以及其标准实现
- 我的selenium日记3
- 层次分析法的matlab的实现
- hdu 5874 构造
- vs2015里给c#添加轮廓折叠功能
- hdu5869——Different GCD Subarray Query(思考+树状数组)
- Appium基于安卓的元素定位方法
- HDU2544-最短路
- python爬虫之scrapy框架(一)
- IOS 之 Quartz 2D 绘图(上)
- effective stl 第18条: 避免使用vector<bool>
- hdu5873 Football Games(数学)(Landau's Theorem )
- LeetCode 4 Median of Two Sorted Arrays
- Python标准库简介
- diy自己的linux