CF1513E.Cost Equilibrium

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

An array is called beautiful if all the elements in the array are equal.

You can transform an array using the following steps any number of times:

  1. Choose two indices ii and jj ( 1i,jn1 \leq i,j \leq n ), and an integer xx ( 1xai1 \leq x \leq a_i ). Let ii be the source index and jj be the sink index.
  2. Decrease the ii -th element by xx , and increase the jj -th element by xx . The resulting values at ii -th and jj -th index are aixa_i-x and aj+xa_j+x respectively.
  3. The cost of this operation is $x \cdot |j-i| $ .
  4. Now the ii -th index can no longer be the sink and the jj -th index can no longer be the source.

The total cost of a transformation is the sum of all the costs in step 33 .For example, array [0,2,3,3][0, 2, 3, 3] can be transformed into a beautiful array [2,2,2,2][2, 2, 2, 2] with total cost 113+114=51 \cdot |1-3| + 1 \cdot |1-4| = 5 .

An array is called balanced, if it can be transformed into a beautiful array, and the cost of such transformation is uniquely defined. In other words, the minimum cost of transformation into a beautiful array equals the maximum cost.

You are given an array a1,a2,,ana_1, a_2, \ldots, a_n of length nn , consisting of non-negative integers. Your task is to find the number of balanced arrays which are permutations of the given array. Two arrays are considered different, if elements at some position differ. Since the answer can be large, output it modulo 109+710^9 + 7 .

输入格式

The first line contains a single integer nn ( 1n1051 \leq n \leq 10^5 ) — the size of the array.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 0ai1090 \le a_i \le 10^9 ).

输出格式

Output a single integer — the number of balanced permutations modulo 109+710^9+7 .

输入输出样例

  • 输入#1

    3
    1 2 3

    输出#1

    6
  • 输入#2

    4
    0 4 0 4

    输出#2

    2
  • 输入#3

    5
    0 11 12 13 14

    输出#3

    120

说明/提示

In the first example, [1,2,3][1, 2, 3] is a valid permutation as we can consider the index with value 33 as the source and index with value 11 as the sink. Thus, after conversion we get a beautiful array [2,2,2][2, 2, 2] , and the total cost would be 22 . We can show that this is the only transformation of this array that leads to a beautiful array. Similarly, we can check for other permutations too.

In the second example, [0,0,4,4][0, 0, 4, 4] and [4,4,0,0][4, 4, 0, 0] are balanced permutations.

In the third example, all permutations are balanced.

首页