Cracking the Code CodeChef Solution

Problem -Cracking the Code 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.

Cracking the Code CodeChef Solution in C++14

#include <bits/stdc++.h>
#define pb push_back
#define sz(a) (int)a.size()
#define x 1.0*sqrt(2)
#define y 1.0*sqrt(3)
using namespace std;
typedef long long ll;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    ll i , k , s; cin >> i >> k >> s;
    double ai , bi; cin >> ai >> bi;
    if((i&1) != (k&1)){
        double s = ai+bi;
        double r = ai-bi;
        ai = x*s - x*y*r;
        bi = x*r + x*y*s;
        i++;
    }
    cout << fixed << setprecision(2);
    cout << 1.0 * (ai+bi) * pow(2 , 2*(k-i) - s) << endl;
    return 0;
}

Cracking the Code CodeChef Solution in PYTH 3

# cook your dish here
import math

def main():
  #print("enter i, k, s")
  IN = '11 6 5'
  z = IN.split()
  z = input().split()
  i = int(z[0])
  k = int(z[1])
  s = int(z[2])

  #print("enter a_i and b_i")
  IN = '4 5'
  z = IN.split()
  z = input().split()
  a_i = int(z[0])
  b_i = int(z[1])

  #print ( "i = %d   k = %d   s = %d " % (i, k, s)  )
  #print ( "a_i = %d   b_i = %d" % (a_i, b_i)  )

  x = math.sqrt(2)
  y = math.sqrt(3)
  #print(x,y)

  # Obtaining the k-th element when k >= i
  if(i<=k):
    diff = k-i
    #if both k and i are odd or even
    if(k-i)%2==0:
      #print("#1")
      ans =  (a_i + b_i) * math.pow(2,2*(k-i)-s)
      #diff = int(diff/2) 
      #ans =  (a_i + b_i) * math.pow(2,4*diff-s)
          
    #if i and k are of different parities then obtaining first
    # a_(i+1) and b_(i+1)
    else:
      #print("#2")
      ans = (2*x*a_i + 2*x*y*b_i) * math.pow(2,2*(k-(i+1))-s )
      diff = int(diff/2)
      ans = (2*x*a_i + 2*x*y*b_i) * math.pow(2,4*diff - s)
      #print("1: ", (2*x*a_i + 2*x*y*b_i))
      #print("2: ", math.pow(2,4*diff - 2- s))  
      #print("2 sol: ", math.pow(2,4*int(diff)-s))
      #print("diff: ",diff)


  # Obtaining the k_th element when k < i
  else:
    diff = i-k
    #if both k and i are odd or even
    if(i-k)%2==0:
      #print("#3")
      ans =  (a_i + b_i) / math.pow(2,2*(i-k)+s)
      #diff =  int(diff/2)
      #ans =  (a_i + b_i) / math.pow(2,4*diff+s)

    #if i and k are of different parities then obtaining first
    # a_(i+1) and b_(i+1)
    else:
      #print("#4")
      ans = (2*x*a_i + 2*x*y*b_i) / math.pow(2,2*(i+1-k)+s)
      diff = int(diff/2)
      ans = (2*x*a_i + 2*x*y*b_i) / math.pow(2,4*diff + 4 + s)


  print(ans)
  
main()

Cracking the Code CodeChef Solution in C

#include <stdio.h>
#include <math.h>
int main()
{
	long long int i,k,s;
	double ans,a_i,b_i;
	scanf("%lld%lld%lld",&i,&k,&s);
	scanf("%lf%lf",&a_i,&b_i);
	if((k-i)%2==0)
	{
		ans=a_i+b_i;
		printf("%lf\n",ans*pow(2.0,(2*(k-i))-s));
	}
	else
	{
		ans = sqrt(2)*pow(2.0,(2*(k-i)-1-s))*(a_i+(sqrt(3)*b_i));
		printf("%lf\n",ans);
	}
	return 0;
}

Cracking the Code CodeChef Solution in JAVA

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

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
	class Main
		{  
		static class Chef implements Comparable<Chef> {
	        int age;
	        int rating;
	        Chef(int age,int rating) {
	            this.age = age;
	            this.rating = rating;
	        }
	        @Override
	        public int compareTo(Chef p) {
	            return this.age-p.age;
	        }
	    }
		/*
		class Pair
		{
		  int f,s;
		  Pair( int f, int s)
		  {
		    this.f=f;
		    this.s=s;
		  }
		}*/
		static class Dish{

		    int id;
		    int time;

		    Dish(int a, int b){
		        this.id = a;
		        this.time = b;
		    }
		}

		static class compare implements Comparator<Dish>{
		    public int compare(Dish a, Dish b){
		        if(a.time!=b.time)return a.time - b.time;
		        else return a.id - b.id;
		    }
		}
		
		 static class Pair {
		        char prefix;
		        int suffixId;
		        Pair(char prefix, int suffixId) {
		            this.prefix = prefix;
		            this.suffixId = suffixId;
		        }

		        @Override
		        public int hashCode() {
		            final int prime = 199;
		            int hash = 0;
		            hash = prime * prefix + hash;
		            hash = prime * suffixId + hash;
		            return hash;
		        }

		        @Override
		        public boolean equals(Object obj) {
		            if(obj instanceof Pair) {
		                Pair p2 = (Pair) obj;
		                return this.prefix == p2.prefix && this.suffixId == p2.suffixId;
		            }
		            return false;
		        }
		    }
		 static class Circle{
	         long x;
	         long y;
	         long r;

	        public Circle(long x, long y, long r) {
	            this.x = x;
	            this.y = y;
	            this.r = r;
	        }
	     }
	     
	     
	     
	     
	     
	     static class Distance{
	         long shortest;
	         long largest;
	         
	         
	     }
		/*static class pair implements Comparable<pair>
		 {
		     long x;
		     int y;
		     pair(long x,int y)
		     {
		         this.x=x;
		         this.y=y;
		     }
		     public int compareTo(pair o)
		     {
		         return (int)(x-o.x);
		     }

		 }
	static	class Pair
		{
			long x,y;
			Pair(long x, long y)
			{
				this.x = x;
				this.y = y;
			}
		}
		static class PairComparator implements Comparator<Pair>
		{
			public long compare(Pair a, Pair b)
			{
				//if(a.x==b.x)
					return a.y-b.y;
			//	return a.x-b.x;
			}
		}
		static class Point {
			public double x, y;
		 
			private static final int MAX = (int) 1e6 + 1;
		 
			public Point(double x, double y) {
				this.x = x;
				this.y = y;
			}
		 
			public int hashCode() {
				return (int)x * MAX + (int)y;
			}
		 
			public boolean equals(Object ob) {
				if(ob instanceof Point) {
					Point other = (Point) ob;
					return other.x == x && other.y == y;
				}
				
				return false;
			}
		 
			public String toString() {
				return "(" + x + ", " + y + ")";
			}
		}*/
	/*	static int days4=0;
		static int all;
		static long phi[]=new long[1000001];
     	static long ans[]=new long[1000001];
		//static int tree[],sum[];
		//public static long mod = 1000000007;	public static long [][] tempMatrix;
		public static long max = (long) Math.pow(10, 9) + 7;
		static StringBuilder res = new StringBuilder();
		//static Node tree[];
		//static int a[];
	//	static long mod = 998244353;
		 public static int rootColor=0;
		 static int MX = (1<<18);
	//	 static boolean primes[]=new boolean[10000001];*/
		 
		static double pi = 3.1415926535; 
		 private static final int MAXN = 5000;
		/*    private static final int MOD = 1000_000_007;
		    private static Modular modular_nCr = new Modular(MOD);
		    private static Modular modular = new Modular(MOD);
		    private static final long[][] nCr = new long[MAXN+5][MAXN+5];
		    static int[] maxval = new int[1000000];
		    static int[] minval = new int[1000000];
		    private static long bruteAns = 1;
		    static double eps = 1e-7;
		    static {
		    	nCr[0][0]=1;
				for(int i=0;i<=MAXN;i++)
					nCr[i][0]=1;
				for(int i=1;i<=MAXN;i++){
					for(int j=1;j<=i;j++){
						nCr[i][j]=(nCr[i-1][j-1]+nCr[i-1][j])%MOD;
					}
				}
			}*/
/*
		    static { nCr[0][0] = 1;
		    for (int i = 1; i < MAXN; i++) {
		        nCr[i][0] = 1;
		        for (int j = 1; j <= i; j++) {
		            nCr[i][j] = modular_nCr.add(nCr[i - 1][j - 1], nCr[i - 1][j]);
		        } }
			}
			
		    static int arr[] , dp[][][];
	//		static int n ;
		    static int final_sum=0;
		    static int mod=1000000007;
		    static ArrayList<Integer> graph[];
		    static boolean vis[];
		    static int seive[]=new int[1000001];
		    static int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
		    static int[] calcPower() {
		        int[] arr = new int[26];
		        for (int i = 0; i < 26; i++) {
		            arr[i] = (int) Math.pow(2, i);
		        }
		        return arr;
		    }
		   // static int p[] = new int[1000001], size[] = new int[1000001], n, m;
		    static int bits=32;
		    static class Trie
		    {
		        Trie tree[]=new Trie[2];
		        int val=0;
		        public Trie()
		        {
		            val=0;
		            tree[0]=null;tree[1]=null;
		        }
		    }
		    static Trie root;
		    static void insert(int xor)
		    {
		        Trie temp=root;
		        for(int i=bits-1;i>=0;i--)
		        {
		            int v = (xor & (1<<i)) >=1 ? 1 : 0; 
		            if(temp.tree[v]==null)
		                temp.tree[v]=new Trie();
		            temp=temp.tree[v];
		        }
		        temp.val=xor;
		    }
		    static int query(int xor)
		    {
		        Trie temp=root;
		        for(int i=bits-1;i>=0;i--)
		        {
		            int v = (xor & (1<<i)) >=1 ? 1 : 0; 
		            if(temp.tree[1-v]!=null)
		                temp=temp.tree[1-v];
		            else if(temp.tree[v]!=null)
		                temp=temp.tree[v];
		        }
		        return xor^temp.val;
		    }
		    public static int getBit(int n, int pos) {
				return (n & (1 << pos)) == 0 ? 0 : 1;
			}
		    private static int n, m, ci, cj;
		    private static char[][] g;
		    private static boolean[][] vs;
		    private static int[] v = new int[]{-1, 0, 1, 0};
		    private static int[] h = new int[]{0, 1, 0, -1};
		    private static char[] d = new char[]{'U', 'R', 'D', 'L'};

		    private static boolean v(int i, int j) {
		        return (i >= 0 && i < n && j >= 0 && j < m);
		    }

		    private static boolean pr(int i, int j) {
		        return g[i][j] == '*';
		    }
		    private static double dh, dl, dr, k;
		    static int zero = Integer.MAX_VALUE, one = Integer.MAX_VALUE;
		    static ArrayList<Long> leaf=new ArrayList<>();*/
	        static int mod=1000000007;
		    static int MX = 1000001;
			static int pr[] = new int[MX];
			static boolean cmp[] = new boolean[MX];
			static int c = 0;
			static void pre(){
				for(int x = 2;x<MX;x++){
					if(!cmp[x]){
						pr[c++] = x;
						for(int y = x+x; y<MX; y+=x){
							cmp[y] = true;
						}
					}
				}
			}
		   
	//		static final int MAXN = (int) 1e6;
		    static int spf[] = new int[MAXN];
		    static ArrayList<Integer> primes = new ArrayList<>();
		static   final int size = 1000001;
	    static	final long MOD = 1000000007L;
		static	int n;
		static	int m;
		static	int a[] = new int[size];
		static	int dp[] = new int[size];
	//	static	int arr[] = new int[size];
		static	int stops;
		static int[] arr = new int[] {2,3,5,7,11,13,17,19,23,29};
		public static List<Integer> g[];
		public static long[] subLeaf;
		public static long[] count;
		
		public static void dfs(int node,int parent){
		    if(g[node].size()==1){
		        subLeaf[node]=1;
		        return;
		    }
		    long paths=0;
		    for(Integer i : g[node]){
		        if(i==parent){
		            continue;
		        }
		        dfs(i,node);
		        subLeaf[node]+=subLeaf[i];
		        count[node]+=(paths*subLeaf[i]);
		        paths+=subLeaf[i];
		    }
		}
		    
			public static void main (String[] args) throws java.lang.Exception
			{  
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//	FastReader in = new FastReader(System.in);
			StringBuilder sb = new StringBuilder();
	        FastScanner in=new FastScanner();
		//	Scanner sc = new Scanner(System.in);
			PrintWriter out=new PrintWriter(System.out);
			
			HashMap<Character, Integer> h = new HashMap<Character, Integer>();
		//	TreeMap<Integer, Integer> h1 = new TreeMap<Integer, Integer>();
		//	HashMap<Integer, Integer> h2 = new HashMap<Integer, Integer>();
		//	HashSet<Point> s = new HashSet<Point>();
			
		//	HashSet<Double> s2 = new HashSet<Double>();
		//	HashSet<Double> s3 = new HashSet<Double>();
			//	HashSet<Character> h2 = new HashSet<Character>();
			//long t= in.nextLong();
			//	long t = in.nextLong();
			//DecimalFormat df = new DecimalFormat("#,###,##0.000000");
			
		/* boolean prime[]=new boolean[10000000];
			   int p[]=new int[10000000];
			    int k=1;
			    Arrays.fill(prime, true);
			    prime[0]=false;
			    prime[1]=false;for(int i=2;i<10000000;i++)
			    {
			        if(!prime[i])
			        {
			        	p[i]=p[i-1];
			            continue;
			        }
			        p[i]=k; k++;
	        for(long j=(long)i*i;j<10000000;j+=i)
            prime[(int)j]=false;
			    }
		//	 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		     /*  int pri[]=new int[1000005];
		        int p=2;
		        List<Integer> list=new ArrayList<>();
		        while(p*p <=1000005)
		        {
		            if(pri[p]==0)
		            {
		                for(int i=p*2;i<=1000004;i=i+p)
		                {
		                    pri[i]=1;
		                }
		                list.add(p);
		            }
		            p++;
		        }
		        //System.out.println(list);*/
	//		int t = in.nextInt();
	//		while(t-->0)
	//		{
				long i,k,s;
				i = in.nextLong();
		        k = in.nextLong();
		        s = in.nextLong();

		        int ai, bi;
		        ai = in.nextInt();
		        bi = in.nextInt();

		        double a = ai, b = bi;
		        if ((k % 2 == 0 && i % 2 != 0) || (k % 2 != 0 && i % 2 == 0)) {
		            i++;
		            a = Math.sqrt(2.0) * ((ai + bi) - Math.sqrt(3.0) * (ai - bi));
		            b = Math.sqrt(2.0) * ((ai - bi) + Math.sqrt(3.0) * (ai + bi));
		            
		        }

		        double result = (a + b) * Math.pow(2.0, 2 * (k - i) - s);
		        out.println(result);
	//		}
		   out.close();
		}
			
			static boolean isSquare(int n)
		    {
		        if(Math.ceil((double)Math.sqrt(n))==Math.floor((double)Math.sqrt(n)))
		            return true;
		        return false;
		    }
		
			public static long gcd(long a,long b,long n){
			    if(a==b){
			        return (power(a,n,mod)+power(b,n,mod))%mod;
			    }
			    long res=1l;
			    long num=a-b;
			    for(long i=1;i*i<=num;i++){
			        if(num%i==0){
			            long temp= (power(a,n,i)+ power(b,n,i))%i;
			            if(temp==0){
			                res=Math.max(res,i);
			            }
			            temp= (power(a,n,num/i) + power(b,n,num/i))%(num/i);
			             if(temp==0){
			                res=Math.max(res,num/i);
			            }
			        }
			    }
			    return res%mod;
			}
			public static long power(long a,long n,long d){
			    long res=1l;
			    while(n>0){
			        if(n%2==1){
			            res =((res%d)*(a%d))%d;
			            n--;
			        }else{
			            a=((a%d)*(a%d))%d;
			            n=n/2;
			        }
				   }
				   return res%mod;
			}
/*	static long power(long x,long y) { 
        long res=1;  
        x%=m;  
        while(y>0) { 
            if(y%2!=0) 
                res=(res*x)%m; 
            y=y>>1;
            x=(x*x)%m; 
        } 
        return res; 
    } 			
*/
	    
static long gcd(long a, long b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
static long  nextPower_2 (  long x, long y )
{

long  count  =  0 ;
while ( y < x )
{count ++ ;
y  =  y <<  1 ;
}
return  count ;
}
/*
static long power(long a, long b, long p) 
{ 	long x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) {
x = (x * y);
if (x >= p) x %= p;
}
y = (y * y); if (y >= p) y %= p;
b /= 2;}
return x;
}*/
public static class Modular {

private int MOD;

Modular(int MOD) {
   this.MOD = MOD;
}

public long add(long a, long b) {
   return (a + b) % MOD;
}

public long sub(long a, long b) {
   return (a - b + MOD) % MOD;
}

public long mul(long a, long b) {
   return (a * b) % MOD;
}

public long div(long a, long b) {
   return mul(a, inverseEuler(b));
}	public long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
    if (b % 2 == 1) {
        x = (x * y);if (x >= MOD) x %= MOD;
    }
    y = (y * y);
    if (y >= MOD) y %= MOD;
    b /= 2;}
return x;
}

public long inverseEuler(long n) {
return power(n, MOD - 2);
}
}
	static class FastReader {
byte[] buf = new byte[2048];
int index, total;
InputStream in;FastReader(InputStream is) {
in = is;
}	int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) { return -1;
}
}
return buf[index++];
}

String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) {
 sb.append((char) c);}
return sb.toString();
}String nextLine() throws IOException {
int c;for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c != 10 && c != 13; c = scan()) {
    sb.append((char) c);
} return sb.toString();
}char nextChar() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan()) ;
return (char) c;
}	int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
    c = scan(); }
for (; c >= '0' && c <= '9'; c = scan()) {
    val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}long nextLong() throws IOException {
int c;long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+') {
    c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
    val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}

static class FastScanner{
	BufferedReader br;
	StringTokenizer st;
	public FastScanner(){br=new BufferedReader(new InputStreamReader(System.in));}
	String nextToken(){
		while(st==null||!st.hasMoreElements())
			try{st=new StringTokenizer(br.readLine());}catch(Exception e){}
		return st.nextToken();
	}
	int nextInt(){return Integer.parseInt(nextToken());}
	long nextLong(){return Long.parseLong(nextToken());}
	double nextDouble(){return Double.parseDouble(nextToken());}
}
}

Cracking the Code CodeChef Solution in PYTH

import math
c1=math.sqrt(2)-math.sqrt(6)
c2=math.sqrt(2)+math.sqrt(6)
z=raw_input().split()
i=int(z[0])
k=int(z[1])
s=int(z[2])
z=raw_input().split()
a_i=int(z[0])
b_i=int(z[1])
if(i<=k):
    diff=k-i
    if(diff%2==0):
        diff=diff/2
        ans=(a_i+b_i)*math.pow(2,4*diff-s)
    else:
        diff=diff/2
        ans=(c1*(a_i-b_i)+c2*(a_i+b_i))*math.pow(2,4*diff-s)
else:
    diff=i-k
    if(diff%2==0):
        diff=diff/2
        ans=(a_i+b_i)/math.pow(2,4*diff+s)
    else:
        diff=diff/2
        ans=(c1*(a_i-b_i)+c2*(a_i+b_i))/math.pow(2,4*diff+4+s)
print ans

Cracking the Code CodeChef Solution in C#

using System;

namespace CodeChefPractice
{
    // Prepared as a solution to Chef and Cracking the Code
    // David Sturge (David_S)  31 May 2018.

    class CrackingCode
    {
        public class Test
        {
            public static void Main()
            {
                // Read I, K, S
                int[] data1 = DecodeString(3, Console.ReadLine());
                int i = data1[0];
                int k = data1[1];
                int s = data1[2];
                // Read a_i, b_i
                int[] data2 = DecodeString(2, Console.ReadLine());
                int a_i = data2[0];
                int b_i = data2[1];

                // The recurrence relations simplify to 
                // a[n+2] = 16 * a[n]  and b[n+2] = 16 * b[n]
                // We need to be careful to avoid underflow or overflow,
                // by incorporating 2^s into 'inv_power_of_2'.
                int inv_power_of_2 = s;
                if (k > i) {
                    inv_power_of_2 -= 4 * ( ( k - i ) / 2 );
                } else if (k < i) {
                    inv_power_of_2 += 4 * ( ( i - k + 1 ) / 2 );
                }
                double fac = Math.Pow(0.5, inv_power_of_2);
                double a_k = fac * a_i;
                double b_k = fac * b_i;

                if (( k - i ) % 2 != 0) {
                    // Extra evaluation for odd one
                    double sum = a_k + b_k;
                    double dif = a_k - b_k;
                    double x = Math.Sqrt(2.0);
                    double xy = Math.Sqrt(6.0);
                    a_k = x * sum - xy * dif;
                    b_k = x * dif + xy * sum;
                }
                // The result (factor 2^s incorporated already)
                double q = a_k + b_k;
                Console.WriteLine(q);

                Console.ReadKey();
            }

            private static int[] DecodeString(int num, String s)
            // Decode num integers from line of input
            {
                int[] output = new int[num];
                int start_i = 0;
                int num_read = 0;
                int len = s.Length;
                while (num_read < num && start_i < len) {
                    int next_i;
                    output[num_read++] = StringToInt(s, start_i, out next_i);
                    start_i = next_i + 1;
                }
                return output;
            }

            private static int StringToInt(String s, int index, out int space_i)
            // Convert string from index up to next space or end to integer.
            // Return space_i = index of next space encountered, or off end of string.
            {
                bool minus = s[index] == '-';
                if (minus)
                    ++index;
                const char zero = '0';
                int n = s[index] - zero;
                int len = s.Length;
                while (++index < len) {
                    if (s[index] < zero)
                        break;
                    n = n * 10 + ( s[index] - zero );
                }
                space_i = index;
                return (minus) ? -n : n;
            }
        }
    }
}
Cracking the Code CodeChef Solution Review:

In our experience, we suggest you solve this Cracking the Code 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 Cracking the Code CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Cracking the Code 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 *