题目描述
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
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
输入
The input is given from Standard Input in the following format:
N
A1 A2 … AN
N
A1 A2 … AN
输出
If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.
样例输入
5
4 5 1 2 3
样例输出
YES
提示
All the stones can be removed in one operation by selecting the second box.
题意:
有N个盒子,每个盒子有Ai块石头,每次操作可以选一个盒子i,然后对于每个j(1 <= j <= N),从第(i+j)个盒子移走j个石头(第N+k个盒子即为第k个盒子),成一个环状。如果存在一个盒子的石头数目不够操作将不能进行。问能否将所有的石头移走,能的话,输出YES,反之,输出NO.
思路:
成环状,sum=总石头数,首先不知道每次的起点,但每次循环,移走的石头数是从1到n,即ans+=i;可以用公式n*(n+1)/2;所以可以判断总的石头数sum%ans,若不为0,输出NO;轮数=sum/ans,看相邻两个石头差值,如果比轮数少或轮完后有剩余,输出NO.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005],b[100005];
int main(){
ll ans,sum;
ans = 0; sum = 0;
int n;
scanf("%d",&n);
int flag = 1;
for(int i = 0 ; i < n ; i ++){
scanf("%lld",&a[i]);
sum += a[i];
ans += i+1;
}
if(sum%ans)
flag = 0;
else{
ll y = sum/ans;
for(int i = 0; i < n; i ++)
b[i] = a[(i+1)%n]-a[i];
for(int i = 0; i < n; i ++){
if(b[i]-y>0||(b[i]-y)%n<0){
flag = 0;
break;
}
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}