# 2020杭电多校第三场 1004 Tokitsukaze and Multiple

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.

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

3
3

#include <bits/stdc++.h>

#define ll long long
using namespace std;
int a[100001];
int vis[100001];
int cnt = 0,ans;
int main()
{
ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);
int t;
cin>>t;
while(t--){
int n,p;
cin>>n>>p;
ans = 0;
for(int i = 1;i <= n;i++){
cin>>a[i];
a[i]=a[i]%p;
}
cnt++;
ll sum = 0;
//用cnt来区别是否是已经消去了的区间
//a...b...c...d
//假设ab区间内相加的和为p的倍数且里面的前缀和用vis记录且设为1
//但是之前消去了就会导致误认为没消去 如果cnt不加1的话
for(int i = 1;i <= n;i++){
sum += a[i];
sum %= p;
if(vis[sum] == cnt || sum == 0){
ans++;
sum = 0;
cnt++;
}
else
vis[sum] = cnt;
}
cout<<ans<<endl;

}
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
08-01 166
03-10 819
05-11 651
07-14 453

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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