AtCoder - 2303 Boxes 神奇的题目

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
5
4 5 1 2 3
Sample Output 1
YES
All the stones can be removed in one operation by selecting the second box.

Sample Input 2
5
6 9 12 10 8
Sample Output 2
YES
Sample Input 3
4
1 2 3 1
Sample Output 3
NO

这题当时看没有看懂,就一直撂在一遍,现在重新拾起来,至今大部分的题解我还没有看懂。
这是我第一次见到用线性代数的思维来做的题目。还是太年轻,做题太少。

题目大意:就是说有n个盒子,每个盒子里面有ai块石头。可以选择第i个盒子,然后往后依次在第(i+j)%n个盒子中取走j块石头,1<=j<=n,如果盒子里的石头不够则不能进行操作。问能否把石头全部取走。

思路:
取走石头的方式无非就n种,也就是说选择第i个盒子的方式有n种,假设每种有xi次操作。
那么就有方程组可得。写成矩阵形式

这里写图片描述

在这里面规定好了只能拿走石头,而不能放石头在里面,所以对于每一个xi,都不可能是小于0或者是小数的。
看起来这个方程很大,但是并不难解。
首先将每一个方程项加起来得:
(1+2+3+…+n)*(x1+x2+…+xn)=a1+a2+a3+…+an.
设x1+x2+x3+…+xn=s, a1+a2+a3+…+an=sum;
那么就得到s=sum/(n*(n+1)/2);(这里sum必须是整除的,否则没有必要继续计算下去,直接输出no)
然后相邻的方程相减,比如第一个减去第二个得到
(n-1)*x1-x2-x3-…-xn=a1-a2;
-s+n*x1=a1-a2;
x1=(s+a1-a2)/n;
这样就能解出x1了,这里要判断条件,如果x1是负数或者是小数,那么当然就不能达成目的,自然输出no,否则输出yes。

下面是代码:要用long long

#include<iostream>
#include<cstdio>
#include<cmath>
typedef long long ll;
using namespace std;
ll n;
ll a[100010];
ll dis[100010];
int main()
{
    ll sum=0;
    scanf("%lld",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    ll k;
    if((2*sum%(n*(n+1)))!=0)
    {
        printf("NO");
        return 0;
    }
    k=2*sum/n/(n+1);
    for(int i=0;i<n;i++)
    {
        if(i==n-1)
            dis[i]=a[i]-a[0];
        else
            dis[i]=a[i]-a[i+1];
    }
    for(int i=0;i<n;i++)
    {
        if(dis[i]+k<0||(dis[i]+k)%n!=0)
        {
            printf("NO");
            return 0;
        }
    }
    printf("YES");
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值