问题 K: Boxes
时间限制: 2 Sec 内存限制: 256 MB提交: 254 解决: 52
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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个数,可对这n个数,执行任意次以下操作,选定一个i,用j从1-n开始循环,另第i+j个数减j,若第i+j个数小于j则不会再减去j。
分析:每执行一次这个操作,就会使数组总和减去n(n+1)/2,所以初始数组总和sum对n(n+1)/2取余一定为0,否则输出NO。每次操作一定是-1,-2,-3.......-n,所以差分一下就是每次都-1。但是对于第一个数和最后一个数的值缺增加了-(n-1)(可以推一下)。对于差分数组b[i]必须满足关系式:b[i] − (k − x) + (n − 1)x = 0,k是执行了几次操作,x是选了这个数x次作为操作中的i。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
ll sum=0,n,a[MAX],x;
ll b[MAX];
int main()
{
ll i,j;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum=sum+a[i];
}
if(sum%(n*(n+1)/2))
{
printf("NO\n");
return 0;
}
x=sum/(n*(n+1)/2);
b[1]=a[1]-a[n]-x;
for(i=2;i<=n;i++)
b[i]=a[i]-a[i-1]-x;
for(i=1;i<=n;i++)
{
if(b[i]-x>0 || (-b[i])%n>0)
{
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}