博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Kattis之旅——Divisible Subsequences
阅读量:5368 次
发布时间:2019-06-15

本文共 2516 字,大约阅读时间需要 8 分钟。

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
27 31 2 34 82 1 2 1 1 2 1 2
06

错到心态爆炸,谁能告诉我WA的代码错在哪。。

WA代码:

//Asimple#include 
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
0 ) ans += Hash[i]*(Hash[i]-1)/2; } printf("%lld\n", ans); }}int main(){ input(); return 0;}

 

AC代码:

//Asimple#include 
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
0 ) ans += Hash[i]*(Hash[i]-1)/2; } printf("%lld\n", ans); }}int main(){ input(); return 0;}

谢谢大佬的AC代码:

转载于:https://www.cnblogs.com/Asimple/p/6769778.html

你可能感兴趣的文章
day 3 修改haproxy.cfg 作业
查看>>
sim usim Uim 区别
查看>>
网页中插入透明Flash的方法和技巧
查看>>
动态内存申请函数选择(realloc、malloc 、alloca、 calloc)
查看>>
获取元素属性get_attribute
查看>>
Python/jquery
查看>>
【BZOJ】【2132】圈地计划
查看>>
Java有没有goto?
查看>>
求不相邻金币相加和的最大值--动态规划1
查看>>
[转][osg]探索未知种族之osg类生物【目录】
查看>>
四十九. Zabbix报警机制 、 Zabbix进阶操作 、 监控案例
查看>>
元类中__new__ 与 __init__的区别--day27
查看>>
占小狼的简书博客
查看>>
struts2__action执行顺序
查看>>
php异常处理
查看>>
[xampp] /usr/bin/env: php: No such file or directory
查看>>
细学PHP 10 贴吧-2
查看>>
黑客攻防入门秘籍
查看>>
Swift迎来了1.0 GM 版(2014.09.09)
查看>>
【iOS开发-68】APP下载案例:利用tableView自带的cell布局+缓存池cell复用时注意button状态的检查...
查看>>