2020杭电多校第三场 Tokitsukaze and Multiple

第四题:Tokitsukaze and Rescue

题目:

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.

思路

我们将每次前缀和模数出现的位置记录一下,并且记录我们最后可以使用的位置。(之前写的存栈的算法其实是n^2的算法orz)

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;

const int N=1e5+7;

ll sum[N];
int vis[N];

int main()
{
	//freopen("test.in","r",stdin);//设置 cin scanf 这些输入流都从 test.in中读取
    //freopen("test.out","w",stdout);//设置 cout printf 这些输出流都输出到 test.out里面去
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		int n,m;
		memset(vis,-1,sizeof vis);
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			ll x;
			cin>>x;
			sum[i]=sum[i-1]+x;
		}
		int end=0;
		vis[0]=0;
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			int t=sum[i]%m;
			if(vis[t]>=end)
			{
				ans++;
				end=i;
			}
			vis[t]=i;
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值