ACdream 1112 Alice and Bob (sg函数的变形+素数筛)
题意:有N个数,Alice 和 Bob 轮流对这些数进行操作,若一个数 n=a*b且a>1,b>1,可以将该数变成 a 和 b 两个数;
或者可以减少为a或b,Alice先,问谁能赢
思路:首先单看对每个数进行除法的操作,我们可以知道其实是在除以每个数的素因子或素因子之间的积
比如 70=2*5*7 我们可以变成 10(2*5)或 14(2*7) 或 35(5*7)或 2 或 5 或 7 或 1 这七种状态
当我们把他们(2,5,7)当作3个石子也就是一堆时,然而实际上我们是将这堆石子进行nim游戏
我拿走一个石子 =》 10(2*5) 我拿走了石子7
14 (2*7) 我拿走了石子5
35 (5*7) 我拿走了石子2
我拿走两个石子 =》 2 我拿走了石子5 和 石子7
5 我拿走了石子2 和 石子7
7 我拿走了石子2 和 石子5
我拿走三个石子 =》 1 我拿走了石子2 和 石子5 和 石子7
接下来我们分析把一个数n=a*b变成 a 和 b ,其实这里上面的思想很像,把它当作石子的分堆
我可以分成 第一种 10(2*5) 和 7
第二种 14(2*7) 和 5
第三种 35(5*7) 和 2
综上所诉,根据正整数唯一分解定理,任何一个正整数x必然有x=(p1^r1)*(p2^r2)*......*(pn^rn)
定义sum=r1+r2+...+rn,这个sum的值就是这堆石子的总数,那么sg=sg[sum1]^sg[susm2]^....
问题又来了? 这个sum我们应该如何求呢?
我们可以通过素数筛得到每一个数的最小质因子,我们得到一个类似于递推的公式
一个正整数的质因子的个数=(这个正整数 / 这个数的最小质因子 所得数) 的质因子个数 + 1(也就是加上这是最小质因子的数量 1)
接下来代码实现就可以了
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 5000000
using namespace std; int prime[maxn+],k=,samll[maxn],sum[maxn];
bool visit[maxn+];
int sg[]; void get_prime()
{
memset(visit,false,sizeof(visit));
memset(samll,,sizeof(samll));
memset(sum,,sizeof(sum));
for(int i=;i<=maxn;i++)
{
if(visit[i]==false)
{
prime[k++]=i;
for(int j=i+i;j<=maxn;j+=i)
{
visit[j]=true;
if(samll[j]==) samll[j]=i;
}
samll[i]=i;
}
}
for(int i=;i<=maxn;i++)
sum[i]=sum[i/samll[i]]+;
} int get(int n)
{
if(sg[n]!=-) return sg[n];
bool vis[];
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)
vis[get(n-i)]=true;
for(int i=;i<=n/;i++)
vis[get(i)^get(n-i)]=true;
int k;
for(int i=;i<;i++)
{
if(vis[i]==false)
{
return sg[n]=i;
}
}
} int main()
{
get_prime();
memset(sg,-,sizeof(sg));
sg[]=;
sg[]=;
for(int i=;i<=;i++)
{
get(i);
}
int n;
while(cin>>n)
{
int ans=;
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
ans=ans^sg[sum[x]];
}
if(ans)
cout<<"Alice"<<endl;
else
cout<<"Bob"<<endl;
}
return ;
}
ACdream 1112 Alice and Bob (sg函数的变形+素数筛)的更多相关文章
- ACdream 1112 Alice and Bob(素筛+博弈SG函数)
Alice and Bob Time Limit:3000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit ...
- 2014 Super Training #6 A Alice and Bob --SG函数
原题: ZOJ 3666 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 博弈问题. 题意:给你1~N个位置,N是最 ...
- ACdream 1112 Alice and Bob (博弈&&素数筛选优化)
题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x ...
- sg函数的变形 - 可以将一堆石子分开
Nim is a two-player mathematic game of strategy in which players take turns removing objects from di ...
- hdu 3032 Nim or not Nim? sg函数 难度:0
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- ACdream群赛1112(Alice and Bob)
题意:http://acdream.info/problem?pid=1112 Problem Description Here is Alice and Bob again ! Alice and ...
- uoj266[清华集训2016]Alice和Bob又在玩游戏(SG函数)
uoj266[清华集训2016]Alice和Bob又在玩游戏(SG函数) uoj 题解时间 考虑如何求出每棵树(子树)的 $ SG $ . 众所周知一个状态的 $ SG $ 是其后继的 $ mex $ ...
- ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)
ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS Memory Limit:262144KB 64bit IO For ...
- Alice and Bob HDU - 4111 (SG函数)
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The ...
随机推荐
- 如何发布第一个WP8.1程序(VisualStudio2015)
学习WP开发有一段时间了,近一个月开始着手开发程序,并在开发程序中不断地学习(有一定的基础后,边开发程序,边学习是很好的,能练习运用所学的知识,并能在遇到问题后上网上资料不断地学习,很有效果,因为老是 ...
- jquery及原生javascript对jsonp解决跨域问题实例详解
jquery方式 前端: $.ajax({ url: 'http://m.xxx.tv/goLottery', data: { data: data }, type: 'GET', dataType: ...
- Git如何检出指定目录或文件
系统版本:Window 10,Git 版本:2.7.1 对于大型 Git 仓库,每次执行 Git 命令,都需要经过漫长的等待,特别是要经常执行的 git status 命令.下面是一个例子... 从 ...
- c语言二叉树的递归建立
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <malloc.h&g ...
- Digital Tutors - Introduction to Scripting Shaders in Unity 学习笔记
1. Overview 2. Understanding Shader definition:code that define what the material can do in the envi ...
- html基础知识1(基本标签)2017-03-07
摘要:php基础知识1 内容:大学中虽有接触,却是以学生的心态去应付考试的,学的都是理论知识:从今天开始我同样还是要以学生的心态去学习,但却要以要从事工作的心态去练习. 以下为第一天所学内容,因电脑原 ...
- Redis 安装与简单使用
安装 Redis 一般系统都会有软件管理工具,但是通常版本都不会太新,况且 Redis 的安装很简单,因此下面使用源码的安装方式. 下载源码 wget http://download.redis.io ...
- 3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二
3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved ...
- 最大化最小值 Aggressive cows
Aggressive cows http://poj.org/problem?id=2456 N间小屋,M头牛,使得牛跟牛之间的距离最远,以防止牛打架. 2<=N<=100000 2< ...
- 3-15 JS基础知识02
一.For循环: For (var i = 0; i <= 10; i++){ 循环体: } 注意:For循环中的表达式是可以省略的,省略以后是个死循环. odd:奇数 even : 偶 ...