Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Bob and Pallindromes CodeChef Solution

Problem -Bob and Pallindromes 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.

Bob and Pallindromes CodeChef Solution in C++14

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

long long power(long long a,int n){
  long long ans = 1;
  
  while (n) {
    if (n & 1) ans = ((ans % mod) * (a % mod)) % mod;
    a = ((a % mod) * (a % mod)) % mod;
    n >>= 1;
  }
  
  return ans;
}

int solve(int n){
  int ans=1;
  long long x=power(26,mod-2);
  long long y=(2*power(25,mod-2))%mod;
  y=(y*(1+mod-power(x,n/2)))%mod;
  ans=(ans+y)%mod;
  if(n%2==0){
      ans=(ans+mod-power(x,n/2))%mod;
  }
  return ans;
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  
  int t, n;
  
  cin >> t;
  
  while (t--) {
    cin >> n;
    cout << solve(n) << '\n';
  }
  
  return 0;
}

Bob and Pallindromes CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here
import sys

# stdin = open("testdata.txt", "r")
ip = sys.stdin

mod = pow(10, 9) + 7

modinv = lambda x: pow(x, mod-2, mod)

t = int(ip.readline())

def solve(n):
	ans = 1
	a = pow(26, n, mod)
	d = a 
	a = (a-1)%mod
	a = (a*2)%mod
	mmi1 = modinv(d)
	mmi2 = modinv(25)
	a = (a*mmi1)%mod 
	a = (a*mmi2)%mod 
	ans = (a+ ans)%mod
	return ans

for _ in range(t):
	n = int(ip.readline())
	res = solve(n//2)
	if n%2 == 0:
		last = modinv(pow(26, n//2, mod))
		res = (res - last)%mod
	print(res)

Bob and Pallindromes CodeChef Solution in C

#include <stdio.h>
int mod=(int)1e9+7;
int powmod(int a,int n)
{
    int ans=1;
    while(n)
    {
        if(n&1) ans=((long long)ans*a)%mod;
        a=((long long)a*a)%mod;
        n>>=1;
    }
    return ans;
}
int main()
{
    int i,T,N;
    scanf("%d",&T);
    for(i=1;i<=T;i++)
    {
        scanf("%d",&N);
        int num,denom;
        if(N&1)
        {
            int temp=powmod(26,(N-1)/2);
            denom=((long long)25*temp)%mod;
            num=((long long)27*temp)%mod;
            num-=2;
            if(num<0) num+=mod;
        }
        else
        {
            int temp=powmod(26,N/2);
            denom=((long long)25*temp)%mod;
            temp--;
            if(temp==-1) temp=mod-1;
            num=((long long)27*temp)%mod;
        }
        printf("%d\n",((long long)num*powmod(denom,mod-2))%mod);
    }
    return 0;
} 

Bob and Pallindromes CodeChef Solution in JAVA

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

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

/* Name of the class has to be "Main" only if the class is public. */
/*
Gp formula = 1+2[((1/26)-((1/26^floor(n/2))*(1/26)))/(1-(1/26))] ..... for odd
             1+2[((1/26)-((1/26^floor(n/2))*(1/26)))/(1-(1/26))]-(1/26^(n/2)) ..... for Even
*/
class Codechef
{
    final static long mod = 1000000007;
	public static long power(long number, long pow) {
		if(pow==1) {
			return number%mod;
		}
		if(pow==2) {
			return (number * number)%mod;
		}
		long result = power(number,pow/2);
		result = (result * result)%mod;
		if((pow&1)==1) {
			result = (result* number)%mod;
		}
		return result;
	}
	public static long solution(int n) {
		
		long r = power(26, mod-2)%mod;
		
		long numeratorTemp = power(26, (long)Math.floor(n/2));
		
		numeratorTemp = power(numeratorTemp, mod-2);
		
		numeratorTemp = (numeratorTemp * r)%mod;
		
		long numerato = ((r - numeratorTemp)+mod)%mod;
		
		long denomenator = ((1 - r)+mod)%mod;
		
		denomenator = power(denomenator, mod-2);
		
		long output = (numerato * denomenator)%mod;
		
		long result1 = (1 + 2 * output)%mod;
		
		if(n%2==0) {
			
			long temp = power(26, n/2);
			temp = power(temp, mod-2);
			long result2 = ((result1 - temp)+mod)%mod;
			return result2;
		}else {
			return result1;
		}
	}
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-->0) {
			int n = sc.nextInt();
			if(n==1) {
				System.out.println(1);;
			}else {
				System.out.println(solution(n));
			}
		}
	}
}

Bob and Pallindromes CodeChef Solution in PYTH

mod = 10 ** 9 + 7
inv = lambda x : pow(x, mod-2, mod)
for _ in xrange(int(raw_input())):
    n = int(raw_input())
    if n == 1 :
        print 1 
    else:
        s = 52 * (pow(26, n//2+1, mod) - 1) * inv(pow(26, n//2+1, mod)) * inv(25) - 1
        if n % 2  == 0:
            s -= inv(pow(26, n//2, mod)) 
        s %= mod 
        print (s)
            
    

Bob and Pallindromes 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--
		n := readNum(reader)
		res := solve(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' {
			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 pow(a, b int) int64 {
	A := int64(a)
	R := int64(1)
	for b > 0 {
		if b&1 == 1 {
			R = (R * A) % MOD
		}
		A = (A * A) % MOD
		b >>= 1
	}
	return R
}

func solve(n int) int {
	var f int
	if n&1 == 0 {
		f = 1
	}
	last := pow(26, n/2)
	last = pow(int(last), MOD-2)

	n -= f

	a := pow(26, n/2)
	d := a
	a = (a - 1 + MOD) % MOD
	a = (a * 2) % MOD
	mmi1 := pow(int(d), MOD-2)
	mmi2 := pow(25, MOD-2)
	a = (a * mmi1) % MOD
	a = (a * mmi2) % MOD
	ans := (a + 1) % MOD
	if f == 1 {
		ans = (ans + last) % MOD
	}
	return int(ans)
}
Bob and Pallindromes CodeChef Solution Review:

In our experience, we suggest you solve this Bob and Pallindromes 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 Bob and Pallindromes CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Bob and Pallindromes 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 *