题目链接
B - Boxes
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
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
Copy
5 4 5 1 2 3
Sample Output 1
Copy
YES
All the stones can be removed in one operation by selecting the second box.
Sample Input 2
Copy
5 6 9 12 10 8
Sample Output 2
Copy
YES
Sample Input 3
Copy
4 1 2 3 1
Sample Output 3
Copy
NO
Source code
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #include<vector>
- #include<queue>
- #include<stack>
- using namespace std;
- #define rep(i,a,n) for (int i=a;i<n;i++)
- #define per(i,a,n) for (int i=n-1;i>=a;i--)
- #define pb push_back
- #define fi first
- #define se second
- typedef vector<int> VI;
- typedef long long ll;
- typedef pair<int,int> PII;
- const int inf=0x3fffffff;
- const ll mod=1000000007;
- const int maxn=1e5+100;
- int a[maxn];
- int main()
- {
- int n;
- scanf("%d",&n);
- ll sum=0,t=0;
- rep(i,1,n+1) scanf("%d",&a[i]),sum+=a[i],t+=i;
- if(sum%t)
- {
- puts("NO");
- return 0;
- }
- ll tmp=a[1];
- rep(i,1,n) a[i]=a[i+1]-a[i];
- a[n]=tmp-a[n];
- ll d=sum/t;
- rep(i,1,n+1)
- {
- a[i]-=d;
- if(a[i]%n||a[i]>0)
- {
- puts("NO");
- return 0;
- }
- }
- puts("YES");
- return 0;
- }