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. [LeetCode] 367. Valid Perfect Square_Easy tag:Math

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  2. js动态规划---背包问题

    //每种物品仅有一件,可以选择放或不放 //即f[i][w]表示前i件物品恰放入一个容量为w的背包可以获得的最大价值. //则其状态转移方程便是:f[i][w]=max{f[i-1][w],f[i-1 ...

  3. pip安装时遇到的问题集锦,持续更新!

    1.Python安装时出现Could not fetch URL https://pypi.python.org/simple/pool/: There was a problem confirmin ...

  4. 一个简单的sel server 函数的自定义

    创建自定义函数:use 数据库名gocreate function 函数名(@pno int)returns intasbegin  declare @a int   if not exists(se ...

  5. win10升级后,无法ping通vmware的centos解决方法

    win10是lenovo thinkpad460上面的,是正版的.无法做设置,不让其自动升级.10月8日节后第一天上班,电脑要求更新,我就点更新. 结果就发现无法ping通vmware中的centos ...

  6. cocos2dx 3.x(游戏中的储存-UserDefault)

      引擎提供的UserDefault可以简单地储存游戏中的信息,例如背景音乐音效的开关状态,用户名等等. UserDefault是一个单例对象,可以用getInstance方法来获得. 如果是第一次调 ...

  7. DataGrip设置长sql语句自动换行

    我这个DataGrip是英文版的. 在每个查询窗口(Console)的工具图标里,有个Settings.打开Settings弹出框,试图从Appearance和Editor里找到设置选项,却没有找到. ...

  8. unity3d 第一人称脚本解释MouseLook

    using UnityEngine; using System.Collections; /// MouseLook rotates the transform based on the mouse ...

  9. vss使用笔记

    一.四大代码/文档管理软件 (1)     git:具有PR(push request)特性,推送请求.需要负责人审核后才能推送.另外,在推送过程中,git会预编译(合并),分布式代码管理(客户端本地 ...

  10. [7] Windows内核情景分析---线程同步

    基于同步对象的等待.唤醒机制: 一个线程可以等待一个对象或多个对象而进入等待状态(也叫睡眠状态),另一个线程可以触发那个等待对象,唤醒在那个对象上等待的所有线程. 一个线程可以等待一个对象或多个对象, ...