2020 Multi-University Training Contest 3 1004 Tokitsukaze and Multiple

题面

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

思路

维护一个前缀和,然后在set里面统计0的个数,贪心思想。

代码实现

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
#define per(i,n,a) for (int i=n;i>=a;i--)
#define MT(x,i) memset(x,i,sizeof(x) )
#define rev(i,start,end) for (int i=0;i<end;i++)
#define inf 0x3f3f3f3f
#define mp(x,y) make_pair(x,y)
#define lowbit(x) (x&-x)
#define MOD 1000000007
#define exp 1e-8
#define N 1000005 
#define fi first 
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int ,int> PII;
ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
const int maxn=100100;
int n,p;
int t;
int  a[maxn];
int suf[maxn];
set <int > s;

int main () {
   cin>>t;
   while (t--) {
       cin>>n>>p;
       rep (i,1,n) {
           cin>>a[i];
           a[i]=a[i]%n;
       }
       suf[n+1]=0;
       s.clear ();
       int ans=0;
       per (i,n,1) suf[i]=(suf[i+1]+a[i])%p;
       s.insert (suf[1]);
       rep (i,2,n+1) {
           if (s.count (suf[i])) {
               ans++;
               s.clear ();
           }
           s.insert (suf[i]);
       }
       cout<<ans<<endl;
   }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hhlya/p/13394600.html
今日推荐