题目描述:原题链接
Problem Description
Now it’s time for lunch. Today’s menu is chocolate!
Though every baby likes chocolate, the appetites of babies are little. After lunch, there are still n pieces of chocolate remained: The length of the ith piece is li.
Using the remained chocolate, Baby Volcano is going to play a game with his teacher, Mr. Sprague. The rule of the game is quite simple.
Two player plays in turns, and Baby Volcano will play first:
- In each turn, the player needs to select one piece of chocolate. If the length of the selected piece is equal to 1, the player of this turn will lose immediately.
- Suppose the length of the selected piece is l. Then the player needs to select a positive integer k satisfying k is at least 2 and k is a factor of l.
- Then the player needs to cut the selected piece into k pieces with length lk.
The game continues until one player selects a piece of chocolate with length 1.
Suppose both players plays optimally, your task is to determine whether Baby Volcano will win.
Input
The first line contains single integer t(1≤t≤2∗104), the number of testcases.
For each testcase, the first line contains a single integer n(1≤n≤10).
The second line contains n positive integers li(1≤li≤109), representing the length of each piece.
Output
For each testcase, output char ‘W’ if Baby Volcano will win, otherwise output char ‘L’.
Sample Input
3
2
4 9
2
2 3
3
3 9 27
Sample Output
W
L
L
思路:
转化成普通的Nim游戏,每堆石子个数相当于每根棒(奇质因子个数+是否为偶数)。
对于因子2而言,无论2的幂次是多少,他的贡献只有1。可以这么思考,比如对于12而言,如果我拆成了两堆6,那么无论我在一堆中做什么操作,另外一个人都可以复制我的操作。也就是12与6是等效的,;增加的操作次数都是偶数次,也就是没有交换胜负手。
因为n的范围是1e9所以直接分解质因数会T,所以要先筛出
(
n
)
\sqrt(n)
(n)的素数进行唯一性分解。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N=100010,INF=0x3f3f3f3f;
int primes[N],cnt;
bool st[N];
void get_prime(int n)
{
for(int i=2;i<=n;i++)
{
if(!st[i]) primes[cnt++]=i;
for(int j=0;primes[j]<=n/i;j++)
{
st[primes[j]*i]=true;
if(i%primes[j]==0) break;
}
}
}
int divide(int n)
{
int res=0;
for(int i=0;i<cnt;i++)
{
if(n<primes[i]) break;
if(n%primes[i]==0)
{
int s=0;
while(n%primes[i]==0)
{
n/=primes[i];
s++;
}
if(primes[i]==2) res++;
else res+=s;
}
}
if(n>1) res++;
return res;
}
int main()
{
int T; scanf("%d",&T);
get_prime(50010);
while(T--)
{
int n; scanf("%d",&n);
int res=0;
for(int i=0;i<n;i++)
{
int x; scanf("%d",&x);
res^=divide(x);
}
if(res) puts("W");
else puts("L");
}
return 0;
}