Codeforces Round #236 (Div. 1) D. Beautiful Pairs of Numbers

http://codeforces.com/contest/403/problem/D

D. Beautiful Pairs of Numbers

time limit pertest

3 seconds

memory limit pertest

256 megabytes

input

standard input

output

standard output

The sequence ofinteger pairs (a1, b1), (a2, b2), ..., (ak, bk) is beautiful,if the following statements are fulfilled:

·        1 ≤ a1 ≤ b1 < a2 ≤ b2 < ... < ak ≤ bk ≤ n, where n is a givenpositive integer;

·        all numbers b1 - a1b2 - a2...bk - ak aredistinct.

For the givennumber n find the number of beautiful sequences of length k. As the answercan be rather large, print the remainder after dividing it by 1000000007 (109 + 7).

Input

The first line containsinteger t (1 ≤ t ≤  2·105) — the numberof the test data.

Each of thenext t linescontains two integers n and k (1 ≤ k ≤ n ≤ 1000).

Output

For each testfrom the input print the answer to the problem modulo 1000000007 (109 + 7). Print theanswers to the tests in the order in which the tests are given in the input.

Sample test(s)

input

6

1 1

2 1

2 2

3 1

3 2

3 3

output

1

3

0

6

2

0

Note

In the firsttest sample there is exactly one beautiful sequence: (1, 1).

In the secondtest sample, the following sequences are beautiful:

·        (1, 1);

·        (1, 2);

·        (2, 2).

In the fourthtest sample, the following sequences are beautiful:

·        (1, 1);

·        (1, 2);

·        (1, 3);

·        (2, 2);

·        (2, 3);

·        (3, 3).

In the fifthtest sample, the following sequences are beautiful:

·        (1, 1), (2, 3);

·        (1, 2), (3, 3).

In the third andsixth samples, there are no beautiful sequences.

 

解题思路:

首先,每个pair差值都不相等,同样每个pair没有任何交集,that is b1<a2.

假设pair的差值是递增的,观察最小的pair组合:(1,1) (2,3) (4,6) (7,10) (11,15) (16,21)(22,28)

每个整数看做一个点,每个pair是一个区间,区间的长度为:1,2,3,4,5,6

因此对于输入的n,k,题目的问题等价于:计算k个长度不同的区间在[1,n]的数轴上有多上中分配方法。

例如 n=3, k=2.

[1,n]的数轴为 [1,3],数轴的长度=3因此k个长度不同的区间只能是,长度为1 长度为2 的区间.

因此分配方式是 1+2 2+1两种。

又例如n=4, k=2.

数轴[1,4],长度=42个不同的区间,其长度可以是 1,2,3

组合情况有 1+2, 1+3 两类。

对于1+2,结果有6中,[1 gap 2] [gap 1 2][1 2 gap][2 gap 1] [gap 2 1][2 1 gap]gap是区间没有覆盖的一个点。

对于1+3,结果又2种,[1,3]  [3,1]

8种。

以上是思路的理解,下面是详细解法。

首先,需要计算出一个正整数n划分为k个不同的正整数,有多少种划分方法,记为dp[n][k]

当程序输入n,k是,k个区间占用长度为i,则数轴上有k个长度不同的区间,有n-i个多余的gap点,因此这种情况有 (k+n-i)!/(n-i)! * dp[i][k]种分配方法,

·)前一部分(k+n-i)!/(n-i)!,即A(k + n-i,k).可以看出,1+2+...+45 >1000,因此k>45的情况答案都是0,可以限定k<45.因此需要预处理A(1000,45)

·)后一部分dp[i][k]的计算:

注意看以下等式,a+b+c = 3*a + (b-a) + (c-a). 也就是当 a,b,c是递增时(a最小是1),则 b-a,c-a 也是递增(b-a最小是1)。【子问题】

如是,dp[n][k]可以从dp[n-k*a][k-1]转换,重点落在最小的a是怎么取值。

 

ps:很久没有写acm博客了,原谅这一次写的这么不规范,大致意思还是能看懂。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值