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 :



title-img


                            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()
                        








View More Similar Problems

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 →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →