B - Boxes

题目连接:

http://agc010.contest.atcoder.jp/tasks/agc010_b

Description

There are N boxes arranged in a circle. The i-th box contains Ai stones.

Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:

Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.

Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.

Input

1≦N≦105

1≦Ai≦109

The input is given from Standard Input in the following format:

N

A1 A2 … AN

Output

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

Sample Input

5

4 5 1 2 3

Sample Output

YES

Hint

题意

给你n个数,你可以操作任意次,每次操作你可以选择一个数,使得离这个数距离为i的数-i。

问你最后是否能够使得所有数都变成0.

题解:

每次操作会减去1+2+....+n=n(n+1)/2,所以至少sum%(n(n+1)/2)==0

满足这个条件之后,我们发现是一个等差数列递减的,那么我们差分一下,就变成全部减一了。但是!最后一个数和第一个数之间的差值,却增加了(-(n-1)),这个推一下就知道了。

那么差分后的d[i]=a[i+1]-a[i],必须满足d[i] − (k − x) + (n − 1)x = 0,其中k为总共操作次数,x为在这个位置的操作次数,化简后得到k-d[i]=nx,那么我们只需要check(k-d[i]%n==0)就好了。

智商题……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n;
long long a[maxn],b[maxn],sum;
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld",&a[i]),sum+=a[i];
long long t=1ll*n*(n+1)/2;
if(sum%t){
cout<<"NO"<<endl;
return 0;
}
long long cnt = sum/t;
for(int i=0;i<n;i++){
b[i]=(a[(i+1)%n]-a[i]-cnt);
}
bool flag = true;
for(int i=0;i<n;i++){
if(b[i]>0||(-b[i])%n)
flag = false;
}
puts(flag?"YES":"NO");
}

Atcoder Grand Contest 010 B - Boxes 差分的更多相关文章

  1. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  2. Atcoder Grand Contest 010 C - Cleaning 树贪心(伪)

    C - Cleaning 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_c Description There is a tree with ...

  3. AtCoder Grand Contest 010 C:Cleaning

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_c 题目翻译 给你一棵树,每个点有个权值,每次操作可以选择两个度数为\(1\)的结点,然后让这 ...

  4. AtCoder Grand Contest 010 F - Tree Game

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...

  5. AtCoder Grand Contest 010题解

    传送门 \(A\) 判一下奇数的个数就行了 const int N=1e5+5; int a[N],n,res; int main(){ scanf("%d",&n); f ...

  6. AtCoder Grand Contest 010 D - Decrementing

    题目描述 有n个整数,其中第i个数为Ai.这些数字的gcd为1.两人轮流操作,每次操作把一个大于1的数减1,并把所有数除以所有数的最大公约数,最后无法操作者输,求是否先手必胜. 如果当前的sum为偶数 ...

  7. AtCoder Grand Contest 006

    AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...

  8. Atcoder Grand Contest 036 D - Negative Cycle

    Atcoder Grand Contest 036 D - Negative Cycle 解题思路 在某些情况下,给一张图加或删一些边要使图合法的题目要考虑到最短路的差分约束系统.这一题看似和最短路没 ...

  9. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

随机推荐

  1. vue-cli构建项目使用 less

    在vue-cli中构建的项目是可以使用less的,但是查看package.json可以发现,并没有less相关的插件,所以我们需要自行安装. 第一步:安装 npm install less less- ...

  2. CSS规范 - 典型错误--(来自网易)

    不符合NEC规范的选择器用法 .class{} 不要以一个没有类别的样式作为主选择器,这样的选择器只能作为后代选择器使用,比如.m-xxx .class{}.        .m-xxx div{} ...

  3. [转载]HTML5 真的会消灭原生App吗?

    http://mp.weixin.qq.com/s?__biz=MzA5NjQ4MzkyMw==&mid=201728945&idx=1&sn=8f43b5bb10695efc ...

  4. 第12月第14天 sfml cmake

    1. cd Desktop/mycode/ ls mkdir sfml03 cd sfml03 ls vi main.cpp vi config.h vi CMakeLists.txt ls pwd ...

  5. python_ssh连接

    首先下载paramikopip install paramiko查看并启动ssh服务service ssh status 添加用户:useradd -d /home/zet zetpasswd zet ...

  6. bulk_write&Replace_one

    ns=[]ns.append(ReplaceOne({'ip': ok['ip']}, ok, upsert=True))#更新插入 if len(ns) > 0: res = coll.bul ...

  7. oracle日期、转换函数

    转换函数 日期类型转换成字符类型 select to_char(sysdate) s1, --14-3月 -16        to_char(sysdate, 'yyyy-mm-dd') s2, - ...

  8. [HNOI2007]梦幻岛宝珠 「套路:分层 $DP$」

    显然直接 \(01\) 背包会超时并且超空间 套路:分层 \(DP\) 「考虑将每个子结构看作一层(也就是包含了不止 \(1\) 个物品的信息),并且大层不会对小层造成影响,可以考虑先进行每一层的自我 ...

  9. 秘钥登录服务器执行shell脚本

    做自动化的时候,有时候避免不了要和服务器有互动,刚巧碰上一个项目,需要执行命令才能完成本次测试. 昨天遇到的是秘钥形式的,只有秘钥和用户名,百度找了许久也没有思路,(能账号密码登录服务器的还简单些), ...

  10. 局域网搭建https局域网

    局域网搭建https局域网 1.使用tomcat作为服务器搭建局域网访问https 需要使用java jdk\bin下的keytool.exe来创建证书 使用命令:keytool -genkenpai ...