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
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;
}

你可能感兴趣的:(2020杭电多校)