题解 | #K序列#

K序列

https://ac.nowcoder.com/acm/problem/15613

思路

注意看数据范围,nk1e7nk\le1e7就说明了O(n2)O(n^2)暴力可过,我们只需要记录前缀和,然后区间长度从大到小试区间和是否可以整除k即可。

代码

#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;

//int read(){	int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}

int n,k,x,sum[N],ans;

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>x;
		sum[i]=sum[i-1]+x;
	}
	for(int len=n;len>=1;len--){
		for(int l=1;l+len-1<=n;l++){
			int r=l+len-1;
			if((sum[r]-sum[l-1])%k==0){
				cout<<len<<"\n";
				return 0;
			}
		}
	}
	return 0;
}
全部评论

相关推荐

点赞 评论 收藏
转发
2 1 评论
分享
牛客网
牛客企业服务