Boxes

Topic 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.

Constraints
1≤N≤105
1≤Ai≤109

enter

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

All the stones can be removed in one operation by selecting the second box.

The meaning of the question: Given N boxes, each box contains Ai stones.
Each operation can select a box i, and for each j (1 <= j <= N), remove j stones from the (i+j)th box (the N+kth box is the kth box )

If there are not enough stones in a box, the operation will not proceed. Ask if all the stones can be removed. Output yes if all can be removed otherwise output no.

Idea: If all the stones can be removed, then all the stones will add up to satisfy the sum of the first n items or satisfy the multiples of the sum of the first n items, and then if these are the difference between the two that the head can be completely removed will be It will be 1 or 1-n. According to this, use an array vis to save the difference between the two, and then use for to judge it. The point to note here is that I submitted several codes that were incorrect, and later found that it was longlong is not used when seeking the sum of the first n terms.




#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include<map>
#define mes(a,b) memset(a,b,sizeof(a))
#define rep(i,m,n) for(i=m;i<=n;i++)
typedef long long ll;
using namespace std;
int max3(int a,int b,int c){return max(max(a,b),c);}
ll min3(ll a,ll b,ll c){return min(min(a,b),c);}
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int n;
int a[100005],vis[100005];


int main(){
    while(cin>>n)
    {
        ll sum=0,sum2,t;
        bool flag=1;
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];

        }
         sum2=(ll)((1+n))*n/2;
        if(sum%sum2!=0)
        {
            printf("NO\n");
            continue;
        }


        t=sum/sum2;//printf("%d\n",t);
        a[n]=a[0];
        for(int i=0; i<n; i++)
            vis[i]=a[i+1]-a[i];
        for(int i=0; i<n; i++)
        {
            if((vis[i]-t)%n!=0 || vis[i]-t>0)
            {
                flag=0;
                break;
            }


        }
        if(flag==0)
            printf("NO\n");
        else
            printf("YES\n");
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324808352&siteId=291194637