#abc209f. F - Deforestation
F - Deforestation
Score : points
问题描述
有 棵树从左至右排成一行。第 棵树()从左边算起,即树 ,其高度为 。
现在你将按照你喜欢的顺序砍倒这 棵树。具体来说,你需要选择一个 的排列 ,并按照以下方式依次对 进行操作:
- 砍倒树 ,即将 设置为 ,此时的成本为 。
这里,我们约定 ,。
换句话说,砍倒一棵树的成本是砍倒之前该树及其相邻两棵树的总高度之和。
找出那些能使得砍倒所有树木的总成本最小的排列 的个数。由于这个计数可能非常大,请输出模 后的结果。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are trees standing in a row from left to right. The -th tree from the left, Tree , has the height of .
You will now cut down all these trees in some order you like. Formally, you will choose a permutation of and do the operation below for each in this order.
- Cut down Tree , that is, set to , at a cost of .
Here, we assume .
In other words, the cost of cutting down a tree is the total height of the tree and the neighboring trees just before doing so.
Find the number of permutations that minimize the total cost of cutting down the trees. Since the count may be enormous, print it modulo .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of permutations , modulo , that minimize the total cost of cutting down the trees.
Sample Input 1
3
4 2 4
Sample Output 1
2
There are two permutations that minimize the total cost: and .
Below, we will show the process of cutting down the trees for , for example.
- First, Tree is cut down at a cost of .
- Next, Tree is cut down at a cost of .
- Finally, Tree is cut down at a cost of .
The total cost incurred is .
Sample Input 2
3
100 100 100
Sample Output 2
6
Sample Input 3
15
804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 189641422 25202363 350490028 783368691 102520060 44897764
Sample Output 3
54537651
Be sure to print the count modulo .
update @ 2024/3/10 09:23:45