题解 | #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;
}
全部评论

相关推荐

06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
Ncsbbss:又想干活又想要工资,怎么什么好事都让你占了
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务