Given a sequence of positive integers, count all contiguous subsequences (sometimes called substrings, in contrast to subsequences, which may leave out elements) the sum of which is divisible by a given number. These subsequences may overlap. For example, the sequence (see sample input)
2, 1, 2, 1, 1, 2, 1, 2

contains six contiguous subsequences the sum of which is divisible
by four: the first to eighth number, the second to fourth number, the
second to seventh number, the third to fifth number, the fourth to sixth
number, and the fifth to seventh number.

Input

The
first line of the input consists of an integer c (1 <= c <= 200),
the number of test cases. Then follow two lines per test case.

Each test case starts with a line consisting of two integers d (1
<= d <= 1 000 000) and n (1 <= n <= 50 000), the divisor of
the sum of the subsequences and the length of the sequence,
respectively. The second line of a test case contains the elements of
the sequence, which are integers between 1 and 1 000 000 000,
inclusively.

Output

For
each test case, print a single line consisting of a single integer, the
number of contiguous subsequences the sum of which is divisible by d.
Sample Input 1  Sample Output 1
2
7 3
1 2 3
4 8
2 1 2 1 1 2 1 2
0
6

错到心态爆炸,谁能告诉我WA的代码错在哪。。

WA代码:

//Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, s, res, ans, len, T, k, num;int Hash[maxn];
void input() {
scanf("%lld", &T);
while( T -- ) {
scanf("%lld%lld", &m, &n);
memset(Hash, , sizeof(Hash));
Hash[] = ;
ll sum = ;
for(int i=; i<n; i++) {
scanf("%lld", &num);
sum += num;
sum %= m;
Hash[sum]++;
}
ans = ;
for(int i=; i<m; i++) {
if( Hash[i]> )
ans += Hash[i]*(Hash[i]-)/;
}
printf("%lld\n", ans);
}
} int main(){
input();
return ;
}

AC代码:

 //Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, s, res, ans, len, T, k, num;
ll Hash[maxn];
int a[maxn];void input() {
scanf("%lld", &T);
while( T -- ) {
scanf("%lld%lld", &m, &n);
memset(Hash, , sizeof(Hash));
Hash[] = ;
a[] = ;
for(int i=; i<=n; i++) {
scanf("%lld", &num);
a[i] = a[i-]+num;
a[i] %= m;
Hash[a[i]]++;
}
ans = ;
for(int i=; i<m; i++) {
if( Hash[i]> )
ans += Hash[i]*(Hash[i]-)/;
}
printf("%lld\n", ans);
}
} int main(){
input();
return ;
}

谢谢大佬的AC代码:http://blog.csdn.net/luckyxiaoqiang/article/details/7900284

Kattis之旅——Divisible Subsequences的更多相关文章

  1. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

  2. Kattis之旅——Chinese Remainder

    Input The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Th ...

  3. Kattis之旅——Fractional Lotion

    Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...

  4. Kattis之旅——Factovisors

    The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...

  5. Kattis之旅——Rational Arithmetic

    Input The first line of input contains one integer, giving the number of operations to perform. Then ...

  6. Kattis之旅——Number Sets

    You start with a sequence of consecutive integers. You want to group them into sets. You are given t ...

  7. Kattis之旅——Prime Path

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  8. Kattis之旅——Inverse Factorial

    题目意思就是已知n的阶乘,求n. 当输入的阶乘小于10位数的时候,我们可以用long long将字符串转化成数字,直接计算. 而当输入的阶乘很大的时候,我们就可以利用位数去大概的估计n. //Asim ...

  9. Kattis之旅——Perfect Pth Powers

    We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, ...

随机推荐

  1. Linux学习和ROS安装(1)

    参考文档:https://www.cnblogs.com/liu-fa/p/5779206.html#undefined 系统环境:Window7 64bit+VMware11 ubuntu-gnom ...

  2. Learn nodejs: Tutorials for Programmers of All Levels, 程序员每个阶段的示例

    https://stackify.com/learn-nodejs-tutorials/ What is Node.js? Node.js can be defined as a dynamic, c ...

  3. js 图片区域可点击,适配移动端,图片大小随意改变

    实现图片区域可点击,实际上使用map是可以的,但是适配效果并不好,图片只能是固定大小的值,而且点都被写死了. 在这里,我使用的js基于canvas写的一个小工具.可以圈出你需要点击的部分,然后生成一串 ...

  4. MSSqlServer 发布/订阅配置(主从同步)

    背景: 1.单个独立数据库的吞吐量是有瓶颈的,那么如何解决这个瓶颈? 2.服务器直接数据如何复制.并具备一致性.可扩展性? 资源: 官方资源:https://technet.microsoft.com ...

  5. JavaScript 基础数组循环和迭代的几种方法

    JavaScript 数组循环和迭代   (之前一直没怎么注意数组循环,今天做一道题时,用到forEach循环发现它并没有按照我想象的样子执行,总结一下数组循环) 一.第一种方法就是for()循环   ...

  6. pyqt常用窗口组件

    扩展知识: 熟悉常用的窗口组件: 1 按钮类 QPushButton   普通按钮 QToolButton   工具按钮:通常在工具栏使用 QRadioButton   单选框 QCheckBox   ...

  7. 记录一则RMAN恢复到历史备份(多个incarnation)

    环境: OEL 5.7 + Oracle 11.2.0.4 1.直接restore到想要恢复的时间点报错: RMAN> sql "alter session set nls_date_ ...

  8. 让bat以管理员权限运行

    有的电脑是非管理员登录,运行程序时,需要提示是否运行运行.解决方法如下: @ echo off % % ver|find "5.">nul&&goto :Ad ...

  9. H5 dom元素保存为图片

    一.使用插件html2canvas:https://github.com/niklasvh/html2canvas 具体代码: 1.html <div class="test" ...

  10. SqlServer表和EXCEL数据互相复制方法

    一.SqlServer表数据复制到excel 1.新建查询,用sql语句把表数据读出来 2.然后,选择数据,右键,复制(也可以点击连同标题复制),复制到记事本中(不然会乱码) 3.然后再把记事本的内容 ...