-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPERMUT1.cpp
56 lines (49 loc) · 995 Bytes
/
PERMUT1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<stdio.h>
#define Nmax 13
using namespace std;
int max(int a, int b)
{
if(a > b)
return a;
return b;
}
int main()
{
int t, n, k;
int mem[Nmax][Nmax*(Nmax-1)/2] = {0};
// precomputation of all the results
for(int i = 0; i < Nmax;++i)
{
mem[i][0] = 1;
mem[i][i*(i-1)/2] = 1;
}
mem[1][1] = 1;
mem[2][1] = 1;
for(int i = 2; i < Nmax; ++i)
{
for(int j = 1; j < i*(i-1)/2; ++j)
{
int result = 0 ;
int temp = max(1, i-j);
for(int l = temp; l <= i; ++l)
{
result += mem[i-1][j-i+l];
}
mem[i][j] = result;
}
}
scanf("%d", &t);
while(t-->0)
{
scanf("%d%d", &n, &k);
// first case to eliminate results
if( k > n*(n-1)/2 )
{
printf("0\n");
continue;
}
printf("%d\n", mem[n][k]);
}
return 0;
}