B-Boxes
http://agc010.contest.atcoder.jp/tasks/agc010_b
Problem Statement
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.
Constraints
- 1≦N≦105
- 1≦Ai≦109
Input
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 1
5 4 5 1 2 3
Sample Output 1
YES
All the stones can be removed in one operation by selecting the second box.
Sample Input 2
5 6 9 12 10 8
Sample Output 2
YES
Sample Input 3
4 1 2 3 1
Sample Output 3
NO
大致意思是说,假设初始化N个数字为0,选取某一个位置,分别增加1到N(如果到达数组末尾就从头开始)。
比如:
0 0 0(选择第二个位置开始)
3 1 2(选择第三个位置开始)
5 4 3(选择第 X 个位置开始)
.......
给出N个数字,看看是不是由以上操作得到的,是的话输出YES,不是输出NO;
对于数据,记录其和,则K=(n*(n+1))/2为操作次数,另d[i]=a[i]-a[i-1];必定有一个的d[i]=n-1;剩下的为1;
且有d[i]-(k-x)+(n-1)*x=0或k-d[i]=nx(x为相对于的d[i]来说异常操作的数),所以,一定有k-d[i]>=0 && (k-d[i])%n==0,
也可以得到(k-d[i])/n的和为k,由此可解题。
#include<iostream> using namespace std; const int mod=1e5+5; long long a[mod],b[mod]; int main() { long long n,i,sum=0,count=0,k; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; b[i]=a[i]-a[i-1]; sum+=a[i]; } b[1]=a[1]-a[n]; if(sum%(n*(n+1)/2))//如果k不为整数,直接输出NO { cout<<"NO"<<endl; return 0; } k=sum/(n*(n+1)/2); for(i=1;i<=n;i++) { if((k-b[i]<0) || (k-b[i])%n) { cout<<"NO"<<endl; return 0; } else count+=(k-b[i])/n; } if(count!=k) cout<<"NO"<<endl; else cout<<"YES"<<endl; return 0; }