XXXXX codeforces1364A(数学)

时间:2022-07-28
本文章向大家介绍XXXXX codeforces1364A(数学),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题意描述

Ehab loves number theory, but for some reason he hates the number x. Given an array a, find the length of its longest subarray such that the sum of its elements isn’t divisible by x, or determine that such subarray doesn’t exist.

An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

给定n长度的数组,求出满足和不为x倍数的最大子数组的长度。

思路

我们可以知道,如果一个数不能被x整除,而另一个数可以被x整除,那么两个数的和一定不能被整除。所以我们记录一下第一个出现的不能被x整除的和的下标,然后再找最后一个可以被整除的和的下标即可。

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int n,x;
void solve(){
    cin>>n>>x;
    int sum=0,ans=-1,pos=-1;
    for(int i=0;i<n;i++){
        int z;cin>>z;
        sum+=z;
        if(sum%x!=0){
            ans=max(ans,i+1);
            if(pos==-1) pos=i;
        }else{
            if(pos==-1) continue;
            else{
                ans=max(ans,i-pos);
            }
        }
    }
    cout<<ans<<endl;
}
int main(){
    IOS;
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}