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. [Shapefile C Library]读写shp图形(C++&.net Wapper)

    ShapeLib的.net Wapper版可以在官网下载到,在WorldWind中也有使用.ORG据说也是使用的ShapeLib实现的shp文件的读写. 官网:http://shapelib.mapt ...

  2. typedef 详解

    一行很常用的代码: typedef int XX_Size_Check[(sizeof(XX)==64)? 1:-1]; 很容易猜到上面这行代码是要在编译时检查XX的size,但再深究点,我们就会发现 ...

  3. echarts 实现tooltip双栏效果

    实现效果如下: 代码: //option tooltip: { trigger: 'axis', axisPointer: { label: { show: true, fontSize: 15 } ...

  4. [py]类的专有方法

    陆陆续续总结一些用到的类的特殊方法 看源码总会看到一些奇奇怪怪的写法: 掺杂着设计模式 https://coding.net/u/RuoYun/p/Python-design-pattern/git/ ...

  5. 118A

    #include <iostream> #include <cctype> #include <string> using namespace std; int m ...

  6. IOT专用IOP平台

    10.110.20.200(iot)  root  789a?     centos7  给展湾paul,iot-hub 10.110.20.199(iot)  root             ce ...

  7. [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  8. js图的数据结构处理----普里姆算法

    //求加权无向连通图的MST的贪心算法 //最小树,最小路径联通各个点 function PRIM(){ var graph = [ [], [undefined,Infinity, 23 ,Infi ...

  9. apache分割数组和集合的分法

    public static void main(String[] agrs){    String[] array={"1","2","", ...

  10. jmeter 发送加密请求 beanshell断言 线程组间传递参数

    原文地址https://www.cnblogs.com/wnfindbug/p/5817038.html 最近在做http加密接口,请求头的uid参数及body的请求json参数都经过加密再发送请求, ...