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 :
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
The Strange Function
One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting
View Solution →Self-Driving Bus
Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever
View Solution →Unique Colors
You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti
View Solution →Fibonacci Numbers Tree
Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T
View Solution →Pair Sums
Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v
View Solution →Lazy White Falcon
White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi
View Solution →