【loj#6220】sum
2024-09-05 07:20:06
题目传送门:https://loj.ac/problem/6220
题意:对于一个序列$a$,找出它的一个子序列$b$,使$\sum_{a_i \in b}a_i \equiv 0 \pmod n$
这是一道很好的思维题。
全体子序列较难考虑,因此我们考虑子序列中的区间。设$sum_i=\sum_{i=1}^{n} a_i$,显然$\sum_{i=l}^{r} a_i \equiv 0 \pmod n$当且仅当$sum_{l-1}=sum_r$,而我们发现$sum_i \bmod n$只有$n$种取值,那么根据抽屉原理,必定存在$x,y \in [0,n],x \neq y$,使$sum_x=sum_y$,因此区间$[x+1,y]$就是我们的答案。
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
#define maxn 1000020
inline ll read()
{
ll x=; char c=getchar(),f=;
for(;c<''||''<c;c=getchar())if(c=='-')f=-;
for(;''<=c&&c<='';c=getchar())x=x*+c-'';
return x*f;
}
inline void write(ll x)
{
static int buf[],len; len=;
if(x<)x=-x,putchar('-');
for(;x;x/=)buf[len++]=x%;
if(!len)putchar('');
else while(len)putchar(buf[--len]+'');
}
inline void writeln(ll x){write(x); putchar('\n');}
inline void writesp(ll x){write(x); putchar(' ');}
ll a[maxn];
int pos[maxn];
int n;
int main()
{
n=read();
pos[]=;
int sum=;
for(int i=;i<=n;i++){
a[i]=read();
sum=(sum+a[i])%n;
if(pos[sum]){
for(int j=pos[sum];j<=i;j++)
writesp(j),writeln(a[j]);
return ;
}
else pos[sum]=i+;
}
return ;
}
loj6220
最新文章
- win7系统下,vs2010一调式,vs就关闭要重启
- Xcode里-ObjC, -all_load, -force_load
- 今天的工作发现了4年前的“bug一枚”
- 记录一次MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
- 配置 Apache+php多端口多站点(转载)
- iOS NSURLConnection和异步网络请求
- 那些年不错的Android开源项目
- LeetCode Intersection of Two Linked Lists
- CSS和JS实现单行、多行文本溢出显示省略号(该js方法有问题不对)
- 在centos上安装mysql5.7的三种方法
- USACO Section 3.2: Sweet Butter
- Spark SQL - DataFrame
- IE8 innerHTML赋值时包含多级HTML标签时的解决方案
- SpringMVC中对Controller使用AOP
- sed示例
- Markdown简单语法总结
- hibernate exception nested transactions not supported 解决方法
- Delphi中打开网页连接的几种方法
- 使用 IdentityServer4 实现 OAuth 2.0 与 OpenID Connect 服务
- Python模块hashlib