题目链接

Alice and Bob

Time Limit: 6000/3000MS (Java/Others)Memory Limit: 256000/128000KB (Java/Others)

Problem Description

Here  is Alice and Bob again !

Alice and Bob are playing a game. There are several numbers.First, Alice choose a number n.Then he can replace n (n > 1)with one of its positive factor but not itself or he can replace n with a and b.Here a*b = n and a > 1 and b > 1.For example, Alice can replace 6 with 2 or 3 or (2, 3).But he can’t replace 6 with 6 or (1, 6). But you can replace 6 with 1. After Alice’s turn, it’s Bob’s turn.Alice and Bob take turns to do so.Who can’t do any replace lose the game.

Alice and Bob are both clever enough. Who is the winner?

Input

This problem contains multiple test cases. The first line contains one number n(1 <= n <= 100000).

The second line contains n numbers.

All the numbers are positive and less than of equal to 5000000.

Output

For each test case, if Alice can win, output “Alice”, otherwise output “Bob”.

Sample Input

2
2 2
3
2 2 4

Sample Output

Bob
Alice

Source

yehuijie

Manager

给出n个数,两个轮流任选一个数n进行操作,第一种操作是将该数变为他的一个因子,但是不能变为本身。
第二种是变成两个数a和b,要满足a * b = n。a>1, b>1.
第一次写线性筛,这个算法和普通的筛法略有不同,也更不好理解一点,就是说算法保证每个数只会被
他的最小的质因子筛去。也保证了算法的时间是线性的。
Accepted Code:
/*************************************************************************
> File Name: 1112.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月05日 星期五 11时35分09秒
> Propose:
************************************************************************/
#include <set>
#include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int MAX_N = ;
int n, pnum, p[MAX_N], mindiv[MAX_N], cnt[MAX_N];
bool vis[MAX_N]; //线性筛,可以很方便的保存每个数最小的质因子,
//和每个数不同质因子的个数
void get_prime(int n) {
pnum = ; vis[] = true; cnt[] = ;
for (int i = ; i <= n; i++) {
if (!vis[i]) {
p[pnum++] = i; mindiv[i] = i; cnt[i] = ;
}
for (int j = ; j < pnum; j++) {
if (p[j] * i > n) break;
vis[p[j] * i] = true;
mindiv[p[j] * i] = p[j];
cnt[p[j] * i] = cnt[i] + ;
if (i % p[j] == ) break;
}
}
} //记忆化搜索所用数组,初始化为-1
int sg[];
int dfs(int n) {
if (sg[n] != -) return sg[n]; set<int> S;
//第一种转移变为因子a
for (int i = ; i < n; i++) S.insert(dfs(i));
//第二种转移变为两个因子a * b
for (int i = ; i < n; i++) S.insert(dfs(i)^dfs(n - i)); int g = ;
while (S.find(g) != S.end()) g++;
return sg[n] = g;
} void get_SG() {
get_prime(); memset(sg, -, sizeof(sg));
sg[] = ;
for (int i = ; i <= ; i++) {
sg[i] = dfs(i);
}
} int main(void) {
get_SG(); //预处理sg值
int n;
while (~scanf("%d", &n)) {
int ans = ;
for (int i = ; i < n; i++) {
int x;
scanf("%d", &x);
ans ^= sg[cnt[x]];
}
if (ans) puts("Alice");
else puts("Bob");
}
return ;
}

最新文章

  1. 【开源】OSharp框架解说系列(5.2):EntityFramework数据层实现
  2. Leetcode-283 Move Zeroes
  3. Free Slideshow, Gallery And Lightboxes Scripts
  4. UVALive 6523 Languages
  5. hdu1058丑数(优先队列、暴力打表)
  6. mysql中存不进去json_encode格式的数据
  7. 软件工程随堂小作业——(C++)
  8. 【转】多文件目录下makefile文件递归执行编译所有c文件
  9. Android06-Fragment碎片
  10. linux kernel的函数与抽象层
  11. DOM attributes and properties
  12. Ubuntu上搭建Hadoop环境(单机模式+伪分布模式)
  13. Hadoop2动态调整Log级别-以datanode的heartbeat log为例
  14. DDD - 概述 - 模块 (二)
  15. web前端自动化测试/爬虫利器puppeteer介绍
  16. day21(1)---python的内存管理
  17. HDU 5968(异或计算 暴力)
  18. python学习第二次笔记
  19. anaconda3/lib/libcrypto.so.1.0.0: no version information available (required by wget)
  20. formidable模块的使用

热门文章

  1. Android开发 使用SparseArray代替HashMap[转载]
  2. 筛法求欧拉函数(poj2478
  3. 异常处理记录: Unable to compile class for JSP
  4. 第十一章 Odoo 12开发之看板视图和用户端 QWeb
  5. 莫烦PyTorch学习笔记(六)——批处理
  6. Installer - 使用Maven自动布署至外部Tomcat
  7. wifi共享大师,去除弹窗广告。
  8. Activiti 变量设置
  9. 如何将Map键值的下划线转为驼峰
  10. 吴恩达《机器学习》课程总结(18)_照片OCR