CodeCraft-21 and Codeforces Round 711 (Div. 2) C. Planar Reflections

Planar Reflections

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Gaurang has grown up in a mystical universe. He is faced by n n n consecutive 2D planes. He shoots a particle of decay age k k k at the planes.

A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age k − 1 k-1 k1. If a particle has decay age equal to 1 1 1, it will NOT produce a copy.

For example, if there are two planes and a particle is shot with decay age 3 3 3 (towards the right), the process is as follows: (here, D ( x ) D(x) D(x) refers to a single particle with decay age x x x)

  1. the first plane produces a D ( 2 ) D(2) D(2) to the left and lets D ( 3 ) D(3) D(3) continue on to the right;
  2. the second plane produces a D ( 2 ) D(2) D(2) to the left and lets D ( 3 ) D(3) D(3) continue on to the right;
  3. the first plane lets D ( 2 ) D(2) D(2) continue on to the left and produces a D ( 1 ) D(1) D(1) to the right;
  4. the second plane lets D ( 1 ) D(1) D(1) continue on to the right ( D ( 1 ) D(1) D(1) cannot produce any copies).

In total, the final multiset S S S of particles is { D ( 3 ) , D ( 2 ) , D ( 2 ) , D ( 1 ) } \{D(3), D(2), D(2), D(1)\} {D(3),D(2),D(2),D(1)}. (See notes for visual explanation of this test case.)

Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset S S S, given n n n and k k k.

Since the size of the multiset can be very large, you have to output it modulo 1 0 9 + 7 10^9+7 109+7.

Note: Particles can go back and forth between the planes without colliding with each other.

Input

The first line of the input contains the number of test cases t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100). Then, t t t lines follow, each containing two integers n n n and k k k ( 1 ≤ n , k ≤ 1000 1 \le n, k \le 1000 1n,k1000).

Additionally, the sum of n n n over all test cases will not exceed 1000 1000 1000, and the sum of k k k over all test cases will not exceed 1000 1000 1000. All test cases in one test are different.

Output

Output t t t integers. The i i i-th of them should be equal to the answer to the i i i-th test case.

Example

i n p u t \tt input input
4
2 3
2 2
3 1
1 3
o u t p u t \tt output output
4
3
1
2
i n p u t \tt input input
3
1 1
1 500
500 250
o u t p u t \tt output output
1
2
257950823

Note

Let us explain the first example with four test cases.

Test case 1: ( n = 2 n = 2 n=2, k = 3 k = 3 k=3) is already explained in the problem statement.

See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other)

Test case 2: ( n = 2 n = 2 n=2, k = 2 k = 2 k=2) is explained as follows:

  1. the first plane produces a D ( 1 ) D(1) D(1) to the left and lets D ( 2 ) D(2) D(2) continue on to the right;
  2. the second plane produces a D ( 1 ) D(1) D(1) to the left and lets D ( 2 ) D(2) D(2) continue on to the right;
  3. the first plane lets D ( 1 ) D(1) D(1) continue on to the left ( D ( 1 ) D(1) D(1) cannot produce any copies).

Total size of multiset obtained { D ( 1 ) , D ( 1 ) , D ( 2 ) } \{D(1), D(1), D(2)\} {D(1),D(1),D(2)} is equal to three.

Test case 3: ( n = 3 n = 3 n=3, k = 1 k = 1 k=1), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one.

Test case 4: ( n = 1 n = 1 n=1, k = 3 k = 3 k=3) there is only one plane. The particle produces a new copy to the left. The multiset { D ( 2 ) , D ( 3 ) } \{D(2), D(3)\} {D(2),D(3)} is of size two.

Tutorial

本题可以用动态规划来解决

d p i , j dp_{i, j} dpi,j 为光线强度为 i i i 时,前面还有 j j j 面镜子时,最后会得到的光线数量,由于每次一束强度为 k k k 的光穿过镜子还会反射出一束强度为 k − 1 k - 1 k1 的光线往反方向照射,所以可以得到递推公式 d p i , j = d p i , j − 1 + d p i − 1 , n − j dp_{i, j} = dp_{i, j - 1} + dp_{i - 1, n - j} dpi,j=dpi,j1+dpi1,nj

此解法时间复杂度为 O ( n k ) \mathcal O(nk) O(nk)

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long
const int mod = 1e9 + 7; // 998244353;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<vector<int>> dp(k + 1, vector<int>(n + 1));
    for (int i = 1; i <= k; ++i) {
        dp[i][0] = 1;
        for (int j = 1; j <= n; ++j) {
            dp[i][j] = (dp[i][j - 1] + dp[i - 1][n - j]) % mod;
        }
    }
    cout << dp[k][n] << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值