# King and Four Sons

### Problem Statement :

```The King of Byteland wants to grow his territory by conquering K other countries. To prepare his 4 heirs for the future, he decides they must work together to capture each country.

The King has an army, A, of N battalions; the ith battalion has Ai soldiers. For each battle, the heirs get a detachment of soldiers to share but will fight amongst themselves and lose the battle if they don't each command the same number of soldiers (i.e.: the detachment must be divisible by 4). If given a detachment of size , the heirs will fight alone without any help.

The battalions chosen for battle must be selected in the following way:

1.A subsequence of K battalions must be selected (from the N battalions in army A).
2.The jth battle will have a squad of soldiers from the jth selected battalion such that its size is divisible by 4.
The soldiers within a battalion have unique strengths. For a battalion of size 5, the detachment of soldiers {0,1,2,4} is different from the detachment of soldiers

The King tasks you with finding the number of ways of selecting K detachments of battalions to capture K countries using the criterion above. As this number may be quite large, print the answer modulo 10^9+7.

Input Format

The first line contains two space-separated integers, N (the number of battalions in the King's army) and K (the number of countries to conquer), respectively.

The second line contains N space-separated integers describing the King's army, A, where the ith integer denotes the number of soldiers in the ith battalion (Ai).

Constraints

1 <= N <= 10^4
1 <= K <= min(100,N)
1 <= Ai <= 10^9
1 <= Ai <= 10^3 holds for test cases worth at least 30% of the problem's score.
Output Format

Print the number of ways of selecting the K detachments of battalions modulo 10^9+7.```

### Solution :

```                            ```Solution in C :

In C++ :

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;
const int mod = 1e9 + 7;

pii mul(pii a, pii b) {
ll c = (ll) a.st * b.st - (ll) a.nd * b.nd;
ll d = (ll) a.st * b.nd + (ll) a.nd * b.st;
c %= mod;
d %= mod;
if(c < 0) c += mod;
if(d < 0) d += mod;
return mp((int) c, (int) d);
}

pii pw(pii a, int k) {
pii r = mp(1, 0);
while(k) {
if(k % 2) r = mul(r, a);
a = mul(a, a);
k /= 2;
}
return r;
}
int pw(int a, int k) {
int r = 1;
while(k) {
if(k % 2) r = (ll) r * a % mod;
a = (ll) a * a % mod;
k /= 2;
}
return r;
}

int f(int n) {
int r = pw(mp(1,mod-1), n).st + pw(mp(1,1), n).st;
r %= mod;
r += pw(2, n);
r %= mod;
r = (ll) r * pw(4, mod - 2) % mod;
return r;
}

int dp[105];

int main() {
int n, k;
scanf("%d%d", &n, &k);
dp[0] = 1;
REP(_, n) {
int a;
scanf("%d", &a);
a = f(a);
FORD(j, k, 1)
dp[j] = (dp[j] + (ll) dp[j-1] * a) % mod;
}
printf("%d\n", dp[k]);
return 0;
}

In Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
int mod = 1000000007;
int[] waysA;

public void solve(int[] a, int k) {
buildAToWays(a);
long[] c = new long[a.length + 1];
for (int n = 0; n <= a.length; n++) c[n] = 1;
for (int ik = 1; ik <= k; ik++) {
long[] c1 = new long[a.length + 1];
c1[ik] = (waysA[ik - 1] * c[ik - 1]) % mod;
for (int n = ik + 1; n <= a.length; n++) {
c1[n] = (c1[n-1] + waysA[n - 1] * c[n - 1]) % mod;
}
c = c1;
}
System.out.println(c[a.length]);
}

long power2(int n) {
if(n < 2) return n == 0 ? 1 : 2;
long n2 = power2(n / 2);
n2 = (n2 * n2) % mod;
return n % 2 == 0 ? n2 : (n2 * 2) % mod;
}

long waysForA(int a) {
if(a < 4) return 1;
long result = power2(a - 2);
int m = a % 8;
if(m == 2 || m == 6) return result;
if(m == 7 || m == 1) return (result + power2((a - 3)/2)) % mod;
if(m == 0) return (result + power2((a - 2)/2)) % mod;
if(m == 3 || m == 5) return (result + mod - power2((a - 3)/2)) % mod;
if(m == 4) return (result + mod - power2((a - 2)/2)) % mod;
return -1;
}

void buildAToWays(int[] a) {
waysA = new int[a.length];
for (int i = 0; i < a.length; i++) {
waysA[i] = (int)waysForA(a[i]);
}
}

public void run() {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
solve(a, k);
}

public static void main(String[] args) {
Solution s = new Solution();
s.run();
}
}

In C :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MOD 1000000007
void one(long long*a,int SIZE);
void mul(long long*a,long long*b,int SIZE);
void powm(long long*a,int n,long long*res,int SIZE);
int a[10000];
long long dp[101][10001]={0},b[10000],B[4][4],C[4][4],A[4][4]={
{1,0,0,1},
{1,1,0,0},
{0,1,1,0},
{0,0,1,1}
},aa[4]={1,1,0,0};

int main(){
int N,K,i,j;
scanf("%d%d",&N,&K);
for(i=0;i<N;i++)
scanf("%d",a+i);
for(i=0;i<N;i++)
if(!a[i])
b[i]=1;
else{
memcpy(B,A,sizeof(B));
powm(&B[0][0],a[i]-1,&C[0][0],4);
for(j=b[i]=0;j<4;j++)
b[i]=(b[i]+aa[j]*C[0][j])%MOD;
}
dp[0][0]=1;
for(i=0;i<N;i++)
for(j=0;j<=K;j++)
if(j)
dp[j][i+1]=(dp[j][i]+dp[j-1][i]*b[i])%MOD;
else
dp[j][i+1]=dp[j][i];
printf("%lld",dp[K][N]);
return 0;
}
void one(long long*a,int SIZE){
int i,j;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
a[i*SIZE+j] = (i == j);
return;
}
void mul(long long*a,long long*b,int SIZE){
int i,j,k;
long long res[SIZE][SIZE];
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
res[i][j]=0;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
for (k = 0; k < SIZE; k++)
res[i][j] = (res[i][j]+a[i*SIZE+k] * b[k*SIZE+j])%MOD;
for (i = 0; i < SIZE; i++)
for (j = 0; j < SIZE; j++)
a[i*SIZE+j] = res[i][j];
return;
}
void powm(long long*a,int n,long long*res,int SIZE){
one(res,SIZE);
while (n > 0) {
if (n % 2 == 0)
{
mul(a, a,SIZE);
n /= 2;
}
else {
mul(res, a,SIZE);
n--;
}
}
}

In Python3 :

#!/bin/python3

import os
import sys
from functools import reduce
from itertools import combinations
#
# Complete the king function below.
#
cc = 10**9 + 7
d30 = 73741817
D2 = 976371285
D3 = 688423210
D4 = 905611805
D5 = 607723520
D6 = 235042059
D7 = 255718402
D8 = 494499948
def twopower(a):
if a < 30:
return (2**a)
elif a >= 30 and a <100:
dvi = a//30
rem = a%30
ans = ((d30**dvi)%cc) * ((2**rem)%cc)
ans = ans%cc
return (ans)
elif a >= 100 and a <= 1000:
dvi2 = a//100
dvi = a%100//30
rem = a%100%30
ans = ((D2**dvi2)%cc) * ((d30**dvi)%cc)*((2**rem)%cc)
ans = ans%cc
return (ans)
def midpower(a):
if a <= 1000:
return (twopower(a))
elif a > 1000 and a <= 10**4:
x = a //1000
y = a % 1000
z = ((D3**x)%cc) * twopower(y)
z = z%cc
return (z)
elif a >10**4 and a <= 10**5:
x1 = a // 10**4
x2 = a // 10**3 - x1*10
y = a % 10**3
z = ((D4**x1)%cc) * ((D3**x2)%cc) * twopower(y)
z = z % cc
return (z)
elif a >10**5 and a <= 10**6:
x1 = a // 10**5
x2 = a // 10**4 - x1*10
x3 = a // 10**3 - x2*10 - x1*100
y = a % 10**3
z = ((D5**x1)%cc) * ((D4**x2)%cc) * ((D3**x3)%cc) * twopower(y)
z = z % cc
return (z)
def hipower(a):
if a <= 10**6:
return (midpower(a))
elif a > 10**6 and a <= 10**7:
x = a //10**6
y = a % 10**6
z = ((D6**x)%cc) * midpower(y)
z = z%cc
return (z)
elif a >10**7 and a <= 10**8:
x1 = a // 10**7
x2 = a // 10**6 - x1*10
y = a % 10**6
z = ((D7**x1)%cc) * ((D6**x2)%cc) * midpower(y)
z = z % cc
return (z)
elif a >10**8 and a <= 10**9:
x1 = a // 10**8
x2 = a // 10**7 - x1*10
x3 = a // 10**6 - x2*10 - x1 *100
y = a % 10**6
z = ((D8**x1)%cc) * ((D7**x2)%cc) * ((D6**x3)%cc) * midpower(y)
z = z % cc
return (z)

def fanum(a):
if a%4 == 0 or a%4 == 1:
if (a//4) % 2 == 0:
return (int(hipower(a-2)+hipower(2*(a//4)-1)))
else:
return (int(hipower(a-2)-hipower(2*(a//4)-1)))
elif a%4 == 3:
if (a//4) % 2 == 0:
return (int(hipower(a-2) - hipower(2*(a//4))))
else:
return (int(hipower(a-2) + hipower(2*(a//4))))
elif a%4 == 2:
return (int(hipower(a-2)))
def king(army, k):
lst = []
for i in army:
lst.append(fanum(i))
lst1 = []
lst1.append(lst)
for i in range(1,k):
l = []
el = 0
for j in range(i,n):
el = (el + lst1[i-1][j-i])%cc
l.append((el*lst[j])%cc)
lst1.append(l)
tt = sum(lst1[-1])
return(tt%cc)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

nk = input().split()

n = int(nk[0])

k = int(nk[1])

army = list(map(int, input().rstrip().split()))

result = king(army, k)

fptr.write(str(result) + '\n')

fptr.close()```
```

## 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

## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

## 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