【LeetCode刷题】NIM游戏:妙解

巴什博奕,n%(m+1)!=0时,先手总是会赢的

来自 <https://leetcode-cn.com/problems/nim-game/comments/>

  • 思路:此题代码很简单,但是思想却不好理解,我们每次拿石头,一共有两种情况,A:石头总数为4的倍数,B:石头总数不为4的倍数,为什么我们会关注4的倍数呢。我们可以采取数学归纳法证明一下(证明对于4的倍数而言不管怎么拿都会得到还是4的倍数从8开始证明),就是因为对于4而言,谁面对4谁就输了,因为不管你拿1,2,3个剩下的都会被一次拿完,而对于2* 4=8个,面对8的时候不管拿x(1,2,3)个另一个人都可以4-x个使得剩下的为4个也就是输了(所以n=8时成立)。我们推广到4k数而言,当第一个人拿x(1,2,3),第二个人就拿4-x所有得到了4k-x+(4-x) = 4*(k-1)也为4的倍数(所以当数为4k而言成立);所以数学归纳法成立,所以对于任意4的倍数而言每次减小4最后都会得到4就代表着遇到4的倍数的人就输了,而对于情况B而言为什么就赢了呢?,因为对于一个不为4的倍数的值4* n>x>4* (n-1),x处于两个4的倍数之间他们之差为4,由于x不为4的倍数,所以,x距离最近的4的倍数值最大为3,所以只要面对到不是4的倍数的值我们一定可以取走(1,2,3)中的某个值导致剩下的值为4的倍数,而面对4倍数的人一定输(已经证明)。所以我们只需要关注4的倍数的值即可。

    class Solution {

    public:

    bool canWinNim(int n) {

    return n%4!=0;// 速度更快的是位运算 (n&3)!=0;

    }

    };

作者:vailing

链接:https://leetcode-cn.com/problems/nim-game/solution/shu-xue-gui-lei-fa-zheng-ming-by-vailing/

来源:力扣(LeetCode)

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/xukaiae86/p/12047322.html

时间: 2024-08-30 06:03:47

【LeetCode刷题】NIM游戏:妙解的相关文章

leetcode 刷题之路 94 N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

leetcode 刷题之路 95 N-Queens I

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. N皇后问题的变种,要求直接输出N皇后的解法数目.这道题可以在N-Queens I的基础上增加计数功能,在每求得一个成功的解时(行数为N时)使计数变量递增即可.题目不要求输出具体的解法,因此可以做一点优化,使用position数组用来表示皇后的位置,p

【leetcode刷题笔记】Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 所谓的anagrams,只若干个词,它们包含的字母的个数和种类完全一样,只是字符的顺序不一样.比如说bus,usb,sub就是一组angrams.同一组angrams具有排序后相同的特点,比如对上述三个单词按字典序排序分别得到bsu,bsu,bsu.我们用这一点

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t