3823. Divisible Subsquences

本文介绍了一种算法,用于计算给定序列中子序列之和为特定数倍数的总数。通过余数分堆的方法,可以有效解决此类问题。

题目大意:给定一个数和一个序列  计算这个序列中有多少个子序列的和是这个数的倍数。

思路:a[i]+a[i+1]+..+a[j] = s[j]-s[i],如果要和是其倍数, 则表示s[j]-s[i]能够整除那个数,只要s[j]和s[i]对那个数的余数相同就可以了啊,所以,将各个和按余数分堆!! 每个堆选择两个出来,即(n*n-1)/2;

ps: 余数分堆很常用!!  序列为两个相减也很常用!!


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.

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 subse-quences the sum of which is divisible by d.

Sample Input

2
7 3
1 2 3
4 8
2 1 2 1 1 2 1 2

Sample Output

0
6

#include<iostream>
#include<cstring>
using namespace std;
const int num=1000010;
int sum[50010],divd[num];
int main()
{
	int c,d,n,a;
	cin>>c;
	while(c--)
	{
		cin>>d>>n;
		sum[0]=0;
		for(int i=0;i<n;i++)
		{
			cin>>a;
			sum[i+1]=(sum[i]+a)%d;
		}
		memset(divd,0,sizeof(divd));
		for(int i=0;i<=n;i++)
		{
			divd[sum[i]]++;
		}
		long long count=0;
		for(int i=0;i<d;i++)
		{
			if(divd[i]>=2)
				count=count+(long long)divd[i]*(divd[i]-1)/2;
		}
		cout<<count<<endl;
	}
	return 0;
}


内容概要:本文系统介绍了标准化和软件知识产权的基础知识,涵盖标准化的基本概念、分类、标准代号、国际标准的采用原则及程度,重点讲解了信息技术标准化、ISO与IEC等国际标准化组织以及ISO9000和ISO/IEC15504等重要标准体系;在知识产权部分,详细阐述了知识产权的定义、分类及特点,重点分析了计算机软件著作权的主体、客体、权利内容、行使方式、保护期限及侵权认定,同时涉及商业秘密的构成与侵权形式、专利权的类型与申请条件,以及企业如何综合运用著作权、专利、商标和商业秘密等方式保护软件知识产权。; 适合人群:从事软件开发、项目管理、IT标准化或知识产权相关工作的技术人员与管理人员,以及备考相关资格考试的学习者;具备一定信息技术背景,希望系统掌握标准化与软件知识产权基础知识的专业人员。; 使用场景及目标:①帮助理解各类标准的分类体系及国际标准采用方式,提升标准化实践能力;②指导企业在软件研发过程中有效保护知识产权,规避法律风险;③为软件著作权登记、专利申请、技术保密等提供理论依据和操作指引。; 阅读建议:建议结合国家相关政策法规和实际案例进行深入学习,重点关注软件著作权与专利权的适用边界、标准制定流程及企业知识产权管理策略,强化理论与实践的结合。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值