[AtCoderContest010D]Decrementing
[AtCoderContest010D]Decrementing
试题描述
There are \(N\) integers written on a blackboard. The \(i\)-th integer is \(A_i\), and the greatest common divisor of these integers is \(1\).
Takahashi and Aoki will play a game using these integers. In this game, starting from Takahashi the two player alternately perform the following operation:
Select one integer on the blackboard that is not less than \(2\), and subtract \(1\) from the integer.
Then, divide all the integers on the black board by \(g\), where \(g\) is the greatest common divisor of the integers written on the blackboard.
The player who is left with only \(1\)s on the blackboard and thus cannot perform the operation, loses the game. Assuming that both players play optimally, determine the winner of the game.
两个人玩游戏,他们轮流操作一个初始时最大公约数为 \(1\) 的序列,一次操作是将一个数减 \(1\),然后所有数除以它们的最大公约数,最终无法操作者输,问是否先手必胜。
输入
The input is given from Standard Input in the following format:
N
A_1 A_2 … A_N
输出
If Takahashi will win, print First
. If Aoki will win, print Second
.
输入示例1
3
3 6 7
输出示例1
First
输入示例2
4
1 2 4 8
输出示例2
First
输入示例3
5
7 8 8 8 8
输出示例3
Second
数据规模及约定
\(1 \le N \le 10^5\)
\(1 \le Ai \le 10^9\)
The greatest common divisor of the integers from \(A_1\) through \(A_N\) is \(1\).
题解
可能考虑奇偶性是一类博弈问题的思路吧。
首先,如果开始时全是奇数,注意到每次操作不会改变数的奇偶性,所以先手一定会将一个奇数变成偶数,那么后手就可以将这个偶数变回奇数,直到最终都变成了 \(1\)(全是奇数),所以后手必胜。
类似地,可以推广出:奇数个偶数,先手必胜;偶数个偶数,后手必胜。
但是有一个小 bug,如果开始时只有一个奇数,那么就不一定了,因为先手可能将那个奇数变成偶数,然后除以一下公约数,变成一个新局面。
当然这种情况很好处理,直接暴力模拟,直到“只有一个奇数”的局面消失,或者变成了全 \(1\) 序列,模拟次数不会超过 \(log_2max\{A_i\}\)。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
#define maxn 100010
int n, ceven, A[maxn];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
int sum = 0; bool has1 = 0, cur = 0;
n = read();
for(int i = 1; i <= n; i++) A[i] = read(), ceven += !(A[i] & 1), has1 |= (A[i] == 1), (sum += A[i] - 1) &= 1;
if(has1) return puts(sum ? "First" : "Second"), 0;
if(ceven & 1) return puts("First"), 0;
if(n - ceven > 1) return puts("Second"), 0;
for(; ;) {
cur ^= 1;
for(int i = 1; i <= n; i++) if(A[i] & 1) A[i]--;
int g = A[1];
for(int i = 2; i <= n; i++) g = gcd(g, A[i]);
ceven = sum = has1 = 0;
for(int i = 1; i <= n; i++) A[i] /= g, ceven += !(A[i] & 1), has1 |= (A[i] == 1), (sum += A[i] - 1) &= 1;
if(has1) return puts((sum ^ cur) ? "First" : "Second"), 0;
if(n - ceven > 1) return puts(((ceven & 1) ^ cur) ? "First" : "Second"), 0;
}
return 0;
}
[AtCoderContest010D]Decrementing的更多相关文章
- AGC010 - D: Decrementing
原题链接 题意简述 给出一个个数的序列,足够聪明的AB两人轮流进行以下操作: 令一个大于1的数减1,然后所有数除以. 如果一个人不能操作了,那么他就输了. 输入保证所有数都是正整数并且. 分析 这是一 ...
- Agc010_D Decrementing
今天本人因调了上篇博客的题而脑壳不适,不想颓题,因此有了这篇博客. 但是博客毕竟得讲点什么,想想有没有什么代码短的. 哦,好像有,就Agc010_D Decrementing好了. Alice和Bob ...
- AGC 010D.Decrementing(博弈)
题目链接 \(Description\) 给定\(n\)个数\(A_i\),且这\(n\)个数的\(GCD\)为\(1\).两个人轮流进行如下操作: 选择一个\(>1\)的数使它\(-1\). ...
- 【AGC010D】Decrementing
Solution 日常博弈论做不出来. 首先,数值全部为1的局面先手必败. 在接下来的过程中,我们只关注那些大于1的数值. 按照官方题解的思路,首先想一个简化版的问题:没有除的操作,其余相同.那么局面 ...
- AtCoder Grand Contest 010 D - Decrementing
题目描述 有n个整数,其中第i个数为Ai.这些数字的gcd为1.两人轮流操作,每次操作把一个大于1的数减1,并把所有数除以所有数的最大公约数,最后无法操作者输,求是否先手必胜. 如果当前的sum为偶数 ...
- AT2305-[AGC010D]Decrementing【博弈论】
正题 题目链接:https://www.luogu.com.cn/problem/AT2305 题目大意 \(n\)个数字两个人进行博弈,每个人的操作为 选择一个大于1的数字减一 之后所有数字除以所有 ...
- ExtJS 4.2 Date组件扩展:添加清除按钮
ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- How to step through your code in chrome
By executing code one line or one function at a time, you can observe changes in the data and in the ...
随机推荐
- redis的一些问题总结,转载自infoq
Redis是时下比较流行的Nosql技术.在优酷我们使用Redis Cluster构建了一套内存存储系统,项目代号蓝鲸.到目前为止集群有700+节点,即将达到作者推荐的最大集群规模1000节点.集群从 ...
- 苹果市值破万亿,iPhone 会涨价吗?
今日导读 苹果教父乔布斯曾经说过:“活着就是为了改变世界.”虽然他在 56 岁时就遗憾离世,但他极具创新和变革的精神早已深埋进苹果公司的企业文化里,影响着一代又一代的人.就在最近,这家一直努力“改变世 ...
- centos Chrony设置服务器集群同步时间
Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP)同步,让时间始终 ...
- iOS--获取文件目录的方法
很多文章都有写这个问题,我只是为了记录一下,免得总翻书... 1.Documents 目录: 你应该将所有的应用程序数据文件写入到这个目录下.这个目录用于存储用户数据或其它应该定期备份的信息. 2.L ...
- iOS应用架构谈-part2 view层的组织和调用方案
前言 <iOS应用架构谈 开篇>出来之后,很多人来催我赶紧出第二篇.这一篇文章出得相当艰难,因为公司里的破事儿特别多,我自己又有点私事儿,以至于能用来写博客的时间不够充分. 现在好啦,第二 ...
- matplotlib subplot 子图
总括 MATLAB和pyplot有当前的图形(figure)和当前的轴(axes)的概念,所有的作图命令都是对当前的对象作用.可以通过gca()获得当前的axes(轴),通过gcf()获得当前的图形( ...
- 强制类型转换(int)、(int&)和(int*)的区别
我们先来看两行代码: float x=1.75,y=1.75; cout<<(int)x<<" "<<(int&)y<<en ...
- 【AC自动机】bzoj4327: JSOI2012 玄武密码
题目思路没话讲:主要是做题时候的细节和经验 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中 ...
- VMware虚拟网卡介绍和使用说明
介绍VMware三种网络连接模式的详细配置及规则 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/p/9455391 ...
- 使用Spring MVC后实现一个BaseController
使用Spring MVC技术后,可以实现一个基类的Controller类来分装一些MVC常用的方法,其他的Controller都继承自这个BaseController,这样在使用常用的方法时将会变得非 ...