Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Minimum Players required to win the game in C++
Problem statement
Given N questions and K options for each question, where 1
Please note that any wrong answer leads to elimination of the player
Example
If N = 5 and K = 2 then answer is 62.
Algorithm
- To solve Nth question K players are needed.
- To solve (N-1)th question K2 players are needed.
- Similarly moving onwards, to solve 1st question KN players are needed.
- So, our problem reduces to finding the sum of GP terms K + K2 + … + KN which is equal to: K(Kn -1) / K -1
Example
#include#include #define MOD 1000000007 using namespace std; long long int power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = res * a; res = res % MOD; } b = b / 2; a = a * a; a = a % MOD; } return res; } long long getMinPlayer(long long n, long long k) { long long num = ((power(k, n) - 1) + MOD) % MOD; long long den = (power(k - 1, MOD - 2) + MOD) % MOD; long long ans = (((num * den) % MOD) * k) % MOD; return ans; } int main() { long long n = 5, k = 2; cout Output
When you compile and execute above program. It generates following output −
Minimum pairs = 62
Advertisements
