Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Chef World CodeChef Solution

Problem -Chef World CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Chef World CodeChef Solution in C++14

#include <stdio.h>

int N = 2;
long long MODO = 1000000007;
long long ** mult(long long ** a, long long ** b)
{
    long long ** res = new long long*[N];
    for (int i=0; i<N; i++)
    {
        res[i] = new long long [N];
        for (int j=0; j<N; j++)
            res[i][j] = 0;
    }
    
    for (int i=0; i<N; i++)
        for (int j=0; j<N; j++)
        {
            long long sum = 0;
            for (int k=0; k<N; k++)
                sum += a[i][k]*1L*b[k][j];
            res[i][j] = sum%MODO;
        }
    return res;
}

long long ** pow(long long ** a, long long exp)
{
    if (exp == 1)
        return a;
    if ((exp%2) == 1)
        return mult(a, pow(a, exp-1));
    long long ** tmp = pow(a, exp/2);
    return mult(tmp, tmp);
}

long long invMod = 400000003L;
long long solve(long long * fib, long long n)
{
    n = n%MODO;
    long long A1 = ((18L*n+25L)%MODO)*fib[1];
    long long A2 = ((11L*(n+1L))%MODO)*fib[0];
    long long A3 = (A1+A2)%MODO;
    long long res = (invMod*A3)%MODO;
    return res;
}

int main()
{
    long long ** start = new long long*[N];
    for (int i=0; i<N; i++)
        start[i] = new long long[N];
    start[0][0] = 0;
    start[1][0] = start[0][1] = start[1][1] = 1;
    long long * sv = new long long[N];
    sv[0] = 0;
    sv[1] = 1;
    
    long long * rf = new long long[3];
    rf[0] = 0;
    rf[1] = 5;
    rf[2] = 18;
    
    long long* v1 = new long long[2];
    long long* v2 = new long long[2];
    
    int T = 0;
    scanf("%d", &T);
    while (T-->0)
    {
        long long k;
        scanf("%lld", &k);
        if (k < 4)
        {
            int kk = (int)(k-1);
            printf("%lld\n", rf[kk]);
        }
        else
        {
            k-=3;
            long long** cur = pow(start, k);
            v1[0] = cur[0][1];
            v1[1] = cur[1][1];
            
            v2[0] = v1[1];
            v2[1] = (v1[1]+v1[0])%MODO;
            
            long long r1 = solve(v1, k);
            long long r2 = solve(v2, k+1);
            long long res = (r1+r2)%MODO;
            
            printf("%d\n", res);
        }
    }
    
    return 0;
}

Chef World CodeChef Solution in C

#include<stdio.h>
long long int temp1[2][2]={{1,1},{1,0}},temp[2][2];
int p=1e9+7;
void mul()
{
    int i,j,k;
    long long int te[2][2];
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            te[i][j]=0;
            for(k=0;k<2;k++)
            {
                te[i][j]+=temp[i][k]*temp[k][j];
            }
        }
    }
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            temp[i][j]=te[i][j]%p;
        }
    }
}
void mul1()
{
    int i,j,k;
    long long  int te[2][2];
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            te[i][j]=0;
            for(k=0;k<2;k++)
            {
                te[i][j]+=temp[i][k]*temp1[k][j];
            }
        }
    }
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            temp[i][j]=te[i][j]%p;
        }
    }
}
void fibo(long long int N)
{
    if(N==0)
    {
      temp[0][0]=1;temp[0][1]=0;temp[1][0]=0;temp[1][1]=1;
    }
    else
    {
        if(N%2==1)
        {
            fibo(N/2);
            mul();mul1();
        }
        else
        {
            fibo(N/2);
            mul();
        }
    }

}
int main()
{
    int t;
    long long int  n,tem1,tem2,ans,x,r,q;
    scanf("%d",&t);
    x=4e8+3;
    while(t--)
    {
        scanf("%lld",&n);
        q=11*n-15;
        r=7*n-3;
        fibo(n);
        tem1=temp[0][0];
        fibo(n-1);
        tem2=temp[0][0];
        //printf("%lld %lld\n",tem1,tem2);
        ans=((q%p*tem1+r%p*tem2)%p)*x;
        ans%=p;
        printf("%lld\n",ans);
    }

return 0;
}

Chef World CodeChef Solution in JAVA

/* package codechef; // don't place package name! */



import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;

/* Name of the class has to be "Main" only if the class is public. */
class NewClass
{
    public static long [][]b;
    
    public static long [][] MatMul(long [][]a,long [][]b)
    {
        long[][] c =new long[4][4];
        for(int i=0;i<4;i++)
        {
           for(int j=0;j<4;j++)
           {
               for(int k=0;k<4;k++)
               {
                   c[i][j]=(c[i][j]+(a[i][k]*b[k][j]))%1000000007;
               }
           }
        }
        return c;
    }
    public static void MatExp(long [][]a,long n)
    {
        long [][]id={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
        
        if(n==0)  {
            b=id.clone(); 
            return ;
        }
        if(n==1) {
            b=a.clone();
            return ;
        }

        long [][]c=new long[4][4];
        long d[][]=new long[4][4];
        MatExp(a,n/2);
        c=b.clone();
        d=MatMul(c,c);
        
        if(n%2 !=0)
        {
            b=MatMul(d,a);
        }
        else
        b=d.clone();
        return ;
    }
   
	public static void main (String[] args) throws Exception
	{		
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            int t=Integer.parseInt(br.readLine());
            for(int tc=0;tc<t;tc++)
            {
                String s=br.readLine();

                long n=Long.parseLong(s);

                long [][]arr={{1,1,1,1},{1,0,0,0},{0,0,0,1},{0,0,1,1}};
                b=new long[4][4];
                if(n==1)
                System.out.println(0);
                else if(n==2)
                System.out.println(5);
                else
                {
                    MatExp(arr,n-2);
                    
                    System.out.println(((((b[0][0]*5))+((b[0][2]*5)))+((b[0][3]*8)))%1000000007);
                }
            }
	}
}

Chef World CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		var n uint64
		s, _ := reader.ReadBytes('\n')
		readUint64(s, 0, &n)
		res := solve(int64(n))
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}
	fmt.Print(buf.String())
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

const MOD = 1000000007

func modAdd(a, b int) int {
	a += b
	if a >= MOD {
		a -= MOD
	}
	return a
}

func modMul(a, b int) int {
	r := int64(a) * int64(b)
	r %= MOD
	return int(r)
}

func modSub(a, b int) int {
	return modAdd(a, MOD-b)
}

func pow(a, b int) int {
	r := 1
	for b > 0 {
		if b&1 == 1 {
			r = modMul(r, a)
		}
		a = modMul(a, a)
		b >>= 1
	}
	return r
}

var inv_5 int

var F [6]int

func init() {
	inv_5 = pow(5, MOD-2)
	F[0] = 0
	F[1] = 1
	F[2] = 1
	F[3] = 2
	F[4] = 3
	F[5] = 5
}

func solve(n int64) int {
	m := int(n % MOD)
	n += 5
	A := make([][]int, 2)
	B := make([][]int, 2)
	C := make([][]int, 2)
	for i := 0; i < 2; i++ {
		A[i] = make([]int, 2)
		B[i] = make([]int, 2)
		C[i] = make([]int, 2)
	}
	A[0][0] = 1
	A[1][1] = 1
	B[0][0] = 1
	B[0][1] = 1
	B[1][0] = 1
	for n > 0 {
		if n&1 == 1 {
			C[0][0] = A[0][0]
			C[0][1] = A[0][1]
			C[1][0] = A[1][0]
			C[1][1] = A[1][1]
			A[0][0] = 0
			A[0][1] = 0
			A[1][0] = 0
			A[1][1] = 0
			for i := 0; i < 2; i++ {
				for j := 0; j < 2; j++ {
					for k := 0; k < 2; k++ {
						A[i][j] = modAdd(A[i][j], modMul(C[i][k], B[k][j]))
					}
				}
			}
		}
		C[0][0] = B[0][0]
		C[0][1] = B[0][1]
		C[1][0] = B[1][0]
		C[1][1] = B[1][1]
		B[0][0] = 0
		B[0][1] = 0
		B[1][0] = 0
		B[1][1] = 0

		for i := 0; i < 2; i++ {
			for j := 0; j < 2; j++ {
				for k := 0; k < 2; k++ {
					B[i][j] = modAdd(B[i][j], modMul(C[i][k], C[k][j]))
				}
			}
		}
		n >>= 1
	}

	fa := A[0][0]
	fb := A[1][0]

	res := modMul(modAdd(modMul(2, m), 10), fa)
	res = modSub(res, modMul(modAdd(m, 6), fb))

	res = modMul(res, inv_5)

	for i := 0; i < 5; i++ {
		fa, fb = fb, modSub(fa, fb)
		if i < 2 {
			res = modSub(res, modMul(2, modMul(F[i], fa)))
		} else {
			res = modSub(res, modMul(F[i], fa))
		}
	}

	return res
}
Chef World CodeChef Solution Review:

In our experience, we suggest you solve this Chef World CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Chef World CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Chef World CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *