• Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
Friday, February 3, 2023
  • Login
Zeroplusfour
No Result
View All Result
  • Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
  • Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
No Result
View All Result
Zeroplusfour
No Result
View All Result
Home Code Solutions Hackerrank Algorithms

Bead Ornaments – HackerRank Solution

Bead Ornaments - HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

bhautik bhalala by bhautik bhalala
May 26, 2022
Reading Time: 1 min read
0
15 Days to learn SQL Hard SQL(Advanced)-Solution

15 Days to learn SQL Hard SQL(Advanced)-Solution alt text

Spread the love

Table of Contents

  • Bead Ornaments  – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
  • Solutions of Algorithms Data Structures Hard HackerRank:
    • Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
  • C++ Bead Ornaments HackerRank Solution
  • Java Bead Ornaments HackerRank Solution
  • Python 3 Bead Ornaments HackerRank Solution
  • Python 2 Bead Ornaments HackerRank Solution
  • C Bead Ornaments HackerRank Solution
    • Leave a comment below
      • Related posts:

Bead Ornaments  – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

Solutions of Algorithms Data Structures Hard HackerRank:

Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts

C++ Bead Ornaments HackerRank Solution


Copy Code Copied Use a different Browser

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<cstdlib>
#include<memory.h>
#include<ctime>

using namespace std;


typedef long double ld;

typedef long long ll;
typedef pair<int,int>	pii;
typedef pair<ld,ld>	pdd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef pair<ll,ll> pl;

#define FOR(i,a,b)		for(int i=(a);i<(b);i++)
#define REP(i,n)		FOR(i,0,n)
#define SORT(v)			sort((v).begin(),(v).end())
#define UN(v)			SORT(v),(v).erase(unique((v).begin(),(v).end()),(v).end())
#define CL(a,b)			memset(a,b,sizeof a)
#define pb				push_back


const int mod = 1000000007;

int cc[55][55];
bool hasc;

ll c(int n,int m){
	if(m>n || m<0 || n<0) return 0;
	if(!hasc){
		hasc=1;
		REP(i,55){
			cc[i][i]=cc[i][0]=1;
			FOR(j,1,i)
				cc[i][j]=(cc[i-1][j-1]+cc[i-1][j])%mod;
		}
	}
	return cc[n][m];
}

ll tr[33][33][33];

ll tree(int n,int d,int m){
	if(d==1){
		if(n==1 && m==1) return 1;
		return 0;
	}
	if(tr[n][d][m]!=-1)
		return tr[n][d][m];
	ll val = 0;
	FOR(pr,1,n-m+1){
		ll t = tree(n-m,d-1,pr);
		REP(i,m) t*=pr,t%=mod;
		t *= c(n,m);t%=mod;
		val += t;val%=mod;
	}
	return tr[n][d][m] = val;
}
ll qp(ll d,ll st){
	ll r =1;
	while(st){
		if(st&1)r*=d,r%=mod;
		d*=d,d%=mod;
		st>>=1;
	}
	return r;
}
ll nt[33];
bool hasnt;
ll numt(int d){
	if(!hasnt){
		hasnt=true;
		CL(nt,-1);
	}
	if(nt[d]!=-1)return nt[d];
	ll res = 0;
	FOR(l,1,d+1)FOR(nl,1,d+1)
		res+=tree(d,l,nl),res%=mod;
	res*=qp(d,mod-2);res%=mod;
	return nt[d]=res;
}

ll w[333][1<<9];
vi v;
int n;	

ll go(int numv,int mmask){
	if(mmask==0) return 1;
	else{
		if(w[numv][mmask]!=-1) return w[numv][mmask];

		ll res = 0;
		for(int mask=mmask;mask;mask=(mask-1)&mmask){
			int nnv = 0;
			vi nt;
			ll ncc = 1;
			REP(j,n-1) if(mask&(1<<j)){
				nnv += v[j];
				ncc*=numv;ncc%=mod;
				ncc*=v[j];ncc%=mod;
				ncc*=numt(v[j]);ncc%=mod;
			}else nt.pb(v[j]);

			res += ncc * go(nnv,mmask^mask);
			res %= mod;
		}
		return w[numv][mmask]=res;
	}
}

int main(){
#ifdef LocalHost
	freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
#endif
	CL(tr,-1);
	int tc;
	cin>>tc;
	REP(TC,tc){
		cin>>n;
		v.resize(n);
		REP(i,n)cin>>v[i];
		vi t = v;
		t.erase(t.begin());
		CL(w,-1);
		ll res=numt(v[n-1])*go(v[n-1],(1<<(n-1))-1)%mod;
		cout<<res<<endl;
	}

#ifdef LocalHost
	printf("TIME: %.3lf\n",ld(clock())/CLOCKS_PER_SEC);
#endif
	return 0;
}

Java Bead Ornaments HackerRank Solution


Copy Code Copied Use a different Browser

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;

public class Solution {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	
	static void dfs(int n, int rd, int res, int[] dim, List<int[]> dims, int num)
	{
		if(res == 0){
			dim[n] = num;
			if(rd == 0)dims.add(Arrays.copyOf(dim, dim.length));
		}else{
			for(int i = 1;i <= rd;i++){
				dim[n-res] = i;
				dfs(n, rd-i, res-1, dim, dims, num);
				num /= i;
			}
		}
	}
	
	static void solve()
	{
		List<List<int[]>> dist = new ArrayList<List<int[]>>();
		dist.add(null);
		dist.add(null);
		for(int i = 2;i <= 10;i++){
			int f = 1;
			for(int j = 1;j <= i-2;j++){
				f *= j;
			}
			List<int[]> dims = new ArrayList<int[]>();
			dfs(i, 2*(i-1), i, new int[i+1], dims, f);
			dist.add(dims);
		}
		
		int mod = 1000000007;
		for(int T = ni();T >= 1;T--){
			int n = ni();
			int[] b = na(n);
			long ret = 1;
			if(n > 1){
				for(int i = 0;i < n;i++){
					ret = ret * pow(b[i], b[i]-2, mod) % mod;
				}
				
				long lsum = 0;
				for(int[] dim : dist.get(n)){
					long lmul = 1;
					for(int i = 0;i < n;i++){
						lmul = lmul * pow(b[i], dim[i], mod) % mod;
					}
					lsum += lmul * dim[n] % mod;
				}
				lsum %= mod;
				ret = ret * lsum % mod;
			}else{
				ret = pow(b[0], b[0]-2, mod);
			}
			out.println(ret);
		}
	}
	
	public static long invl(long a, long mod)
	{
		long b = mod;
		long p = 1, q = 0;
		while(b > 0){
			long c = a / b;
			long d;
			d = a; a = b; b = d % b;
			d = p; p = q; q = d - c * q;
		}
		return p < 0 ? p + mod : p;
	}
	
	public static long pow(long a, long n, long mod)
	{
		long ret = 1;
		int x = 63-Long.numberOfLeadingZeros(n);
		for(;x >= 0;x--){
			ret = ret * ret % mod;
			if(n<<63-x<0)ret = ret * a % mod;
		}
		return ret;
	}
	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static boolean eof()
	{
		if(lenbuf == -1)return true;
		int lptr = ptrbuf;
		while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
		
		try {
			is.mark(1000);
			while(true){
				int b = is.read();
				if(b == -1){
					is.reset();
					return true;
				}else if(!isSpaceChar(b)){
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}
	
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;
	
	private static int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static double nd() { return Double.parseDouble(ns()); }
	private static char nc() { return (char)skip(); }
	
	private static String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private static char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private static char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private static int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private static int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}

 



Python 3 Bead Ornaments HackerRank Solution


Copy Code Copied Use a different Browser

t=int(input());
for i in range(t):
    n=int(input())
    ans=1
    s=0
    a=input()
    b=[int(a) for a in a.split(' ')]
    for j in b:
        ans=ans*(j**(j-1))
        s=s+j
    ans=ans*(s**(n-2))
    ans=ans%1000000007
    print (int(ans))



Python 2 Bead Ornaments HackerRank Solution


Copy Code Copied Use a different Browser

import sys, math
def rs():
    return sys.stdin.readline().strip()
def ri():
    return int(sys.stdin.readline().strip())
def ras():
    return list(sys.stdin.readline().strip())
def rai():
    return map(int,sys.stdin.readline().strip().split())
c= 1000000007
f = lambda v: (v ** (v - 2)) % c
n = ri()

for x in xrange(n):
  k = ri()
  arr = rai()
  if k==1:
    print f(arr[0])
    continue
  r = 1
  for o in arr:
    r = r * f(o) % c
    r = r * o % c
  print int(r * sum(arr)**(k-2)) % c



C Bead Ornaments HackerRank Solution


Copy Code Copied Use a different Browser

#include<stdio.h>
#include<stdlib.h>


unsigned long long a[31];

int nofbead(void)
{
	int i,j,k;
	int n;
	unsigned int *b;
	unsigned long long sum,mul,total;
	scanf("%d",&n);
	b=(int *)malloc(sizeof(int)*n);
	for(i=0;i<n;i++) scanf("%d",&b[i]);
	sum=1;
	mul=1;
	if(n==1)
	{
		printf("%d\n",a[b[0]]);
		return 0;
	}
	if(n==2)
	{
		sum=(a[b[0]]*a[b[1]])%1000000007;
		sum=(sum*(b[0]*b[1]))%1000000007;
		printf("%d\n",sum);
		return 0;
	}
	for(i=0;i<n;i++)
	{
		mul=(mul*b[i])%1000000007;
	}
	sum=0;
	for(i=0;i<n;i++)
	{
		sum+=b[i];
	}
	total=1;
	for(i=0;i<n-2;i++)
	{
		total=(total*sum)%1000000007;
	}
	total=(total*mul)%1000000007;
	for(i=0;i<n;i++) 
	{
		total=(total*a[b[i]])%1000000007;
	}
	printf("%d\n",total);
	free(b);
	return 0;
}

int main()
{
	int i,j,t;
	for(i=0;i<31;i++) a[i]=i;
	a[1]=1;
	a[2]=1;
	for(i=3;i<=30;i++)
	{
		for(j=1;j<(i-2);j++)
		{
			a[i]=(i*a[i])%1000000007;
		}
//        printf("%ld\n",a[i]);
	}
	scanf("%d",&t);
	for(i=0;i<t;i++) nofbead();
	return 0;
}

 

Leave a comment below

 

Related posts:

15 Days to learn SQL Hard SQL(Advanced)-SolutionLetter Islands – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionHow Many Substrings? – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionTime Conversion – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionZig Zag Sequence – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionBalanced Brackets – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionNo Prefix Set – HackerRank Solution
Tags: Bead OrnamentsCc++14full solutionGoHackerRank Solutionjavajava 15java 7java 8java8javascriptpypy 3Python 2python 3
ShareTweetPin
bhautik bhalala

bhautik bhalala

Related Posts

Leetcode All Problems Solutions
Code Solutions

Exclusive Time of Functions – LeetCode Solution

by admin
October 5, 2022
0
30

Exclusive Time of Functions - LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions...

Read more
Leetcode All Problems Solutions

Smallest Range Covering Elements from K Lists – LeetCode Solution

October 5, 2022
32
Leetcode All Problems Solutions

Course Schedule III – LeetCode Solution

October 5, 2022
27
Leetcode All Problems Solutions

Maximum Product of Three Numbers – LeetCode Solution

September 11, 2022
53
Leetcode All Problems Solutions

Task Scheduler – LeetCode Solution

September 11, 2022
119
Leetcode All Problems Solutions

Valid Triangle Number – LeetCode Solution

September 11, 2022
28
Next Post
15 Days to learn SQL Hard SQL(Advanced)-Solution

Floyd : City of Blinding Lights - HackerRank Solution

15 Days to learn SQL Hard SQL(Advanced)-Solution

Kingdom Connectivity - HackerRank Solution

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

15 Days to learn SQL Hard SQL(Advanced)-SolutionLetter Islands – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionHow Many Substrings? – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionTime Conversion – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionZig Zag Sequence – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionBalanced Brackets – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionNo Prefix Set – HackerRank Solution

Categories

  • Algorithms
  • Anime
  • Biography
  • Business
  • Code Solutions
  • Cosmos
  • Countdowns
  • Culture
  • Economy
  • Education
  • Entertainment
  • Finance
  • Games
  • Hackerrank
  • Health
  • How to
  • Investment
  • LeetCode
  • Lifestyle
  • LINUX SHELL
  • Manga
  • News
  • Opinion
  • Politics
  • Sports
  • SQL
  • Tech
  • Travel
  • Uncategorized
  • Updates
  • World
  • DMCA
  • Home
  • My account
  • Privacy Policy
  • Top Posts

Recent Blogs

Leetcode All Problems Solutions

Exclusive Time of Functions – LeetCode Solution

October 5, 2022
Leetcode All Problems Solutions

Smallest Range Covering Elements from K Lists – LeetCode Solution

October 5, 2022
15 Days to learn SQL Hard SQL(Advanced)-Solution
Algorithms

Morgan and a String – HackerRank Solution

September 5, 2022
162
Leetcode All Problems Solutions
Code Solutions

Subarray Sum Equals K – LeetCode Solution

September 10, 2022
34
Shark Tank
Business

Spergo Net Worth 2022 – What Happened After Shark Tank? Everything You need to know.

September 4, 2022
4
15 Days to learn SQL Hard SQL(Advanced)-Solution
Algorithms

2’s complement – HackerRank Solution

August 24, 2022
5

© 2022 ZeroPlusFour - Latest News & Blog.

No Result
View All Result
  • Home
  • Category
    • Business
    • Culture
    • Economy
    • Lifestyle
    • Health
    • Travel
    • Opinion
    • Politics
    • Tech
  • Landing Page
  • Support Forum
  • Contact Us

© 2022 ZeroPlusFour - Latest News & Blog.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?