-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathperm6.cpp
53 lines (49 loc) · 1.04 KB
/
perm6.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
# include <fstream>
# include <algorithm>
# include <cstring>
# define NR 1100
# define N 50
# define base 1000000
using namespace std;
ifstream f("perm6.in");
ofstream g("perm6.out");
int i,j,n,m,K,poz;
int a[N][NR][50], var[10]; // - numarul de permutari din i elemente cu j inversiuni
void aduna (int a[], int b[])
{
int t=0;
a[0]=max (a[0], b[0]);
for (int i=1; i<=a[0]; ++i)
{
a[i]=t+a[i]+b[i];
if (a[i]>=base) t=1, a[i]-=base;
else t=0;
}
if (t) a[++a[0]]=t;
}
void afisare (int k)
{
memset (var, 0, sizeof(var));
int i, VV=0;
while (k>0) var[++VV]=k%10, k=k/10;
for (i=6; i>=1; --i)
g<<var[i];
}
int main ()
{
f>>n>>K;
a[0][0][0]=a[0][0][1]=1;
for (i=1; i<=n; ++i)
{
for (poz=i; poz>=1; --poz)
{
for (j=0; j<=K - (i-poz); ++j)
aduna (a[i][j + (i-poz)], a[i-1][j]);
}
}
g<<a[n][K][a[n][K][0]]; --a[n][K][0];
for (i=a[n][K][0]; i>=1; --i)
afisare (a[n][K][i]);
g<<"\n";
return 0;
}