2020杭电多校3 Tokitsukaze and Multiple

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6794
Problem Description
Tokitsukaze has a sequence of length n, denoted by a.

Tokitsukaze can merge two consecutive elements of a as many times as she wants. After each operation, a new element that equals to the sum of the two old elements will replace them, and thus the length of a will be reduced by 1.

Tokitsukaze wants to know the maximum possible number of elements that are multiples of p she can get after doing some operations (or doing nothing) on the sequence a.

Input
There are several test cases.

The first line contains an integer T (1≤T≤20), denoting the number of test cases. Then follow all the test cases.

For each test case, the first line contains two integers n and p (1≤n,p≤105), denoting the length of the sequence and the special number, respectively.

The second line contains n integers, where the i-th integer ai (1≤ai≤105) is the i-th element of a.

It is guaranteed that the sum of n in all test cases is no larger than 106.

Output
For each test case, output in one line the maximum possible number of elements that are multiples of p after doing some operations.

Sample Input

2
5 3
2 1 3 2 1
3 1
123 456 789

Sample Output

3
3

翻译:
给定两个数n和p,接下来是长度为n的序列。每次可以把序列相邻的两个数合并为两个数的和,数组的长度减1。求数组中是p的倍数的数最多有多少个。

代码:

#include<cstdio>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int main()
{
	int t,n,p;
	int a[N];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&p);
		for(int i=1; i<=n; i++)
			scanf("%d",&a[i]);
		int pos=1;
		int sum=0,res=0;
		for(int i=1; i<=n; i++)
		{
			sum+=a[i];
			if(sum%p==0)
			{
				sum=0;
				res++;
				pos=i+1;
			}
			else///匹配不成功
			{
				int ta=sum;///不在原来的sum和pos上进行操作,因为可能不匹配 
				int lo=pos;
				while(i>lo)
				{
					ta-=a[lo++];
					if(ta%p==0)
					{
						res++;
						sum=0;
						pos=i+1;
						break;
					}
				}
			}
		}
		printf("%d\n",res);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值