给定N个问题,每个问题有K个选项,其中和 。任务是确定所有尝试第i个问题的玩家总数无论如何要赢得比赛。您必须最小化播放器总数的总和,并以10 9 +7为模输出。
注意:任何错误答案都会导致玩家被淘汰。
例子:
Input: N = 3, K = 3
Output: 39
Input: N = 5, K = 2
Output: 62
方法:
- 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.
因此,我们的问题简化为找到GP项之和K + K 2 +…+ K N等于 。
现在我们可以使用费马小定理以10 9 +7的形式获得所需的答案模。
下面是上述方法的实现:
C++
// C++ program to find minimum players
// required to win the game anyhow
#include
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;
}
Java
//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));
}
}
Python 3
# 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
C#
// 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
PHP
输出:
39
时间复杂度: O(log(n))