Dortmund Dilemma


Problem Statement :


Borussia Dortmund are a famous football ( soccer ) club from Germany. Apart from their fast-paced style of playing, the thing that makes them unique is the hard to pronounce names of their players ( błaszczykowski , papastathopoulos , großkreutz etc. ).

The team's coach is your friend. He is in a dilemma as he can't decide how to make it easier to call the players by name, during practice sessions. So, you advise him to assign easy names to his players. A name is easy to him if

1. It consists of only one word.
2. It consists of only lowercase english letters.
3. Its length is exactly N.
4. It contains exactly K different letters from the 26 letters of English alphabet.
5. At least one of its proper prefixes matches with its proper suffix of same length.

Given, N and K you have to tell him the number of easy names he can choose from modulo (10^9+7).

Note : A prefix P of a name W is proper if, P!=W. Similarly, a suffix S of a name W is proper if, S!=W.

Input Format
The first line of the input will contain T ( the number of testcases ). Each of the next  lines will contain  space separated integers N and K.

Output Format
For each testcase, output the number of ways the coach can assign names to his players modulo (10^9+9).

Constraints

1 <= T <= 10^5
1 <= N <= 10^5
1 <= K <= 26



Solution :



title-img


                            Solution in C :

In C++ :





#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
typedef long long LL;
using namespace std;
const int SIZE = 1e5+5;
const int MOD = 1e9+9;
LL C[27][27],an[27][SIZE],tmp[27][SIZE],pp[27][SIZE],tmp2[27][SIZE],ha[27];
int main(){
    REPP(i,1,27){
        pp[i][0]=1;
        REPP(j,1,SIZE)pp[i][j]=pp[i][j-1]*i%MOD;
    }
    REP(i,27){
        C[i][0]=1;
        REPP(j,1,i+1){
            C[i][j]=C[i-1][j-1]+C[i-1][j];
        }
    }
    REPP(i,1,27){
        an[i][1]=i;
        tmp[i][1]=i;
        REPP(j,2,SIZE){
            if(j%2==0)an[i][j]=tmp[i][j/2];
            else an[i][j]=tmp[i][j/2]*i%MOD;
            an[i][j]=pp[i][j]-an[i][j];
            REPP(k,1,i){
                an[i][j]=(an[i][j]-an[k][j]*C[i][k])%MOD;
            }
            if(an[i][j]<0)an[i][j]+=MOD;
            tmp[i][j]=tmp[i][j-1]*i*i%MOD;
            REPP(k,1,i+1){
                tmp[i][j]=(tmp[i][j]+C[i][k]*an[k][j])%MOD;
            }

        }

    }
    CASET{ 
        DRII(N,K);
        if(N==1){
            puts("0");
            continue;
        }
        LL res=0;
        if(N%2==0){
            ha[1]=tmp[1][N/2];
            REPP(i,2,K+1){
                ha[i]=tmp[i][N/2];
                REPP(j,1,i)ha[i]=(ha[i]-ha[j]*C[i][j])%MOD;
                if(ha[i]<0)ha[i]+=MOD;
            }
            res=ha[K]*C[26][K]%MOD;
        }
        else{
            ha[1]=tmp[1][N/2];
            REPP(i,2,K+1){
                ha[i]=tmp[i][N/2]*i%MOD;
                REPP(j,1,i)ha[i]=(ha[i]-ha[j]*C[i][j])%MOD;
                if(ha[i]<0)ha[i]+=MOD;
            }
            res=ha[K]*C[26][K]%MOD;
        }
        if(K==1)res=26;
        if(res<0)res+=MOD;
        printf("%lld\n",res);
    }
    return 0;
}









In Java :





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

public class Solution {

    static final long MOD = 1000000009;
    static final int MAXK = 26;
    static final int MAXN = 100000;

    static long modPow(long x, long pow) {
        long r = 1;
        while (pow > 0) {
            if (pow % 2 == 1) {
                r = r * x % MOD;
            }
            pow /= 2;
            x = x * x % MOD;
        }
        return r;
    }

    public static void solve(Input in, PrintWriter out) throws IOException {
        long[][] c = new long[MAXK + 1][MAXK + 1];
        for (int i = 0; i < c.length; ++i) {
            for (int j = 1; j < i; ++j) {
                c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
            }
            c[i][0] = c[i][i] = 1;
        }
        long[][] d = new long[MAXK + 1][MAXN + 1];
        for (int j = 1; j <= MAXK; ++j) {
            d[j][0] = 1;
        }
        for (int i = 1; i <= MAXN; ++i) {
            for (int j = 1; j <= MAXK; ++j) {
                d[j][i] = (d[j][i - 1] * j + (i % 2 == 0 ? MOD - d[j][i / 2] : 0)) % MOD;
            }
        }
        for (int i = 1; i <= MAXN; ++i) {
            for (int j = 1; j <= MAXK; ++j) {
                d[j][i] = (modPow(j, i) + MOD - d[j][i]) % MOD;
                for (int j1 = 1; j1 < j; ++j1) {
                    d[j][i] = (d[j][i] + (MOD - c[j][j1]) * d[j1][i]) % MOD;
                }
            }
        }
        int tests = in.nextInt();
        for (int test = 0; test < tests; ++test) {
            int n = in.nextInt();
            int k = in.nextInt();
            out.println(d[k][n] * c[MAXK][k] % MOD);
        }
    }

    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        solve(new Input(new BufferedReader(new InputStreamReader(System.in))), out);
        out.close();
    }

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

        public Input(BufferedReader in) {
            this.in = in;
        }

        public Input(String s) {
            this.in = new BufferedReader(new StringReader(s));
        }

        public String next() throws IOException {
            sb.setLength(0);
            while (true) {
                int c = in.read();
                if (c == -1) {
                    return null;
                }
                if (" \n\r\t".indexOf(c) == -1) {
                    sb.append((char)c);
                    break;
                }
            }
            while (true) {
                int c = in.read();
                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>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define M (1000000009)

long long NCR[27][27];
long long DP[100001][27];

int main() {
    int T,N,K;
    for(int n = 0; n <= 26; n++) {
        NCR[n][0] = 1;
        NCR[n][n] = 1;
        for(int r = 1; r < n; r++) {
            NCR[n][r] = (NCR[n-1][r-1] + NCR[n-1][r])%M;
        }
    }
    for(int k = 1; k <= 26; k++) {
        DP[0][k] = 1;
        for(int n = 1; n <= 100000; n++) {
            DP[n][k] = (k*DP[n-1][k])%M;
            if(n%2 == 0) {
                DP[n][k] -= DP[n/2][k];
                DP[n][k] %= M;
                if(DP[n][k] < 0) {
                    DP[n][k] += M;
                }
            }
        }
    }
    for(int k = 1; k <= 26; k++) {
        long long X = 1;
        for(int n = 0; n <= 100000; n++) {
            DP[n][k] = X - DP[n][k];
            if(DP[n][k] < 0) {
                DP[n][k] += M;
            }
            X = (X*k)%M;
        }
    }
    for(int k = 1; k <= 26; k++) {
        for(int n = 0; n <= 100000; n++) {
            for(int j = 1; j < k; j++) {
                DP[n][k] -= (NCR[k][j]*DP[n][j])%M;
                if(DP[n][k] < 0) {
                    DP[n][k] += M;
                }
            }
        }
    }
    scanf("%d",&T);
    for(int i = 0; i < T; i++) {
        scanf("%d",&N);
        scanf("%d",&K);
        printf("%lld\n", (NCR[26][K]*DP[N][K])%M);
    }
    return 0;
}









In Python3 :






MOD = 10 ** 9 + 9
N = 10 ** 5
K = 26

class Solver(object):
    def __init__(self):
        self.factorials = [1 for _ in range(26 + 1)]
        for i in range(1, 26 + 1):
            self.factorials[i] = self.factorials[i - 1] * i

        self.F = [[0 for _ in range(K + 1)] for _ in range(N + 1)]
        for j in range(1, K + 1):
            self.F[1][j] = j
            for i in range(2, N + 1):
                if i % 2 == 0:
                    self.F[i][j] = self.F[i - 1][j] * j - self.F[i // 2][j]
                else:
                    self.F[i][j] = self.F[i - 1][j] * j
                self.F[i][j] %= MOD

        self.G = [[0 for _ in range(K + 1)] for _ in range(N + 1)]

        for j in range(1, K + 1):
            total = j
            for i in range(1, N + 1):
                self.G[i][j] = total - self.F[i][j]
                self.G[i][j] %= MOD
                total *= j
                total %= MOD

    def solve(self, n, k):
        P = 0
        if k == 1:
            P += self.G[n][k]
        else:
            for j in range(k, 0, -1):
                P += (-1) ** (k - j) * self.G[n][j] * (
                    self.factorials[k] // (self.factorials[j] * self.factorials[k - j]))
                P %= MOD

        res = P * (self.factorials[26] // (self.factorials[k] * self.factorials[26 - k]))
        return res % MOD


if __name__ == "__main__":
    import sys

    testcases = int(input())
    solver = Solver()
    for _ in range(testcases):
        n, k = map(int, input().split())
        print(solver.solve(n, k))
                        








View More Similar Problems

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →

Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →

Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

View Solution →

Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

View Solution →