codechef AUG17 T5 Chef And Fibonacci Array

本文介绍了一个有趣的编程挑战:通过特定的操作来变换初始数组,并计算所有可能的数组组合数量。问题中涉及动态规划策略,用于高效解决该问题。

Chef has an array A = (A1, A2, ..., AN), which has N integers in it initially. Chef found that for i ≥ 1, if Ai > 0, Ai+1 > 0, and Ai+2 exists, then he can decrease both Ai, andAi+1 by one and increase Ai+2 by one. If Ai+2 doesn't exist, but Ai > 0, and Ai+1 > 0, then he can decrease both Ai, and Ai+1 (which will be the currently last two elements of the array) by one and add a new element at the end, whose value is 1.

Now Chef wants to know the number of different arrays that he can make from A using this operation as many times as he wishes. Help him find this, and because the answer could be very large, he is fine with you reporting the answer modulo 109+7.

Two arrays are same if they have the same number of elements and if each corresponding element is the same. For example arrays (2,1,1) and (1,1,2) are different.

Input

  • The first line of the input contains a single integer T denoting the number of test cases.
  • The first line contains a single integer N denoting the initial number of elements inA.
  • The second line contains N space-separated integers: A1, A2, ... , AN.

Output

For each test case, output answer modulo 109+7 in a single line.

Constraints

  • 1 ≤ T ≤ 5
  • 1 ≤ N ≤ 50
  • 0 ≤ Ai ≤ 50

Subtasks

  • Subtask 1 (20 points) : 1 ≤ N ≤ 8, 0 ≤ Ai ≤ 4
  • Subtask 2 (80 points) : Original constraints

Example

Input:
3
3
2 3 1
2
2 2
3
1 2 3

Output:
9
4
9

Explanation

Example case 1.

We'll list the various single steps that you can take (ie. in one single usage of the operation):

  • (2, 3, 1) → (2, 2, 0, 1)
  • (2, 2, 0, 1) → (1, 1, 1, 1)
  • (1, 1, 1, 1) → (1, 1, 0, 0, 1)
  • (1, 1, 0, 0, 1) → (0, 0, 1, 0, 1)
  • (1, 1, 1, 1) → (1, 0, 0, 2)
  • (1, 1, 1, 1) → (0, 0, 2, 1)
  • (2, 3, 1) → (1, 2, 2)
  • (1, 2, 2) → (0, 1, 3)

 

So all the arrays you can possibly get are:

(2, 3, 1), (2, 2, 0, 1), (1, 1, 1, 1), (1, 1, 0, 0, 1), (0, 0, 1, 0, 1), (1, 0, 0, 2), (0, 0, 2, 1), (1, 2, 2), and (0, 1, 3)

Since there are 9 different arrays that you can reach, the answer is 9.

——————————————————————————————————

这道题明显每次只关系到相邻两位QAQ

所以我们可以从左到右dp

f【i】【j】【k】表示i-1位已经处理并且i值为j进位为k 所以i+1的值就是v【i+1】+k

然后我们就枚举操作次数x(x<=v【i+1】+k&&x<=j)推出i+1的情况就好辣

易得i个数比n打不了多少 我们求出最大的 i 答案就是f【i】【0】【0】辣

而j也不会超过一个值 这里我带了个200 至于k同理咯QAQ

 

转载于:https://www.cnblogs.com/lyzuikeai/p/7301303.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值