Kattis之旅——Divisible Subsequences

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 = 1000010;
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, 0, sizeof(Hash));
        Hash[0] = 1;
        ll sum = 0;
        for(int i=0; i<n; i++) {
            scanf("%lld", &num);
            sum += num;
            sum %= m;
            Hash[sum]++;
        }
        ans = 0;
        for(int i=0; i<m; i++) {
            if( Hash[i]>0 )
                ans += Hash[i]*(Hash[i]-1)/2;
        }
        printf("%lld\n", ans);
    }
}

int main(){
    input();
    return 0;
}
复制代码

 

AC代码:

复制代码
 //Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000010;
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, 0, sizeof(Hash));
        Hash[0] = 1;
        a[0] = 0;
        for(int i=1; i<=n; i++) {
            scanf("%lld", &num);
            a[i] = a[i-1]+num;
            a[i] %= m;
            Hash[a[i]]++;
        }
        ans = 0;
        for(int i=0; i<m; i++) {
            if( Hash[i]>0 )
                ans += Hash[i]*(Hash[i]-1)/2;
        }
        printf("%lld\n", ans);
    }
}

int main(){
    input();
    return 0;
}
复制代码

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

posted @   Asimple  阅读(426)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示