Misinterpretation CodeChef Solution

Problem -Misinterpretation 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.

Misinterpretation CodeChef Solution in C++14

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

const int M = 1e9+7;

long long binpow(long long  a, long long b, long long m) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res =( res * a) % m;
        a = (a * a )% m;
        b >>= 1;
    }
    return res;
}

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    vector<bool> check(n+1,true);
	    int count = 0;
	    for (int i = 1 ; 2*i <= n; i++){
	        if (check[i]) {
	            count++;
	            int j = 2*i;
	            while(j!= i){
	                if (j<=n/2){
	                    check[j] = false;
	                    j = j*2;
	                }
	                else{
	                    check[j] = false;
	                    j = (j-n/2)*2 -1;
	                }
	            }
	        }
	    }
	    if (n &1 != 0) count++;
	    cout<<binpow(26*1ll,count,M)<<endl;

	}
	return 0;
}

Misinterpretation CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here
from sys import *
import math

def solve():
    mod=1000000007
    n = int(stdin.readline().rstrip())
    visited = [False] * (n+1)
    start = 1
    count = 0 
    res1 = [2*x for x in range(n//2 +1)]
    res2 = [2*x+1 for x in range(n - n//2)]
    res=res1+res2
    for i in range(1,n+1):
        if not visited[i]:
            temp = i 
            while not visited[temp]:
                visited[temp] = True 
                temp = res[temp]
            count += 1 
    print(pow(26,count,mod))
for _ in range(int(stdin.readline().rstrip())):
    solve()

Misinterpretation CodeChef Solution in C

#include<stdio.h>

int m=1000000007;

int main()
{
    int t,tt,n;
    scanf("%d",&t);
    for(tt=0;tt<t;tt++)
    {
        scanf("%d",&n);
        int i,j;
        int p[n+1];
        int comp[n+1];
        p[0]=comp[0]=-1;
        for(i=2,j=1;i<=n;i+=2,j++)
            {
                p[j]=i;
                comp[j]=0;
            }
        for(i=1;j<=n;i+=2,j++)
            {
                p[j]=i;
                comp[j]=0;
            }
        /*printf("comp is ");
        for(i=1;i<=n;i++)
            printf("%d ",comp[i]);
        printf("\n");*/
        int num=0;
        for(i=1;i<=n;i++)
        {
            if(comp[i]==0)
            {
                j=i;
                num++;
                while(comp[j]==0)
                {
                    comp[j]=num;
                    j=p[j];
                }
            }
        }
        long long int ans=1;
        for(i=1;i<=num;i++)
            ans=(ans*26)%m;
        printf("%lld\n",ans);
    }
}

Misinterpretation CodeChef Solution in JAVA

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

class misinter {
    public static void main(String[] args) throws IOException {
        FastReader sc = new FastReader();
        PrintWriter pw=new PrintWriter(System.out);
        int t=sc.nextInt();
        for(int z=1;z<=t;z++) {
            int n = sc.nextInt();
            int arr[]=new int[n+1];
            int orig[]=new int[n+1];
            for(int i=1;i<=n/2;i++){
                orig[i]=i;
                arr[i]=i*2;
            }
            for(int i=n/2 + 1;i<=n;i++){
                arr[i]=(i - n/2)*2 - 1;
                orig[i]=i;
            }
            boolean vis[]=new boolean[n+1];
            long mod=1000000007,res=1;
            for(int i=1;i<=n;i++){
                if(!vis[i]){
                    res=(res*26)%mod;
                    int start=arr[orig[i]];
                    while(!vis[start]){
                        vis[start]=true;
                        start=arr[start];
                    }
                }
            }
            pw.println(res);
        }
        pw.close();
    }
    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;
        }
    }
}

Misinterpretation CodeChef Solution in PYPY 3

from sys import *

def solve():
    mod=1000000007
    n = int(stdin.readline().rstrip())
    visited = [False] * (n+1)
    count = 0 
    res1 = [2*x for x in range(n//2 +1)]
    res2 = [2*x+1 for x in range(n - n//2)]
    res=res1+res2
    for i in range(1,n+1):
        if not visited[i]:
            temp = i 
            while not visited[temp]:
                visited[temp] = True 
                temp = res[temp]
            count += 1
    print(pow(26,count,mod))
for _ in range(int(stdin.readline().rstrip())):
    solve()

Misinterpretation CodeChef Solution in PYTH

"""
Misinterpretation

Link:
https://www.codechef.com/problems/MISINTER
"""


"""
REFERENCES

Disjoint Set Union (Union Find) | HackerEarth

https://www.hackerearth.com/practice/notes/disjoint-set-union-union-find/
"""


"""
SUBMISSIONS

### Solution 1

Submission rejected on 2019-11-10 20:50:55 PST. Got "Time Limit Exceeded".
ID: 27862287
Link: https://www.codechef.com/viewsolution/27862287
Time: 1.54 sec
Memory: 25.9M
"""


## -----------------------------------------------------------------------------


class Solution_1(object):
    """My Solution 1

    Submission rejected on 2019-11-10 20:50:55 PST. Got "Time Limit Exceeded".
    ID: 27862287
    Link: https://www.codechef.com/viewsolution/27862287
    Time: 1.54 sec
    Memory: 25.9M
    """

    def solve(self, N):
        vec = [i for i in xrange(0, N+2)]

        def root(i):
            """Find root of union."""
            while vec[i] != i:
                i = vec[i]
            return i

        def union(a, b):
            """Union make."""
            vec[root(b)] = root(a)

        ##
        # Union the N/2 even ones with the odd ones.
        for i in xrange(1, N/2+1):
            union(i, i*2)

        ##
        # Union the rest with the N/2 odd ones,
        # starting from 1 and goes like 1, 3, 5, ... etc.
        j = 1
        for i in xrange(N/2+1, N+1):
            union(i, j)
            j += 2

        ##
        # Find the number of disjoint sets.
        s = set((root(i) for i in xrange(1, N+1)))

        ##
        # The result is 26 to power of (number of distinct disjoint unions),
        # modulo 1000000007.
        res = 1
        for _ in xrange(len(s)):
            res = (res * 26) % 1000000007
        return res


## -----------------------------------------------------------------------------


class Solution_2(object):
    """My Solution 2"""

    def solve(self, N):
        ##
        # Initialize.
        vec = [i for i in xrange(0, N+2)]

        ##
        # Permute array like described.
        j = 0
        for i in xrange(2, N+1, 2):
            j += 1
            vec[j] = i
        for i in xrange(1, N+1, 2):
            j += 1
            vec[j]=i

        ##
        # Count number of cycles.
        marked = [0] * (N+2)
        cycles = 0
        for i in xrange(1, N+1):
            if not marked[i]:
                cycles += 1
            j = i
            while not marked[j]:
                marked[j] = 1
                j = vec[j]

        ##
        # The result is 26 to power of (number of distinct disjoint unions),
        # modulo 1000000007.
        res = 1
        for _ in xrange(cycles):
            res = (res * 26) % 1000000007
        return res


## -----------------------------------------------------------------------------


# Solution = Solution_1
Solution = Solution_2


class Driver(object):

    def run(self):
        T = int(raw_input())
        for _ in xrange(T):
            N = int(raw_input())
            assert 1 <= N <= 100000
            soln = Solution()
            res = soln.solve(N)
            print res


def main():
    driver = Driver()
    driver.run()


if __name__ == '__main__':
    main()

Misinterpretation CodeChef Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgrammingContest.CodeChef.Contest.July_Cook_off_2011
{
    class Misinterpretation
    {
        static void Main()
        {
            int T = int.Parse(Console.ReadLine());
            while (T-- > 0)
            {
                int N = int.Parse(Console.ReadLine());
                UnionFind uf = new UnionFind(N + 1);

                for (int i = 2; i <= N; i += 2)
                    uf.UnionSet(i, i / 2);

                int cnt = (N+1) / 2;
                for (int i = N / 2 + 1, j = 1; i < N; i++, j += 2)
                    if (!uf.FindSet(i, j))
                    {
                        uf.UnionSet(i, j);
                    }

                bool[] used = new bool[N + 1];
                long res = 1;
                for (int i = 1; i <= N; i++)
                {
                    int r = uf.Root(i);
                    if (!used[r])
                    {
                        res = res * 26 % 1000000007;
                        used[r] = true;
                    }
                }
                Console.WriteLine(res);
            }
        }
        
        #region UnionFind
        class UnionFind
        {
            int[] data;

            public UnionFind(int size)
            {
                data = new int[size];
                for (int i = 0; i < size; i++) data[i] = -1;
            }

            public bool UnionSet(int x, int y)
            {
                x = Root(x); y = Root(y);
                if (x != y)
                {
                    if (data[y] < data[x]) { int t = x; x = y; y = t; }
                    data[x] += data[y];
                    data[y] = x;
                }
                return x != y;
            }

            public bool FindSet(int x, int y)
            {
                return Root(x) == Root(y);
            }

            public int Root(int x)
            {
                return data[x] < 0 ? x : data[x] = Root(data[x]);
            }

            public int Size(int x)
            {
                return -data[Root(x)];
            }
        }
        #endregion
    }
}

Misinterpretation CodeChef Solution in GO

package main

import (
	"fmt"
)

func pow(n, k int64) int {
	toReturn := int64(1)
	for i := int64(0); i < k; i++ {
		toReturn *= n
		toReturn = toReturn % 1000000007
	}

	return int(toReturn)
}

func permute(size int) (out []int) {
	out = make([]int, size)
	o, e := 1, 2

	for i := 0; i < size / 2; i++ {
		out[i] = e
		e += 2
	}

	for i := size / 2; i < size; i++ {
		out[i] = o
		o += 2
	}

	return
}

func nSets(ps []int) (n int) {
	// Find loop
	n = 0

	for i := range ps {
		if ps[i] < 0 {
			continue
		}
		next := i + 1
		for next > 0 {
			tmp := ps[next - 1]
			ps[next - 1] = -(i + 1)
			next = tmp
		}
	}

	for i, val := range ps {
		if i == -(val + 1) {
			n++
		}
	}

	return
}

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var size int
		fmt.Scan(&size)
		fmt.Println(pow(int64(26), int64(nSets(permute(size)))))
	}
}
Misinterpretation CodeChef Solution Review:

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

Find on CodeChef

Conclusion:

I hope this Misinterpretation 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 *