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

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →

Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →