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

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →