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

int main() {
int n, k;
scanf("%d%d", &n, &k);
dp = 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;
long long dp={0},b,B,C,A={
{1,0,0,1},
{1,1,0,0},
{0,1,1,0},
{0,0,1,1}
},aa={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,a[i]-1,&C,4);
for(j=b[i]=0;j<4;j++)
b[i]=(b[i]+aa[j]*C[j])%MOD;
}
dp=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)

k = int(nk)

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

result = king(army, k)

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

fptr.close()```
```

## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -