• Home
  • Top Posts
  • Code Solutions
  • How to
  • News
  • Trending
  • Anime
  • Health
  • Education
Friday, January 27, 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

Tastes Like Winning – HackerRank Solution

Tastes Like Winning - HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

admin by admin
August 24, 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

  • Tastes Like Winning – 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++ replace HackerRank Solution
  • Java rep HackerRank Solution
  • Python 3 rep HackerRank Solution
  • Python 2 rep HackerRank Solution
  • C rep HackerRank Solution
    • Warmup Implementation Strings Sorting Search Graph Theory Greedy Dynamic Programming Constructive Algorithms Bit Manipulation Recursion Game Theory NP Complete Debugging
    • Leave a comment below
      • Related posts:

Tastes Like Winning – 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++ replace HackerRank Solution


Copy Code Copied Use a different Browser

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <complex>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef long double ld;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
typedef vector<string> vs;
const int mod = 1000000007;

ll mpow(ll x, ll n) {
    ll res = 1;
    while (n) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n /= 2;
    }
    return res;
}

int stupid(int n, int m) {
    int c = 0;
    int all = (1 << m) - 1;
    for (int mask = 1; mask < (1 << all); ++mask) if (__builtin_popcount(mask) == n) {
        int xr = 0;
        for (int i = 0; i < all; ++i) if (mask & (1 << i)) {
            xr ^= 1 + i;
        }
        if (xr) ++c;
    }
    for (int i = 2; i <= n; ++i) c = c * (ll)i % mod;
    return c;
}

int solve(int n, int m) {
    vi d(n + 1);
    vi a(n + 1);
    d[0] = 1;
    d[1] = 0;
    ll dif = mpow(2, m) - 1;
    a[0] = 1;
    for (int i = 1; i <= n; ++i) a[i] = a[i-1] * (dif - i + 1) % mod;
    for (int i = 2; i <= n; ++i) {
        d[i] = (a[i-1] - d[i-2] * (dif - i + 2) % mod * (i-1) - d[i-1]) % mod;
        //cerr << i << ' ' << d[i] << endl;
    }
    return (a[n] - (ll)d[n] + 2*mod) % mod;
}

int main() {
    int n,m;
    cin >> n >> m;
    cout << solve(n, m) << endl;
    //cerr << stupid(n,m) << ' ' << a[n] << endl;
    return 0;
}

Java rep 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.Arrays;
import java.util.InputMismatchException;

public class G {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = ni();
		// C(2^n, m)
		if(m < 33 && n > (1L<<m)-1){
			out.println(0);
			return;
		}
		int mod = 1000000007;
		long ret = C(pow(2, m, mod)-1, n, mod);
		
		long del = 0;
		long num = 1;
		long N = pow(2, m-1, mod);
		for(int i = 0;i <= n/2;i++){
			del += num;
			num = num * (N-i) % mod * invl(i+1, mod) % mod * (-1);
		}
		if(n % 2 == 1)del = -del;
		del %= mod;
		
		// x+del+x*(2^m-1) = ret
		long x = (ret-del)*invl(pow(2, m, mod), mod) %mod* (pow(2, m, mod)-1)%mod;
		if(x < 0)x += mod;
		for(int i = 1;i <= n;i++)x = x * i % mod;
		out.println(x);
	}
	
	
	public static long C(long n, long r, int p)
	{
		if(n < 0 || r < 0 || r > n)return 0L;
		if(r == 0 || n == r)return 1;
		long pe = 0;
		for(long i = n/p;i >= 1;i/=p)pe += i;
		for(long i = r/p;i >= 1;i/=p)pe -= i;
		for(long i = (n-r)/p;i >= 1;i/=p)pe -= i;
		if(pe > 0)return 0L;
		
		long flip = n/p-r/p-(n-r)/p;
		int nmp = (int)(n%p), rmp = (int)(r%p);
		if(nmp < rmp)return 0L;
		long ret = C(n/p, r/p, p);
		long den = 1;
		for(int i = 1;i <= rmp;i++){
			ret = ret * (nmp-i+1) % p;
			den = den * i % p;
		}
		ret = ret * invl(den, p) % p;
		if(flip % 2 != 0){
			ret = -ret;
			if(ret < 0)ret += p;
		}
		return 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) {
		//		a %= 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;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}
	
	public static void main(String[] args) throws Exception { new G().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private 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 boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private 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 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 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 int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private 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 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) { System.out.println(Arrays.deepToString(o)); }
}

 



Python 3 rep HackerRank Solution


Copy Code Copied Use a different Browser

_=input().split()
mod=10**9+7
n=int(_[0])
m=int(_[1])
M = pow(2, m, mod) - 1
comb_n_1 = (M%mod)* ((M-1)%mod)
nim_n_2 = 0 #total number combinations that is nim for n-2 stacks
nim_n_1 = 0 #total number combinations that is nim for n-1 stacks
nim_n = 0  #total number combinations that is nim for n stacks

if n==1:
    print (M%mod)
elif n==2:
    print (((M%mod)*((M-1)%mod))%mod)
else:
    for i in range(3, n + 1):
        nim_n = (comb_n_1 - nim_n_1 - nim_n_2 * (M-i+2) * (i-1)) % mod
        '''total number of nim for n stacks = maximum possible number of nim = m!/(m-n+1)! 
                                              [n-1 different numbers]
                                             -(n-1 of them is nim) 
                                              [nim for n-1 stacks]
                                             -(n-2 of them is nim plus any numbers that is not included)
                                              [nim for n-2 stacks * m-n+2 * n-1]
        '''
        comb_n_1 = (comb_n_1 * (M - i + 1)) % mod
        nim_n_2 = nim_n_1
        nim_n_1 = nim_n

    print ((comb_n_1 - nim_n) % mod)



Python 2 rep HackerRank Solution


Copy Code Copied Use a different Browser

# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
n,m = map(int,sys.stdin.readline().split())

p = 1000000007

def pow_mod(x, y, z):
    number = 1
    while y:
        if y & 1:
            number = number * x % z
        y >>= 1
        x = x * x % z
    return number

S = pow_mod(2,m,p)-1 % p
A1 = 0
A2 = 0
A3 = S
W = S

z1 = pow_mod(2,m,p)
x = z1-1
for i in range(2,n+1):
    x -= 1
    A3 = (i * (S-W) * x)%p
    S = (S*x)%p
    W = (S-A1)
    A1 = (W - A3)
    #A2 = (S-W)
print W%p



C rep HackerRank Solution


Copy Code Copied Use a different Browser

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

// for xor of n distinct numbers chosen from [1,(2^m -1)], how many are nonzero?
// Well, choose n-1 distinct numbers that don't xor to 0.  Multiply by (2^m -1 - (n-1) - 1)'
// oops, above doesn't work because sometimes the n-1 numbers include the one which cancels.
// Okay, so when does it include the one that cancels?  When it's a subset of n-2 numbers which
// do xor to 0, added to any other.
//
// Or, choose n-1 distinct numbers which DO xor to 0.  Multiply by (2^m - 1 - (n-1))
// How many ways to choose n distinct numbers from [1, 2^m - 1]?  C(2^m-1, n) of course
//
// So, how many are zero?
// Choose n-1 distinct numbers which don't xor to 0
// Except those n-1 which consist of n-2 which do add to zero plus one more.  Then one will cancel it.
// (note: order matters in the problem)

typedef int num_t;
num_t modulus = 1000000007;

int modmul(int a, int b, int m) {
    return ((long)a*b) % m;
}

int modexp(int a, int b, int m) {
    if (b == 0) return 1;
    if (b&1) {
        return ((long)a * modexp(a, b - 1, m)) % m;
    }
    int r1 = modexp(a, b/2, m);
    return ((long)r1*r1) % m;
}

num_t count_nz_xor1(int n, int m, num_t m2m1);

num_t memo0[10000002];
num_t count_z_xor1(int n, int m, num_t m2m1) {
    if (n == 0) return 1;
    if (n == 1) return 0;
    if (memo0[n]) return memo0[n] - 1;
    num_t recurse2 = count_z_xor1(n-2, m, m2m1);
    recurse2 = modmul(recurse2, n-1, modulus);
    num_t recurse1 = count_nz_xor1(n-1, m, m2m1);
    num_t mult = m2m1 - (n-2);
    if (mult < 0) mult += modulus;
    num_t result = recurse1 - modmul(recurse2, mult, modulus);
    if (result < 0) result += modulus;
    memo0[n] = result + 1;
//    fprintf(stderr, "count_z_xor %d %d = %lld\n", n, m, result);
    return result;
}

num_t memo[10000002];
num_t count_nz_xor1(int n, int m, num_t m2m1) {
    if (n == 0) return 0;
    if (n == 1) {
        return m2m1;
    }
    if (memo[n]) return memo[n] -1;
    num_t recurse2 = count_z_xor1(n-2, m, m2m1);
    num_t recurse1z = count_z_xor1(n-1, m, m2m1);
    num_t recurse1n = count_nz_xor1(n-1, m, m2m1);
//    num_t xxx = recurse1n - recurse2 * (m2m1 - (n - 2));  // which is exactly count_z_xor(n, m)
    num_t xxx = count_z_xor1(n, m, m2m1);
//    num_t result = recurse1z * (m2m1 - (n-1)) + xxx * (m2m1 - (n-1) - 1) + (recurse1n - xxx) * (m2m1 - (n-1));
    num_t tot = recurse1z + recurse1n;
    if (tot >= modulus) tot -= modulus;
    num_t mult = m2m1 - (n - 1);
    if (mult < 0) mult += modulus;
    num_t result = modmul(tot, mult, modulus);
    result -= xxx;
    if (result < 0) result += modulus;
//    num_t result = (recurse1z + recurse1n) * (m2m1 - (n-1)) - xxx;
    // note recurse1z + recurse1n = (n-1)! So this amounts to all possibilities for n-1, times all available numbers, minus the number of zeros at this n, which is of course correct.  
//    fprintf(stderr, "count_nz_xor %d %d = %lld\n", n, m, result);
    memo[n] = result + 1;
    return result;
}
num_t count_nz_xor(int n, int m) {
    num_t m2m1 = modexp(2, m, modulus);
    m2m1 -= 1;
    if (m2m1 < 0) m2m1 += modulus;
    for (int i = 2; i < n; i++) {
        count_nz_xor1(i, m, m2m1);
    }
    return count_nz_xor1(n, m, m2m1);
}

int main() {
    int n, m;
    scanf("%d %d\n", &n, &m);
    // there's one failure where m = 4
    if (m > 4) {
//        exit(-1);
    }
    printf("%d", count_nz_xor(n, m));
    return 0;
}

 

Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging

Leave a comment below

 

Related posts:

15 Days to learn SQL Hard SQL(Advanced)-SolutionTwo Strings Game – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionQueue using Two Stacks – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionTree Flow – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionBeautiful 3 Set – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionReverse a doubly linked list – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionString Transmission – HackerRank Solution
Tags: Cc++14full solutionGoHackerRank Solutionjavajava 15java 7java 8java8javascriptpypy 3Python 2python 3Tastes Like Winning
ShareTweetPin
admin

admin

Related Posts

Leetcode All Problems Solutions
Code Solutions

Exclusive Time of Functions – LeetCode Solution

by admin
October 5, 2022
0
29

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
25
Leetcode All Problems Solutions

Maximum Product of Three Numbers – LeetCode Solution

September 11, 2022
52
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

Walking the Approximate Longest Path - HackerRank Solution

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

Sam's Puzzle (Approximate) - 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)-SolutionTwo Strings Game – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionQueue using Two Stacks – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionTree Flow – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionBeautiful 3 Set – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionReverse a doubly linked list – HackerRank Solution 15 Days to learn SQL Hard SQL(Advanced)-SolutionString Transmission – 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

Binary Search Tree : Lowest Common Ancestor – HackerRank Solution

May 31, 2022
13
Leetcode All Problems Solutions
Code Solutions

Teemo Attacking – LeetCode Solution

September 10, 2022
8
3 inter-linked diseases of Heart value disease
Health

3 Important inter-linked diseases of Heart valve disease

May 13, 2022
6
15 Days to learn SQL Hard SQL(Advanced)-Solution
Code Solutions

Average Population of Each Continent EasySQL (Basic) – SQL – HackerRank Solution

May 30, 2022
0

© 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?