Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Physics Class CodeChef Solution

Problem -Physics Class 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.

Physics Class CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	// your code goes here
	int t;
	cin>> t;
	while(t--){
	    ll n,f;
	    cin>> n>>f;
	    ll arr[n];
	    map<ll,ll> mp;
	    for(int i=0;i<n;i++){
	        cin>>arr[i];
	       // mp[arr[i]]++;
	    }
	    int ans=0;
	    for(int i=0;i<n;i++){
	        ll k = arr[i];
	        
	        
	        while(k%f==0){
	            
	            k/=f;
	        }
	        mp[k]++;
	    }
	    for(auto x:mp){
	        ans+=(x.second*(x.second-1))/2;
	    }
	    cout << ans << endl;
	}
	return 0;
}

Physics Class CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here
n = int(input())

for _ in range(n):
    n, f = [int(c) for c in input().split()]
    li = [int(c) for c in input().split()]
    count = 0
    a = []
    s = {}
    for i in li:
        h = i
        while h % f == 0:
            h /= f
        a.append(h)
        s[h] = 0

    for i in a:
        s[i] += 1

    for i in s:
        count += (s[i] * (s[i] - 1)) // 2

    print(count)

Physics Class CodeChef Solution in C

#include<stdio.h>
 
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;
 
    /* create temp arrays */
    int L[n1], R[n2];
 
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];
 
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }
 
    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 
/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;
 
        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m+1, r);
 
        merge(arr, l, m, r);
    }
}
 
int binary(int a[],int req,int i,int j)
{
    if(i>j) return -1;
    int mid=(i+j)/2;
    if(a[mid]==req)
    {
        int pos=mid;
        while(pos>=0 && a[pos]==req) pos--;
        return pos+1;
    }
    if(a[mid]<req)  return binary(a,req,mid+1,j);
    else    return binary(a,req,i,mid-1);
}
int main()
{
    int t,n,f,i,a[10010],req,pos,same,count1=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&f);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        mergeSort(a,0,n-1);
        int count=0,req2;
        same=0;
        for(i=n-1;i>0;i--)
        {
            req=a[i];
            count1=0;
            while(req>0)
            {
                pos=binary(a,req,0,n-1);
                if(pos!=-1 && a[i]==a[pos])
                {
                    count+=((i-pos+1)*(i-pos))/2;
                    same=i-pos+1;
                }
                else
                {
                    while(pos!=-1 && pos<i && a[pos]==req)
                    {
                        pos++;
                        count1++;
                    }
                }
                if(req%f==0)
                    req/=f;
                else break;
            }
           // printf("%d %d\n",count1,same);
            count+=count1*same;
            i=i-same+1;
        }
        printf("%d\n",count);
    }
    return 0;
}

Physics Class 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. */
class Codechef
{
    Input1 aa;
    public void calc() throws Exception 
    {
        int n=ni();
        int f=ni();
        int ar[]=new int[n];
        long ans=0L;
        int i,b;
        for(i=0;i<n;i++)
        {
            ar[i]=ni();
        }
        Arrays.sort(ar);
        Map<Integer,Integer> hm=new HashMap<>();
        for(i=0;i<n;i++)
        {
             if(hm.containsKey(ar[i]))
                {
                    ans=ans+(long)hm.get(ar[i]);
                    hm.put(ar[i],hm.get(ar[i])+1);
                }
                else 
                hm.put(ar[i],1);
                b=ar[i];
                while(b>=f && b%f==0)
                {
                    b=b/f;
                    if(hm.containsKey(b))
                    ans = ans + (long)hm.get(b);
                }
            
        }
        System.out.println(ans);
    }
    public void run() throws Exception 
    {
        aa=new Input1();
        int t=ni();
        while(t-->0)
        {
            calc();
        }
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		try{
		    new Codechef().run();
		}
		catch(Exception ee)
		{
		    
		}
	}
	public int ni() throws Exception 
	{
	    return Integer.parseInt(aa.next());
	}
	class Input1 
	{
	    BufferedReader br;
	    StringTokenizer str;
	    public Input1()
	    {
	        br=new BufferedReader(new InputStreamReader(System.in));
	    }
	    public String next() throws Exception 
	    {
	        while(str==null || !str.hasMoreTokens())
	        {
	            str=new StringTokenizer(br.readLine());
	        }
	        return str.nextToken();
	    }
	}
}

Physics Class CodeChef Solution in PYPY 3

mod = 1000000007
#from math import factorial, ceil, pow, sqrt, floor, gcd
from sys import stdin, stdout
#from collections import defaultdict, Counter, deque
#from bisect import bisect_left, bisect_right
# import sympy
# from itertools import permutations
# import numpy as np

#           n = int(stdin.readline())
#           stdout.write(str())
#           s = stdin.readline().strip('\n')
#           map(int, stdin.readline().split())
#           l = list(map(int, stdin.readline().split()))
from collections import Counter
for _ in range(int(stdin.readline())):
    n,f=map(int, stdin.readline().split())
    li=list(map(int, stdin.readline().split()))
    ans=0
    if f==1:
        print((n*(n-1))//2)
    else:
        for i in range(n):
            while li[i]%f==0:
                li[i]=li[i]//f
        dic=Counter(li)
        for v in dic.values():
            ans+=(v*(v-1))//2
        print(ans)

Physics Class CodeChef Solution in PYTH

T = int(raw_input())
for _ in range(T) :
	s = (raw_input().strip()).split()
	n = int(s[0])
	f = int(s[1])
	hd = dict()
	s = (raw_input().strip()).split()
	#print s
	heights = map(lambda x : int(x),s)
	
	for h in heights :
		while h%f==0 :
			h=h/f
		if h in hd :
			hd[h]+=1
		else :
			hd[h] = 1
	pairs = 0
	for k,v in hd.iteritems() :
		pairs += v*(v-1)/2
	print pairs

Physics Class CodeChef Solution in C#

using System;
using System.Linq;
using System.Collections.Generic;
public class Test
{
	public static void Main()
	{
		// your code goes here
		int t = int.Parse(Console.ReadLine().Trim());
		while(t-- > 0){
			int[] data = Console.ReadLine().Trim().Split().Select(x => int.Parse(x)).ToArray();
			int[] num = Console.ReadLine().Trim().Split().Select(x => int.Parse(x)).ToArray();
			int n = data[0];
			int f = data[1];
			Dictionary<int,int> dict = new Dictionary<int,int>();
			for(int i = 0 ; i < n ; i++){
				if(!dict.ContainsKey(num[i]))
				      dict.Add(num[i] , 0 );
				 dict[num[i]]++;
			}
			long total = 0 ;
			Dictionary<int,int> usedH = new Dictionary<int,int>();
			for(int i = 0 ; i < n ; i++){
				int h = num[i];
				//find the same height count
				int cntSame = dict[h] - 1;
				//Console.WriteLine(h + "-" + cntSame);
				if(cntSame != 0){
					if(usedH.ContainsKey(h)){
						cntSame = cntSame - usedH[h];
						usedH[h]++;
					} else {
						usedH.Add(h,1);
					}
				}
				total += cntSame;
				for(int k = 1 ; k < 32 ; k++){
					long p = (long)Math.Pow(f,k);
					if(p > h)
					    break;
					 if(h % (int)p != 0)
					       break;
					 int another = h / (int)p;
					 total += !dict.ContainsKey(another) ? 0 : dict[another];
					 //Console.WriteLine(h + "-" + another + "-" + total);
				}
			}
			Console.WriteLine(total);
		}
	}
}
Physics Class CodeChef Solution Review:

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

Find on CodeChef

Conclusion:

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