https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences

求数组中被k整除的子段和有几个。这个要利用sum[i..j] = sum[j] - sum[i-1],注意这样还要保留一个sum[-1]=0(根据下标的起点怎么定)。那么能被k整除的子段sum[i..j]的sum[j]和sum[i-1] %k的值是一样的。

#include <iostream>
#include <vector>
using namespace std; int main() {
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
int sum = 0;
vector<int> cnt(k);
cnt[0] = 1;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
sum += tmp;
sum %= k;
cnt[sum]++;
}
long long count = 0;
for (int i = 0; i < k; i++) {
count += ((long long)cnt[i] * (cnt[i] - 1) / 2);
}
cout << count << endl;
}
return 0;
}

  

*[hackerrank]Consecutive Subsequences的更多相关文章

  1. [LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  2. [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  3. Split Array into Consecutive Subsequences

    659. Split Array into Consecutive Subsequences You are given an integer array sorted in ascending or ...

  4. leetcode659. Split Array into Consecutive Subsequences

    leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...

  5. LeetCode Split Array into Consecutive Subsequences

    原题链接在这里:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目: You ...

  6. 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  7. leetcode 659. Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  8. [LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  9. [LC] 659. Split Array into Consecutive Subsequences

    Given an array nums sorted in ascending order, return true if and only if you can split it into 1 or ...

随机推荐

  1. WPF 绑定二(绑定指定的字符串)

    xaml: <Window x:Class="WpfApplication1.Window2" xmlns="http://schemas.microsoft.co ...

  2. HMTL5的 video 在IOS7中碰到的坑

    直接说问题吧, 测试设备,ipod 我们在移动端播放视频的时候,一般使用H5的video标签,OK,这里有几点差异(就我目前所发现的)给大家分享一下, 1.在IOS7中,video元素是需要确定大小的 ...

  3. Android Studio笔记(2)——快捷键

    在朋友推荐下,上个星期黄老师我用上了Google的新黑暗工具,基于Intellij idea的新Android开发集成开发环境 ——Android Studio,用下来感觉还算不错,但作为一个ADT ...

  4. 在Visual Studio 2010 中创建类库(dll)

    创建类库 选择"文件"->新建->项目->Visual C# ->类库,输入名称,选择位置,单击确定 浏览解决方案资源管理器,可以看到两个C#类,第一个是A ...

  5. [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等

    [转]宏的高级使用--##,__VA_ARGS__, __FILE__, __FUNCTION__等 http://blog.csdn.net/yiya1989/article/details/784 ...

  6. DB天气app冲刺二阶段第十一天(完结)

    今天最后一天冲刺了,明天就不再冲刺了..已经把所有的技术的问题还有设计的问题都弄好了吧应该说 至少目前来说是的.因为有的实现不了的或者需要耗费时间的已经果断舍弃了,然后需要完善的也都基本完善了. 现在 ...

  7. Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

    [Err] 1442 - Can't update table 'test_trigger' in stored function/trigger because it is already used ...

  8. java mail实现Email的发送,完整代码

    java mail实现Email的发送,完整代码 1.对应用程序配置邮件会话 首先, 导入jar <dependencies> <dependency> <groupId ...

  9. hadoop 数据采样

    http://www.cnblogs.com/xuxm2007/archive/2012/03/04/2379143.html 原文地址如上: 关于Hadoop中的采样器 .为什么要使用采样器 在这个 ...

  10. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...