题目传送门: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

最新文章

  1. win7系统下,vs2010一调式,vs就关闭要重启
  2. Xcode里-ObjC, -all_load, -force_load
  3. 今天的工作发现了4年前的“bug一枚”
  4. 记录一次MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
  5. 配置 Apache+php多端口多站点(转载)
  6. iOS NSURLConnection和异步网络请求
  7. 那些年不错的Android开源项目
  8. LeetCode Intersection of Two Linked Lists
  9. CSS和JS实现单行、多行文本溢出显示省略号(该js方法有问题不对)
  10. 在centos上安装mysql5.7的三种方法
  11. USACO Section 3.2: Sweet Butter
  12. Spark SQL - DataFrame
  13. IE8 innerHTML赋值时包含多级HTML标签时的解决方案
  14. SpringMVC中对Controller使用AOP
  15. sed示例
  16. Markdown简单语法总结
  17. hibernate exception nested transactions not supported 解决方法
  18. Delphi中打开网页连接的几种方法
  19. 使用 IdentityServer4 实现 OAuth 2.0 与 OpenID Connect 服务
  20. Python模块hashlib

热门文章

  1. Spring事务管理5-----声明式事务管理(3)
  2. double,float,BigDecimal类型数值的操作
  3. 移动端BI的设计
  4. App唤起微信小程序和回调
  5. markdown居中对齐,左对齐,右对齐
  6. Java工程师学习指南第8部分:分布式系统理论与实践
  7. 从Odds:比值比推导出Logtic分类的算法
  8. 第35课.函数对象分析(&quot;()&quot;重载)
  9. 关于element中的父子组件的传值问题
  10. bitmap位图原理和实现