题目描述
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个盒子围成一个圈,每个盒子里有一定数目的石头,每次顺时针拿 1—n个,问经多次操作后,拿完最后N个之后是否所有盒子里石头数目为0。
解析: 通过题意很明显的发现,每次拿的石头的数目都是1—n的,那么拿完一圈会拿掉n*(n+1)/2个石头,如果给定的石头总数不是
n*(n+1)/2的整数倍肯定是不行的。而每次拿的数目是1—n,所以,相邻两个的差值为 1,或者 1-n,拿到最后取1的次数会是取 1-n 的 n-1 倍;那么就需要求出相应的次数就好。
ac代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
ll min3(ll a,ll b,ll c){return min(min(a,b),c);}
ll max3(ll a,ll b,ll c){return max(max(a,b),c);}
ll gcd(ll x, ll y){if(y==0)return x;return gcd(y, x%y);}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}
ll n, a[200005];
int main()
{
ll i, sum=0, flag=1, x=0, y=0;
cin>>n;
for(i=0;i<n;i++){
cin>>a[i];
sum+=a[i];
}
ll tmp=n*(n+1)/2;
if(sum%tmp){
cout<<"NO"<<endl;
return 0;
}
ll cot=sum/tmp, d, dd;
for(i=0;i<n;i++){
if(i)
d=a[i]-a[i-1];
else
d=a[i]-a[n-1];
d+=cot*(n-1);
if(d%n!=0){
cout<<"NO"<<endl;
return 0;
}
dd=d/n;
if(dd<0 || dd>cot){
cout<<"NO"<<endl;
return 0;
}
x+=dd;
y+=(cot-dd);
}
if(x==y*(n-1))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}