poj 3844 Divisible Subsequences
来源:互联网 发布:2vb空间日志在线播放 编辑:程序博客网 时间:2024/05/16 07:03
Description
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.
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.
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
27 31 2 34 82 1 2 1 1 2 1 2
Sample Output
06
Source
2009 ACM North Western European Regional Contest
先求累加和sum[i](a[0]+a[1]+...+a[i]),然后求余,若两个累加和sum[i]%d==sum[j]%d,那么(sum[j]-sum[i])%d==0,即a[i+1], ... , a[j]就是一个满足条件的连续子序列。注意余数为0的情况,可以在原序列最前面加上一个假设的元素0,这就就可以统一处理了。
#include <stdio.h>#include <memory.h>#define MAX_LEN 50010#define MAX_NUMBER 1000010int sum[MAX_LEN];__int64 mod[MAX_NUMBER];int main(){int c;scanf("%d", &c);while(c--){int d, n;scanf("%d%d", &d, &n);int x;memset(mod, 0, sizeof(mod));sum[0] = 0;mod[0] = 1;for(int i = 1; i <= n; i++){scanf("%d", &x);sum[i] = sum[i-1] + x;sum[i] %= d;mod[sum[i]]++;}__int64 result = 0;for(int i = 0; i < d; i++)if(mod[i] > 0)result += mod[i] * (mod[i]-1) / 2;printf("%I64d\n", result);}return 0;}
- POJ 3844 Divisible Subsequences
- poj 3844 Divisible Subsequences
- POJ 3844Divisible Subsequences
- POJ 3844 Divisible Subsequences
- poj 3844 Divisible Subsequences 组合数学
- poj 3844 Divisible Subsequences 剩余类,组合计数
- poj 3844 Divisible Subsequences(数学+抽屉原理)
- poj 1745 divisible
- POJ 3092 Non-divisible 2-3 Power Sums 笔记
- 1280: Divisible
- Divisible [数学]
- toj3823 Divisible Subsquences
- 3823. Divisible Subsquences
- Largest Divisible Subset
- 【Leetcode】Largest Divisible Subset
- Largest Divisible Subset
- Largest Divisible Subset
- Largest Divisible Subset
- MySQL5.x处理中文全解
- php 得到上周,本周,上月,本月,本季度,上季度
- 深入浅出设计模式笔记之十八:状态模式
- Spring集成JMS
- Qt学习之路_6(Qt局域网聊天软件)
- poj 3844 Divisible Subsequences
- java单例模式
- 深入浅出设计模式笔记之十九:模板方法模式
- 如何让你的qq登录速度变快~~强烈推荐!!
- 更新、删除触发器使用
- nio/mina(五) 构架研究
- 1.Struts访问受保护的页面,动态ActionForward:界面
- iOS开发笔记--入门
- ARM指令优化-Coding for NEON - Part 5: Rearranging Vectors