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. Java中基本数据类型byte,short,char,int,long,float,double 取值范围

    部分内容转自:java 彻底理解 byte char short int float long double 首先说byte: 这段是摘自jdk中 Byte.java中的源代码: /** * A co ...

  2. ie6-ie8支持CSS3选择器的解决办法

    引入nwmatcher.js和selectivizr.js <!--[if lt IE 10]> <script src="html5shiv.js">&l ...

  3. [django]前后端分离之JWT用户认证

    在前后端分离开发时为什么需要用户认证呢?原因是由于HTTP协定是不储存状态的(stateless),这意味着当我们透过帐号密码验证一个使用者时,当下一个request请求时它就把刚刚的资料忘了.于是我 ...

  4. keras后端设置【转载】

    转自:https://keras.io/backend/ At this time, Keras has three backend implementations available: the Te ...

  5. [转]sqlserver2014两台不同服务器上数据库同步

    https://www.cnblogs.com/peng0731/p/7359465.html 同步了快一个月了,因为途中比较麻烦,第一次,遇到烦的地方就停下了,今天终于同步成功了,哈哈,下面我就来介 ...

  6. jupyter 快捷键

    Jupyter Notebook 的快捷键 Jupyter Notebook 有两种键盘输入模式.编辑模式,允许你往单元中键入代码或文本:这时的单元框线是绿色的.命令模式,键盘输入运行程序命令:这时的 ...

  7. leetcode 22括号生成

    非常好的一道题.一开始的思想是这样的,先把n对括号按照某一顺序生成一个string,然后用全排列算法生成所有可能,然后利用stack写一段判断括号是否匹配的字符串,匹配的假如结果中.不过会超时.因为全 ...

  8. leetcode 300最长上升子序列

    用递归DFS遍历所有组合肯定积分会超时,原因是有很多重复的操作,可以想象每次回溯后肯定会有重复操作.所以改用动态规划.建立一个vector<int>memo,初始化为1,memo[i]表示 ...

  9. python开源数据库gadfly安装排除错误

    从sourceforge.net上下载的,结果需要仔细看网页才能找到下载地址.下载zip包,它没有把下载地址放在显眼的地方. 安照提示,python setup.py结果提示出错.看帮助文件,需要运行 ...

  10. myeclipse修改了安装目录名字打不开解决方法

    在MyEclipse XX目录下有一个MyEclipse.ini的文件,里面既有相对路径,又有绝对路径,修改绝对路径指向新的位置即可 来源:http://www.iteye.com/problems/ ...