Minimum Players required to win the game

Given N questions and K options for each question, where 1\leq N \leq 1000000000 and 1\leq K \leq 1000000000. The task is to determine sum of total number of player who has attempted ith question for all 1 \leq i \leq N to win the game anyhow. You have to minimize the sum of total number of player and output it modulo 109+7.

Note: Any wrong answer leads to elimination of the player.

Examples:

Input: N = 3, K = 3
Output: 39

Input: N = 5, K = 2
Output: 62

Approach:

  • 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 \frac{K(K^{N}-1)}{K-1}.
Now we can use Fermat’s Little Theorem to get the required answer modulo with 109+7.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find minimum players
// required to win the game anyhow
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
  
// function to calculate (a^b)%(10^9+7).
long long int power(long long int a, long long int b)
{
    long long int res = 1;
    while (b) {
        if (b & 1) {
            res *= a;
            res %= mod;
        }
        b /= 2;
        a *= a;
        a %= mod;
    }
    return res;
}
  
// function to find the minimum required player
long long int minPlayer(long long int n, long long int k)
{
  
    // computing the nenomenator
    long long int num = ((power(k, n) - 1) + mod) % mod;
  
    // computing modulo inverse of denomenator
    long long int den = (power(k - 1, mod - 2) + mod) % mod;
  
    // final result
    long long int ans = (((num * den) % mod) * k) % mod;
  
    return ans;
}
  
// Driver code
int main()
{
  
    long long int n = 3, k = 3;
  
    cout << minPlayer(n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

//Java program to find minimum players
//required to win the game anyhow
public class TYU {
      
    static long  mod =  1000000007;
  
    //function to calculate (a^b)%(10^9+7).
     static long power(long a, long b)
     {
      long res = 1;
      while (b != 0) {
          if ((b & 1) != 0) {
              res *= a;
              res %= mod;
          }
          b /= 2;
          a *= a;
          a %= mod;
      }
      return res;
     }
  
     //function to find the minimum required player
     static long minPlayer(long n, long k)
     {
  
      // computing the nenomenator
      long num = ((power(k, n) - 1) + mod) % mod;
  
      // computing modulo inverse of denomenator
      long den = (power(k - 1, mod - 2) + mod) % mod;
  
      // final result
      long ans = (((num * den) % mod) * k) % mod;
  
      return ans;
     }
  
     //Driver code
    public static void main(String[] args) {
          
         long n = 3, k = 3;
  
         System.out.println(minPlayer(n, k));
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program  to find minimum players 
# 3 required to win the game anyhow
  
# constant
mod = 1000000007
  
# function to calculate (a^b)%(10^9+7).
def power(a, b) :
    res = 1
  
    while(b) :
        if (b & 1) :
            res *= a
            res %= mod
  
        b //= 2
        a *= a
        a %= mod
  
    return res
  
# function to find the minimum required player 
def minPlayer(n, k) :
  
    # computing the nenomenator 
    num = ((power(k, n) - 1) + mod) % mod
  
    # computing modulo inverse of denomenator 
    den = (power(k - 1,mod - 2) + mod) % mod
  
    # final result 
    ans = (((num * den) % mod ) * k) % mod
  
    return ans
  
# Driver Code
if __name__ == "__main__" :
  
    n, k = 3, 3
  
    print(minPlayer(n, k))
  
# This code is contributed by ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find minimum players
// required to win the game anyhow
using System;
class GFG
{
  
static long mod = 1000000007;
  
// function to calculate (a^b)%(10^9+7).
static long power(long a, long b)
{
long res = 1;
while (b != 0) 
{
    if ((b & 1) != 0) 
    {
        res *= a;
        res %= mod;
    }
    b /= 2;
    a *= a;
    a %= mod;
}
return res;
}
  
// function to find the minimum
// required player
static long minPlayer(long n, long k)
{
  
    // computing the nenomenator
    long num = ((power(k, n) - 1) + mod) % mod;
      
    // computing modulo inverse 
    // of denomenator
    long den = (power(k - 1, mod - 2) + mod) % mod;
      
    // final result
    long ans = (((num * den) % mod) * k) % mod;
      
    return ans;
}
  
// Driver code
public static void Main()
{
    long n = 3, k = 3;
  
    Console.WriteLine(minPlayer(n, k));
}
}
  
// This code is contributed
// by Shashank

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum players 
// required to win the game anyhow 
  
// function to calculate (a^b)%(10^9+7). 
function power($a, $b
    $mod = 1000000007; 
    $res = 1; 
    while ($b
    
        if ($b & 1)
        
            $res *= $a
            $res %= $mod
        
        $b /= 2; 
        $a *= $a
        $a %= $mod
    
    return $res
  
// function to find the minimum
// required player 
function minPlayer($n, $k
    $mod =1000000007; 
      
    // computing the nenomenator 
    $num = ((power($k, $n) - 1) + 
                   $mod) % $mod
  
    // computing modulo inverse
    // of denomenator 
    $den = (power($k - 1, $mod - 2) + 
                  $mod) % $mod
  
    // final result 
    $ans = ((($num * $den) % $mod) * 
                       $k) % $mod
  
    return $ans
  
// Driver code 
$n = 3;
$k = 3; 
  
echo minPlayer($n, $k); 
  
// This code is contributed
// by Shivi_Aggarwal
?>

chevron_right


Output:

39

Time Complexity: O(log(n))



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.