📌  相关文章
📜  最大的奇数除数游戏来检查哪个玩家获胜

📅  最后修改于: 2021-09-24 05:01:25             🧑  作者: Mango

两个玩家正在玩以数字 n开头的游戏。在每一回合中,玩家可以进行以下任一动作:

  • 将 n 除以任何大于 1 的奇数除数。数的除数包括数本身。
  • 如果 n > k 其中 k < n,则从 n 中减去 1。

玩家 1 进行主要移动,如果玩家 1 获胜,则打印“是”,否则如果双方都发挥最佳,则打印“否”。无法移动的玩家输掉游戏。
例子:

做法:思路是针对以下3种情况来分析问题:

  • 当整数n 为奇数时,玩家 1 可以将 n 自除,因为它是奇数,因此 n / n = 1,玩家 2 输了。请注意,此处 n = 1 是一个例外。
  • 当整数n 是偶数并且没有大于 1 的奇数除数时, n 的形式为 2 x 。玩家 1 必须将其减 1,使 n 为奇数。因此,如果 x > 1,则玩家 2 获胜。请注意,对于 x = 1,n – 1 等于 1,因此玩家 1 获胜。
  • 整数 n 是偶数并且有奇数除数时,任务仍然是检查 n 是否可以被 4 整除,然后玩家 1 可以将 n 除以其最大的奇数因子,之后 n 变为 2 x的形式,其中 x > 1,因此玩家 1赢。
  • 否则, n 必须是2 * p形式,其中 p 是奇数。如果p 是素数,则玩家 1 输了,因为他可以将 n 减 1 或将其除以 p,这两种方法对他来说都是失败的。如果p 不是素数,那么 p 必须是 p1 * p2 的形式,其中 p1 是素数,p2 是任何大于 1 的奇数,对于这种情况,玩家 1 可以通过将 n 除以 p2 来获胜。

下面是上述方法的实现:

C++
// C++ implementation to find the
// Largest Odd Divisor Game to
// check which player wins
#include 
using namespace std;
 
// Function to find the
// Largest Odd Divisor Game to
// check which player wins
void findWinner(int n, int k)
{
    int cnt = 0;
 
    // Check if n == 1 then
    // player 2 will win
    if (n == 1)
        cout << "No" << endl;
 
    // Check if n == 2 or n is odd
    else if ((n & 1) or n == 2)
        cout << "Yes" << endl;
 
    else {
        int tmp = n;
        int val = 1;
 
        // While n is greater than k and
        // divisible by 2 keep
        // incrementing tha val
        while (tmp > k and tmp % 2 == 0) {
            tmp /= 2;
            val *= 2;
        }
 
        // Loop to find greatest
        // odd divisor
        for (int i = 3; i <= sqrt(tmp); i++) {
            while (tmp % i == 0) {
                cnt++;
                tmp /= i;
            }
        }
        if (tmp > 1)
            cnt++;
 
        // Check if n is a power of 2
        if (val == n)
            cout << "No" << endl;
 
        else if (n / tmp == 2 and cnt == 1)
            cout << "No" << endl;
 
        // Check if cnt is not one
        // then player 1 wins
        else
            cout << "Yes" << endl;
    }
}
 
// Driver code
int main()
{
    long long n = 1, k = 1;
    findWinner(n, k);
    return 0;
}


Java
// Java implementation to find the
// Largest Odd Divisior Game to
// check which player wins
import java.util.*;
 
class GFG{
     
// Function to find the
// Largest Odd Divisior Game to
// check which player wins
public static void findWinner(int n, int k)
{
    int cnt = 0;
 
    // Check if n == 1 then
    // player 2 will win
    if (n == 1)
        System.out.println("No");
     
    // Check if n == 2 or n is odd
    else if ((n & 1) != 0 || n == 2)
        System.out.println("Yes");
 
    else
    {
        int tmp = n;
        int val = 1;
 
        // While n is greater than k and
        // divisible by 2 keep
        // incrementing tha val
        while (tmp > k && tmp % 2 == 0)
        {
            tmp /= 2;
            val *= 2;
        }
 
        // Loop to find greatest
        // odd divisor
        for(int i = 3;
                i <= Math.sqrt(tmp); i++)
        {
           while (tmp % i == 0)
           {
               cnt++;
               tmp /= i;
           }
        }
        if (tmp > 1)
            cnt++;
 
        // Check if n is a power of 2
        if (val == n)
            System.out.println("No");
 
        else if (n / tmp == 2 && cnt == 1)
            System.out.println("No");
 
        // Check if cnt is not one
        // then player 1 wins
        else
            System.out.println("Yes");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 1, k = 1;
     
    findWinner(n, k);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 implementation to find 
# the Largest Odd Divisor Game
# to check which player wins
import math 
 
# Function to find the Largest
# Odd Divisor Game to check
# which player wins
def findWinner(n, k):
     
    cnt = 0;
 
    # Check if n == 1 then
    # player 2 will win
    if (n == 1):
        print("No");
 
    # Check if n == 2 or n is odd
    elif ((n & 1) or n == 2):
        print("Yes");
 
    else:
        tmp = n;
        val = 1;
 
        # While n is greater than k and
        # divisible by 2 keep
        # incrementing tha val
        while (tmp > k and tmp % 2 == 0):
            tmp //= 2;
            val *= 2;
             
        # Loop to find greatest
        # odd divisor
        for i in range(3, int(math.sqrt(tmp)) + 1):
            while (tmp % i == 0):
                cnt += 1;
                tmp //= i;
         
        if (tmp > 1):
            cnt += 1;
 
        # Check if n is a power of 2
        if (val == n):
            print("No");
 
        elif (n / tmp == 2 and cnt == 1):
            print("No");
 
        # Check if cnt is not one
        # then player 1 wins
        else:
            print("Yes");
             
# Driver code
if __name__ == "__main__":
 
    n = 1; k = 1;
     
    findWinner(n, k);
 
# This code is contributed by AnkitRai01


C#
// C# implementation to find the
// Largest Odd Divisior Game to
// check which player wins
using System;
  
class GFG{
      
// Function to find the
// Largest Odd Divisior Game to
// check which player wins
public static void findWinner(int n, int k)
{
    int cnt = 0;
  
    // Check if n == 1 then
    // player 2 will win
    if (n == 1)
        Console.Write("No");
      
    // Check if n == 2 or n is odd
    else if ((n & 1) != 0 || n == 2)
        Console.Write("Yes");
  
    else
    {
        int tmp = n;
        int val = 1;
  
        // While n is greater than k and
        // divisible by 2 keep
        // incrementing tha val
        while (tmp > k && tmp % 2 == 0)
        {
            tmp /= 2;
            val *= 2;
        }
  
        // Loop to find greatest
        // odd divisor
        for(int i = 3;
                i <= Math.Sqrt(tmp); i++)
        {
           while (tmp % i == 0)
           {
               cnt++;
               tmp /= i;
           }
        }
        if (tmp > 1)
            cnt++;
  
        // Check if n is a power of 2
        if (val == n)
            Console.Write("No");
  
        else if (n / tmp == 2 && cnt == 1)
            Console.Write("No");
  
        // Check if cnt is not one
        // then player 1 wins
        else
            Console.Write("Yes");
    }
}
  
// Driver code
public static void Main(string[] args)
{
    int n = 1, k = 1;
      
    findWinner(n, k);
}
}
// This code is contributed by rutvik_56


Javascript


输出:
No

时间复杂度: O(sqrt(n))
辅助空间: O(1)