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. [django]session设置与获取原理

    admin登录 情况1: 登录后会产生一个sessionid 情况2: 自定义设置了key后,会多一个sessionid, 登录后会替换为登录后的sessionid的key值 if username ...

  2. (转)MySQL排序原理与案例分析

    前言      排序是数据库中的一个基本功能,MySQL也不例外.用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order by语句,Group by语句,Distinct ...

  3. Keras和tf关系【转载】

    转自:https://blog.csdn.net/capecape/article/details/78390246 TensorFlow和theano以及Keras都是深度学习框架,TensorFl ...

  4. confd动态生成配置文件

    下载安装confd $ mkdir -p $GOPATH/src/github.com/kelseyhightower $ git clone https://github.com/kelseyhig ...

  5. elasticsearch6.0.0源码导入到idea总结

    由于elasticsearch使用了gradle管理依赖,进行构建,再加上对gradle没有接触过. 因此,导入到idea中遇到问题非常多.这里大致罗列下并说明解决方法. 环境背景 gradle版本: ...

  6. win7 x64安装TensorFlow

    在windows下安装的TensorFlow做学习研究之用,如果要进行技术,请看相关博文:CentOS7安装TensorFlow 1.安装Pytho3.5 首先到Anaconda网站去下载Window ...

  7. 使用 nghttpx 搭建 HTTP/2 代理 (转)

    来自http://www.fanyue.info/2015/08/nghttpx-http2.html 使用 nghttpx 搭建 HTTP/2 代理 [转] HTTP/1.1,定义于 1999 年, ...

  8. 数据加密之DES加密

    DES加密即使用DESCryptoServiceProvider加密.DESCryptoServiceProvider在命名空间下:System.Security.Cryptography; 对称加密 ...

  9. 32.js 判断当前页面是否被浏览

    可以通过document.hidden属性判断当前页面是否是激活状态. 兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+ 兼容性写法示例: va ...

  10. oracle 常用(二)

    多表查询: 1.等值连接查询: select a.aa,a.bb,b.qq from XX  a ,   CC  b   where a.aa= b.ee 2.不等值连接: select  *  fr ...