题意:有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函数的变形+素数筛)的更多相关文章

  1. ACdream 1112 Alice and Bob(素筛+博弈SG函数)

    Alice and Bob Time Limit:3000MS     Memory Limit:128000KB     64bit IO Format:%lld & %llu Submit ...

  2. 2014 Super Training #6 A Alice and Bob --SG函数

    原题: ZOJ 3666 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 博弈问题. 题意:给你1~N个位置,N是最 ...

  3. ACdream 1112 Alice and Bob (博弈&amp;&amp;素数筛选优化)

    题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x ...

  4. sg函数的变形 - 可以将一堆石子分开

    Nim is a two-player mathematic game of strategy in which players take turns removing objects from di ...

  5. 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 ...

  6. ACdream群赛1112(Alice and Bob)

    题意:http://acdream.info/problem?pid=1112 Problem Description Here  is Alice and Bob again ! Alice and ...

  7. uoj266[清华集训2016]Alice和Bob又在玩游戏(SG函数)

    uoj266[清华集训2016]Alice和Bob又在玩游戏(SG函数) uoj 题解时间 考虑如何求出每棵树(子树)的 $ SG $ . 众所周知一个状态的 $ SG $ 是其后继的 $ mex $ ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. web性能优化 来自《web全栈工程师的自我修养》

    最近在看<web全栈工程师的自我修养>一书,作者是来自腾讯的前端工程师.作者在做招聘前端的时候问应聘者web新能优化有什么了解和经验,应聘者思索后回答“在发布项目之前压缩css和 Java ...

  2. C#中运算符的应用

    c#中的运算符(只说我自己熟悉和常用的)一.算数运算:加 +减 -乘 *除 / (整型的除法是不算小数点的,也就是说结果是整数,余数舍去了,求余数看下面)求余数 % 加减乘好理解,整型的除法和求余数因 ...

  3. WebSite---前台系统图片验证码心得

    背景: 因为移动端APP和Msite手机注册发送短信验证码没有添加图片验证码功能.公司的短信接口被恶意刷取.所以我们就觉得在移动端添加一个图片验证码功能.分享一下大体实现方式思路.PS demo是自己 ...

  4. How to build mscorlib.dll with visual studio

    Recently, Microsoft Corportation has released a new look for .NET Reference Source. And you may find ...

  5. homebrew常用命令

    安装 ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/Go/install)" 搜索 brew sea ...

  6. win7下使用git

    1 安装git for window 2 安装tortoiseGit 3 生成public key 3.1 打开git bash 3.2 创建~/.ssh文件夹 mkdir ~/.ssh 3.3 配置 ...

  7. swift -- 单例

    方式一: (类似OC) class SingletonDispatch{ class var shareInstance : SingletonDispatch { //结构体 struct Stat ...

  8. 控制语句 for while if switch

    一.for…in 结构 for i in 0...4{ print(i)    //使用到了变量 i } for _ in 0...1{  // 后期没有使用到变量,可以直接用个下划线 _  占位就行 ...

  9. iOS网络编程笔记——GCDAsyncSocket使用

    CocoaAsyncSocket为Mac和iOS提供了易于使用且强大的异步通信库. 简单的Socket通信包括了建连.断开连接.发送socket业务请求.重连这四个基本功能. 1.建立连接:GCDAs ...

  10. C#编写代码:求三个数中的最大数

    static void Main(string[] args)        {            float x, y, z, temp;            Console.Write(&q ...