# Swap Permutation

### Problem Statement :

```You are given an array A = [1, 2, 3, ..., n]:

1.How many sequences (S1) can you get after exact k adjacent swaps on A?

2.How many sequences (S2) can you get after at most k swaps on A?

An adjacent swap can be made between two elements of the Array A, A[i] and A[i+1] or A[i] and A[i-1].
A swap otherwise can be between any two elements of the array A[i] and A[j] ∀ 1 ≤ i, j ≤ N, i ≠ j.

Input Format

First and only line contains n and k separated by space.

Constraints

1 ≤ n ≤ 2500
1 ≤ k ≤ 2500

Output Format

Output S1 % MOD and S2 % MOD in one line, where MOD = 1000000007.```

### Solution :

```                            ```Solution in C :

In C++ :

/*
*/

#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-11
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 256

using namespace std;

long n,k,s,dp,lwr,upr,ts;
long long ans1,ans2;

int main(){
//freopen("C:/input.txt","r",stdin);
//freopen("C:/output.txt","w",stdout);
ios_base::sync_with_stdio(0);
//cin.tie(0);

cin>>n>>k;
dp=1;
for (int i=0;i<=2500;i++)
s[i]=1;

for (int i=0;i<n;i++)
{
for (int j=0;j<=k;j++)
{
upr=j;
lwr=j-(n-i-1);
if (lwr<0)lwr=0;
ts=s[i][upr];
if (lwr>0)ts-=s[i][lwr-1];
ts+=bs;
ts%=bs;
dp[i+1][j]=ts;
}
for (int j=0;j<=k;j++)
{
s[i+1][j]=dp[i+1][j];
if (j){s[i+1][j]=(s[i+1][j-1]+s[i+1][j])%bs;}
}
}

ans1=0;
for (int i=k;i>=0;i-=2)
{ans1=(ans1+dp[n][i])%bs;}cout<<ans1;//<<endl;

for (int i=0;i<=n;i++)
for (int j=0;j<=k;j++)
dp[i][j]=s[i][j]=0;

dp=1;
for (int i=0;i<=2500;i++)
s[i]=1;

for (int i=0;i<n;i++)
{
for (int j=0;j<=k;j++)
{
lwr=n-i-1;

dp[i+1][j]=dp[i][j];
if (j)dp[i+1][j]+=(1ll*dp[i][j-1]*lwr)%bs;
dp[i+1][j]%=bs;
}
for (int j=0;j<=k;j++)
{
s[i+1][j]=dp[i+1][j];
if (j){s[i+1][j]=(s[i+1][j-1]+s[i+1][j])%bs;}
}
}

ans1=0;
for (int i=0;i<=k;i++)
ans1=(ans1+dp[n][i])%bs;
cout<<" "<<ans1<<endl;

cin.get();cin.get();
return 0;}

In Java :

import java.io.*;
import java.util.Arrays;

public class Solution {

final static long mod = 1000000007;

public static void solve(Input in, PrintWriter out) throws IOException {
int n = in.nextInt();
int k = in.nextInt();
long[] d = new long[k + 1];
d = 1;
for (int it = 0; it < n; ++it) {
long[] dd = new long[k + 1];
long sum = 0;
for (int i = 0; i <= k; ++i) {
sum = (sum + d[i]) % mod;
dd[i] = sum;
if (i >= it) {
sum = (sum + mod - d[i - it]) % mod;
}
}
d = dd;
}
//        System.err.println(Arrays.toString(d));
long ans1 = 0;
for (int i = 0; i <= k; ++i) {
if (i % 2 == k % 2) {
ans1 = (ans1 + d[i]) % mod;
}
}
long[] d1 = new long[n + 1];
d1 = 1;
for (int it = 0; it < n; ++it) {
for (int i = n; i >= 1; --i) {
d1[i] = (d1[i] * it + d1[i - 1]) % mod;
}
d1 = 0;
}
//        System.err.println(Arrays.toString(d1));
long ans2 = 0;
for (int i = 1; i <= n; ++i) {
if (n - i <= k) {
ans2 = (ans2 + d1[i]) % mod;
}
}
out.println(ans1 + " " + ans2);
}

public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
out.close();
}

static class Input {
StringBuilder sb = new StringBuilder();

this.in = in;
}

public Input(String s) {
}

public String next() throws IOException {
sb.setLength(0);
while (true) {
if (c == -1) {
return null;
}
if (" \n\r\t".indexOf(c) == -1) {
sb.append((char)c);
break;
}
}
while (true) {
if (c == -1 || " \n\r\t".indexOf(c) != -1) {
break;
}
sb.append((char)c);
}
return sb.toString();
}

public int nextInt() throws IOException {
return Integer.parseInt(next());
}

public long nextLong() throws IOException {
return Long.parseLong(next());
}

public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}

In C :

#include<stdio.h>

int i,j,k,n,mod;
long long ans1,ans2;
long long f,g;

main()
{
mod=1000000007;
scanf("%d%d",&n,&k);
f=1;
for (i=2; i<=n; i++)
{
f[i]=1;
for (j=1; j<=k && j<=i*(i-1)/2; j++)
{
f[i][j]=f[i][j-1];
if (j<=(i-1)*(i-2)/2) f[i][j]=(f[i][j]+f[i-1][j])%mod;
if (j>=i) f[i][j]=(f[i][j]-f[i-1][j-i]+mod)%mod;
}
}
ans1=0;
for (i=0; i<=k; i++)
if ((k-i)%2==0) ans1=(ans1+f[n][i])%mod;
g=1;
for (i=2; i<=n; i++)
{
g[i]=g[i-1]*(i-1)%mod;
for (j=2; j<i; j++)
g[i][j]=(g[i-1][j-1]+g[i-1][j]*(i-1)%mod)%mod;
g[i][i]=1;
}
ans2=0;

for (i=n; i>=n-k && i>=1; i--)
ans2=(ans2+g[n][i])%mod;
printf("%lld %lld\n",ans1,ans2);
}

In Python3 :

import sys
import math

if k==0:
return 1
else:
#n==2:
cntksteps = *(k+1)
#n>2:
tmp = *(k+1)
for i in range(3,n+1):
#0...i-1
prefixSum = 0
for j in range(min(i,k+1)):
prefixSum+=cntksteps[j]
prefixSum%=1000000007
tmp[j] = prefixSum
#i...k
for j in range(i,k+1):
prefixSum+=cntksteps[j]
prefixSum-=cntksteps[j-i]
tmp[j] = prefixSum
for j in range(k+1):
cntksteps[j] = tmp[j]
#print(cntksteps)
return cntksteps[k]%1000000007

def anySwaps(n,k):
if k==0:
return 1
elif n<=k:
return math.factorial(n)%1000000007
else:

tmp = *(k+1)
#n==2:
cntksteps = *(k+1)
cntksteps = 1
#n>2:
for i in range(3,n+1):
#if i<k:
#cntksteps[i] = cntksteps[i-1]*i
for j in range(1,k+1):
tmp[j] = (cntksteps[j]+(i-1)*cntksteps[j-1])%1000000007
for j in range(1,k+1):
cntksteps[j] = tmp[j]
#cntksteps = 1
#print(cntksteps)
return cntksteps[k]%1000000007

f = sys.stdin
n = int(tmp)
k = int(tmp)
```

## View More Similar Problems

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

## Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

## Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

## Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie