Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Chef vs Bharat CodeChef Solution

Problem -Chef vs Bharat 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 vs Bharat CodeChef Solution in C++17

#include <bits/stdc++.h>
using namespace std;

const int N=1e5+1,M=1e9+7;
long ps[N];

long mod_pow(long n, long pow, unsigned m=M) {
    long r = 1;
    for(; pow; pow >>= 1) {
        if (pow & 1) r = (r*n)%m;
        n = (n*n)%m;
    }
    return r;
}

long ext(int r) {
    long s = 0, c =1;
    for(int d = r; d; d/=10) {
        s *=10; s += d%10; c*=10;
    }
    s+= (r/10)*c;
    return s;
}

int main() {
    ps[1]=1;
    for(int i = 2; i < N;++i) {
        ps[i] = ps[i-1]+ext(i);
    }
    
	int t; scanf("%d",&t);
	while(t--){
	    int l, r; scanf("%d %d",&l,&r);
	    long al = ext(l), p = ps[r]-ps[l];
	    long ans = mod_pow(al, p);
	    printf("%ld\n",ans);
	}
	return 0;
}

Chef vs Bharat CodeChef Solution in C++14

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e9+7;

ll power(ll a, ll b){
    if(b==0) return 1;
    ll ans = power(a,b/2);
    ans = ((ans%N) * (ans%N))%N;
    if(b%2==0){
        return ans;
    }
    else{
        return (ans*a)%N;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t;
    cin>>t;
    ll chefor[100005]={0};
    ll chefora[100005]={0};

    for(ll i=1;i<100005;i++){
        ll num=i;
        ll j=i/10;
        while(j>0){
            num = num*10 + j%10;
            j/=10;
        }
        chefor[i] = num;
    }
    for(int i=1;i<100005;i++){
        chefora[i] = chefora[i-1] + chefor[i];
    }

    while(t--){
        ll l,r;
        cin>>l>>r;
        cout<<power(chefor[l], chefora[r]-chefora[l])<<endl;
    }
    
	return 0;
}

Chef vs Bharat CodeChef Solution in PYTH 3

# cook your dish here

def chefora(c):
    a=list(str(c))
    a=a+a[-2::-1]
    b=int("".join(a))
    return b

l1=[0]
for i in range(1,100001):
    l1.append(chefora(i))
    l1[i]+=l1[i-1]

for _ in range(int(input())):
    l,r=map(int, input().split())
    ans=0
    t=chefora(l)
    ans=l1[r]-l1[l]
    print(pow(t,ans,1000000007))

Chef vs Bharat CodeChef Solution in C

#include<stdio.h>
int powM(long long int x,long long int y,int m)
{
    if(y==0)
        return 1;
    if(y%2==1)
        return ((((1LL*powM(x,y/2,m)*powM(x,y/2,m))%m)*x)%m);
    else
        return ((1LL*powM(x,y/2,m)*powM(x,y/2,m))%m);
}
int main()
{
    long long arr[100002],res,num,k=1,sum=0,pre[100002],t=1,i;
    for(i=1;i<100001;i++)
    {
        num=i/10;
        res=i;
        while(num!=0)
        {
            res=res*10+num%10;
            num/=10;
        }
        arr[k++]=res;
    }
    for(i=1;i<=100000;i++)
    {
        sum+=arr[i];
        pre[t++]=sum;
    }
    int n,l,r,q,ans;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&l,&r);
        sum=pre[r]-pre[l];
        ans=powM(arr[l],sum,1000000007);
        printf("%d\n",ans);
    }
}

Chef vs Bharat CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) throws java.lang.Exception {
        // your code goes here
        long[] preComputedArray = new long[100001];
        long[] computedDifferenceArray = new long[100001];
        // System.out.println("reverse" + reverseNum(25963));
        for (int j = 1; j < preComputedArray.length; j++) {
            preComputedArray[j] = reverseNum(j);
            computedDifferenceArray[j] = preComputedArray[j] + computedDifferenceArray[j - 1];
            // System.out.println("reverseNum: " + reverseNum(j));
        }
        // System.out.println(Arrays.toString(preComputedArray));
        // System.out.println("Dffernce");
        // System.out.println(Arrays.toString(preComputedDifferenceArray));
        FastReader sc = new FastReader();
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        long mod = 1000000007;
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            long diff = computedDifferenceArray[r] - computedDifferenceArray[l];
            long ans  =  (power(preComputedArray[l], diff, mod)) % mod;
            out.write(ans + "\n");
        }
        out.flush();
    }
    // Utility function to reverse the number n
    static long reverseNum(long n) {
        long newN = n;
        long rem, rev = 0;
        n = n / 10;
        while (n > 0) {
            rem = n % 10;
            newN = newN * 10 + rem;
            n /= 10;
        }
        rev = newN;
        return rev;
    }
    // Boolean Function to check for palindromic
    // number
    static boolean isPalindrom(int num) {
        return num == reverseNum(num);
    }
    // Function for finding nth palindrome of k digits
    static int nthPalindrome(int n, int k) {
        // Get the smallest k digit number
        int num = (int) Math.pow(10, k - 1);
        while (true) {
            // check the number is palindrom or not
            if (isPalindrom(num))
                --n;
            // if n'th palindrome found break the loop
            if (n == 0)
                break;
            // Increment number for checking next palindrome
            ++num;
        }
        return num;
    }
    static int printPalindromes(int d, long[] arr, int indexTrack) {
        if (d <= 0)
            return 0;
        // Smallest and the largest d-digit numbers
        int smallest = (int) Math.pow(10, d - 1);
        int largest = (int) Math.pow(10, d) - 1;
        // Starting from the smallest d-digit
        // number till the largest
        for (int i = smallest; i <= largest; i++) {
            // If the current number
            // is palindrome
            if (isPalindrom(i)) {
                arr[indexTrack++] = i;
                // System.out.print(i + " ");
            }
        }
        return indexTrack;
    }
    static long power(long x, long y, long mod) {
        long res = 1; // Initialize result
        x = x % mod;
        if (x == 0) {
            return 0;
        }
        while (y > 0) {
            if ((y & 1) != 0)
                res = (res * x) % mod;
            y = y >> 1;
            x = (x * x) % mod;
        }
        return res%mod;
    }
}

Chef vs Bharat CodeChef Solution in PYPY 3

import math
import time
from collections import defaultdict,deque,Counter
from sys import stdin,stdout
from bisect import bisect_left,bisect_right
import sys
from heapq import heapify, heappush, heappop 
import random
def findpalin(x):
    al=str(x) 
    return int(al+al[:-1][::-1])

MOD=int(1e9 +7 )
prefix=[0]
for i in range(1,100010):
    temp=findpalin(i)
    prefix.append(temp+prefix[-1])
q=int(input())
for _ in range(q):
    l,r=map(int,stdin.readline().split())
    al=findpalin(l)
    s=prefix[r]-prefix[l]
    print(pow(al,s,MOD))
    

Chef vs Bharat CodeChef Solution in PYTH

# def printArr(arr, n):
#     for i in range(n-1):
#         print arr[i],
#     print arr[-1]
import math
palindromeNumbers = [[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1],[8,1],[9,1]]
digitCount = 3
while digitCount <= 11:
    newNums = []
    for i in range(1,10):
        for num in palindromeNumbers:
            newNum = i*pow(10,digitCount-1)+num[0]*pow(10,digitCount/2-(num[1]/2))+i
            newNums.append([newNum, digitCount])
    palindromeNumbers.extend(newNums)
    digitCount += 2

cumSum = []
currentSum = 0
for current in palindromeNumbers:
    currentSum += current[0]
    cumSum.append(currentSum)

def logPow(num, n):
    mod = 1000000007
    powArr = [1,num]
    for i in range(2,50):
        newPow = (powArr[-1]*powArr[-1])%mod
        powArr.append(newPow)
    ans = 1
    index = 1
    while n != 0:
        if n%2 == 1:
            ans *= (n%2)*powArr[index]
            ans %= mod
        index += 1
        n /= 2
    return ans

for _ in range(input()):
    l,r = map(int, raw_input().split())
    print logPow(palindromeNumbers[l][0], cumSum[r]-cumSum[l])

Chef vs Bharat CodeChef Solution in C#



using Library.CommonMath;

namespace CLown1331
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;

    /*
     *
     */

    static class Program
    {
        private const int NumberOfTestCase = 1;
        private const int StackSize = 256 * (1 << 20);
        private const int Sz = (int)2e5 + 10;
        private const int Mod = (int)1e9 + 7;

        private static void Solve()
        {
            int t = NextInt();
            long ans = 0;
            long sum = 0;

            var ar = Repeat(0, Sz).Select((v, x) =>
            {
                long ret = x;
                for (int i = x / 10; i > 0; i /= 10)
                {
                    ret = (ret * 10) + (i % 10);
                }

                return ret;
            }).ToArray();

            var br = ar.Select(c => sum += c).ToArray();
            while (t-- > 0)
            {
                int l = NextInt();
                int r = NextInt();
                sum = br[r] - br[l];
                ans = CommonMath.ModPower(ar[l], sum, Mod);
                OutputPrinter.WriteLine(ans);
            }
        }

        public static void Main(string[] args)
        {
#if CLown1331
            for (int testCase = 0; testCase < NumberOfTestCase; testCase++)
            {
                Solve();
                ErrorPrinter.WriteLine("--------");
            }

            Utils.CreateFileForSubmission();
#else
            Thread t = new Thread(Solve, StackSize);
            t.Start();
            t.Join();
#endif
            OutputPrinter.Flush();
            ErrorPrinter.Flush();
            if (Debugger.IsAttached) Thread.Sleep(Timeout.Infinite);
        }

        private static int NextInt() => int.Parse(Reader.NextString());

        private static long NextLong() => long.Parse(Reader.NextString());

        private static double NextDouble() => double.Parse(Reader.NextString());

        private static string NextString() => Reader.NextString();

        private static string NextLine() => Reader.ReadLine();

        private static IEnumerable<T> OrderByRand<T>(this IEnumerable<T> x) => x.OrderBy(_ => XorShift);

        private static long Count<T>(this IEnumerable<T> x, Func<T, bool> pred) => Enumerable.Count(x, pred);

        private static IEnumerable<T> Repeat<T>(T v, long n) => Enumerable.Repeat<T>(v, (int)n);

        private static IEnumerable<int> Range(long s, long c) => Enumerable.Range((int)s, (int)c);

        private static readonly IEnumerator<uint> Xsi = Xsc();

        private static uint XorShift
        {
            get
            {
                Xsi.MoveNext();
                return Xsi.Current;
            }
        }

        private static IEnumerator<uint> Xsc()
        {
            uint x = 123456789, y = 362436069, z = 521288629, w = (uint)(DateTime.Now.Ticks & 0xffffffff);
            while (true)
            {
                uint t = x ^ (x << 11);
                x = y;
                y = z;
                z = w;
                w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
                yield return w;
            }
        }

        private static class Reader
        {
            private static readonly Queue<string> Param = new Queue<string>();
#if CLown1331
            private static readonly StreamReader InputReader = new StreamReader("input.txt");
#else
            private static readonly StreamReader InputReader = new StreamReader(Console.OpenStandardInput());
#endif

            public static string NextString()
            {
                if (Param.Count == 0)
                {
                    foreach (string item in ReadLine().Split(' '))
                    {
                        if (string.IsNullOrWhiteSpace(item))
                        {
                            continue;
                        }

                        Param.Enqueue(item);
                    }
                }

                return Param.Dequeue();
            }

            public static string ReadLine()
            {
                return InputReader.ReadLine();
            }
        }

        private static readonly Printer OutputPrinter = new Printer(Console.OpenStandardOutput());

        private static readonly Printer ErrorPrinter = new Printer(Console.OpenStandardError());

        private sealed class Printer : StreamWriter
        {
            public Printer(Stream stream)
                : base(stream, new UTF8Encoding(false, true))
            {
                this.AutoFlush = false;
            }

            public Printer(Stream stream, Encoding encoding)
                : base(stream, encoding)
            {
                this.AutoFlush = false;
            }

            public override IFormatProvider FormatProvider => CultureInfo.InvariantCulture;
        }
    }
}


namespace Library.CommonMath
{
    public class CommonMath
    {
        public static long Gcd(long x, long y)
        {
            if (x < 0)
            {
                x = -x;
            }

            if (y < 0)
            {
                y = -y;
            }

            while (x > 0 && y > 0)
            {
                if (x > y)
                {
                    x %= y;
                }
                else
                {
                    y %= x;
                }
            }

            return x | y;
        }

        public static int Gcd(int x, int y)
        {
            if (x < 0)
            {
                x = -x;
            }

            if (y < 0)
            {
                y = -y;
            }

            while (x > 0 && y > 0)
            {
                if (x > y)
                {
                    x %= y;
                }
                else
                {
                    y %= x;
                }
            }

            return x | y;
        }

        public static long ModPower(long num, long power, long mod)
        {
            long ans = 1;
            for (; power > 0; power >>= 1)
            {
                if (power % 2 == 1)
                {
                    ans = (ans * num) % mod;
                }

                num = (num * num) % mod;
            }

            return ans;
        }
    }
}

Chef vs Bharat CodeChef Solution in GO

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func myPrintln(a ...interface{}) { _, _ = fmt.Fprintln(writer, a...) }

const MOD = 1000000007
const N = 100000

var palindromeNumbers []int64
var sumOfPalindrome = make([]int64, N)

func NumberReversed(value int64) int64 {
	ret := int64(0)
	for value > 0 {
		ret = ret*10 + value%10
		value /= 10
	}
	return ret
}

/*
  length   palindrome_numbers_count
	1               9
	3               90
	5               900
	7               9000
	9               90000
	11              900000
*/
func init() {
	powerOfTen := []int64{1, 10, 100, 1000, 10000}
	for i := 1; i <= 9; i++ {
		palindromeNumbers = append(palindromeNumbers, int64(i))
	}
	for length := 3; length <= 9; length += 2 {
		for i := powerOfTen[length/2-1]; i < powerOfTen[length/2]; i++ {
			for j := 0; j <= 9; j++ {
				palindromeNumbers = append(palindromeNumbers, (i*10+int64(j))*powerOfTen[length/2]+NumberReversed(i))
			}
		}
	}
	palindromeNumbers = append(palindromeNumbers, int64(10000000001))

	sumOfPalindrome[0] = palindromeNumbers[0]
	for i := 1; i < N; i++ {
		sumOfPalindrome[i] = sumOfPalindrome[i-1] + palindromeNumbers[i]
	}
}

func main() {
	defer func() {
		_ = writer.Flush()
	}()

	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1024), 1<<30)

	T := NextInt()
	for t := 0; t < T; t++ {
		Solve()
	}
}

func Solve() {
	l, r := NextInt(), NextInt()
	value := palindromeNumbers[l-1] % MOD
	power := sumOfPalindrome[r-1] - sumOfPalindrome[l-1]
	ans := int64(1)
	for power > 0 {
		if power&1 == 1 {
			ans = ans * value % MOD
		}
		power >>= 1
		value = value * value % MOD
	}
	myPrintln(ans)
}

func NextInt() int {
	scanner.Scan()
	ret, _ := strconv.Atoi(scanner.Text())
	return ret
}
Chef vs Bharat CodeChef Solution Review:

In our experience, we suggest you solve this Chef vs Bharat 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 vs Bharat CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Chef vs Bharat 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 *