You can't possibly imagine how cold our friends are this winter in Nvodsk! Two of them play the following game to warm up: initially a piece of paper has an integer q. During a move a player should write any integer number that is a non-trivial divisor of the last written number. Then he should run this number of circles around the hotel. Let us remind you that a number's divisor is called non-trivial if it is different from one and from the divided number itself.
The first person who can't make a move wins as he continues to lie in his warm bed under three blankets while the other one keeps running. Determine which player wins considering that both players play optimally. If the first player wins, print any winning first move.
The first line contains the only integer q (1 ≤ q ≤ 1013).
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the%I64d specificator.
In the first line print the number of the winning player (1 or 2). If the first player wins then the second line should contain another integer — his first move (if the first player can't even make the first move, print 0). If there are multiple solutions, print any of them.
6
2
30
1 6
1
1 0
Number 6 has only two non-trivial divisors: 2 and 3. It is impossible to make a move after the numbers 2 and 3 are written, so both of them are winning, thus, number 6 is the losing number. A player can make a move and write number 6 after number 30; 6, as we know, is a losing number. Thus, this move will bring us the victory.
将q素因子分解,然后统计素因子的个数,如果素因子个数大于2,那么先手只要拿得只剩下2个给后手,那么先手必胜,因为后手不论怎么拿,先手都不可分了。
如果素因子个数等于2,那么先手必败。
如果素因子个数为1或者q本身为1,那么先手直接取胜。
可以用素数筛先打个[2, sqrt(q)]的表,然后计算素因子个数,对于最后的提取完[2, sqrt(q)]中素因子的q要么为1要么为一个素数,若是素数的话素因子个数还要加一
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 10000003;
ll q, num[10000005], len, cnt;
bool primer[10000005];
int main()
{
cin >> q;
if (q == 1) {
printf("1\n0\n");
return 0;
}
primer[0] = primer[1] = true;
len = 0;
for (ll i = 2; i <= maxn; i++) {
if (!primer[i]) {
num[len++] = i;
for (ll j = 2; j * i <= maxn; j++) {
primer[i * j] = true;
}
}
}
cnt = 0;
ll x = 1, y = 1, tt = q;
for (ll i = 0; i < len; i++) {
if (q % num[i] == 0) {
cnt++;
q /= num[i];
if (cnt == 1) x = num[i];
else if (cnt == 2) y = num[i];
i--;
}
}
//如果最后q没有除尽到1,说明最后q也是个素数
if (q != 1) cnt++;
if (cnt == 1) {
printf("1\n0\n");
} else if (cnt == 2) {
puts("2");
} else {
printf("1\n%I64d\n", x * y);
}
return 0;
}