【LeetCode】1025. Divisor Game 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/divisor-game/
题目描述
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number N on the chalkboard. On each player’s turn, that player makes a move consisting of:
- Choosing any
xwith0 < x < NandN % x == 0. - Replacing the number N on the chalkboard with
N - x.
Also, if a player cannot make a move, they lose the game.
Return True if and only if Alice wins the game, assuming both players play optimally.
Example 1:
Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.
Example 2:
Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
Note:
- 1 <= N <= 1000
题目大意
对于数字N,做两个操作:1. 找出一个因数x,2. 把N换成N - x。两个人轮流做这个操作,问第一个人是否能赢。
解题方法
找规律
首先说结论:当N是偶数时第一个人一定赢,当N是奇数时第一个一定输。
- 奇数的因子只有奇数,偶数的因子至少一个偶数2
- 奇数 - 奇数 = 偶数
- 当Alice的值是N时必输,则当Alice的值是N+1时必赢(拿1即可)
那么,当N为下列数字时,先发的状态如下。
当N=1,输;
当N=2,赢(性质3);
当N=3,输(性质1和2,对方一定是偶数,上面的偶数情况都赢);
当N=4,赢(性质3);
当N=5,输(性质1和2,对方一定是偶数,上面的偶数情况都赢);
…
所以,N为偶数都赢,N为奇数都输。
C++代码如下:
class Solution {
public:
bool divisorGame(int N) {
return N % 2 == 0;
}
};
动态规划
动态规划就是很朴素的做法了,对每个位置i,遍历其因数x,判断N-x的状态,当N-x为输的时候自己赢。
C++代码如下:
class Solution {
public:
bool divisorGame(int N) {
if (N == 1) return false;
if (N == 2) return true;
vector<bool> dp(N, false);
dp[1] = true;
for (int i = 3; i <= N; ++i) {
for (int j = 1; j < i; ++j) {
if (i % j == 0 && !dp[i - j - 1]) {
dp[i - 1] = true;
break;
}
}
}
return dp[N - 1];
}
};
参考资料:https://leetcode.com/problems/divisor-game/discuss/368269/C%2B%2B-100-and-96-and
日期
2019 年 8 月 30 日 —— 赶在月底做个题
【LeetCode】1025. Divisor Game 解题报告(C++)的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- R 语言实战-Part 5-1笔记
R 语言实战(第二版) part 5-1 技能拓展 ----------第19章 使用ggplot2进行高级绘图------------------------- #R的四种图形系统: #①base: ...
- 18-Rotate Array-Leetcode
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- C#序号
OnRowCreated="gridViewCorrection_RowCreated" <asp:BoundField HeaderText="序号" ...
- stm32串行设备接口SPI控制max31865
本人是刚入行的嵌入式,之前也没有多少项目经验,故在公司的这几个月里,可谓是如履薄冰,对于公司不同项目使用的不同的设备之多,数据手册之繁杂,让我不禁望洋兴叹,故而不愿意放弃周末这大好的自我提升时间,努力 ...
- Spark On Yarn的各种Bug
今天将代码以Spark On Yarn Cluster的方式提交,遇到了很多很多问题.特地记录一下. 代码通过--master yarn-client提交是没有问题的,但是通过--master yar ...
- Hadoop、Hive【LZO压缩配置和使用】
目录 一.编译 二.相关配置 三.为LZO文件创建索引 四.Hive为LZO文件建立索引 1.hive创建的lzo压缩的分区表 2.给.lzo压缩文件建立索引index 3.读取Lzo文件的注意事项( ...
- 大数据学习day11------hbase_day01----1. zk的监控机制,2动态感知服务上下线案例 3.HDFS-HA的高可用基本的工作原理 4. HDFS-HA的配置详解 5. HBASE(简介,安装,shell客户端,java客户端)
1. ZK的监控机制 1.1 监听数据的变化 (1)监听一次 public class ChangeDataWacher { public static void main(String[] arg ...
- map/multimap深度探索
map/multimap同样以rb_tree为底层结构,同样有元素自动排序的特性,排序的依据为key. 我们无法通过迭代器来更改map/multimap的key值,这个并不是因为rb_tree不允许, ...
- Redis,Memcache,MongoDb的特点与区别
Redis Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支 ...
- List如何一边遍历,一边删除?
1.新手常犯的错误 可能很多新手(包括当年的我,哈哈)第一时间想到的写法是下面这样的: public static void main(String[] args) { List<String& ...