Goodbye (博弈)

“Goodbye to the friends I’ve had, goodbye to my upstairs neighbor, goodbye to the kids downstairs, and anybody who lend me a favor.”

“Or, goodbye to the positive proper divisor?”

That game is popular these days. Two player take turns to choose a specific number and claim it. The player who takes the first turn chooses one positive proper divisor of given n. In the next rounds, player should choose one positive proper divisor (真因数) of the number claimed in the previous round. The player who can’t make a choice will win.

Formally, a positive proper divisor of n is a divisor of n, and it cannot be either 1 or n.

Now Chino is going to play this game with David. Chino always wants to win and meanwhile she wants to keep the chosen number as big as possible. If with the given n, Chino will win directly or fail anyway, you should also tell her.

Can you help Chino to choose her answer in the first round?

Input
The first line contains one integer T (1≤T≤103) denoting the count of testcases.

For each testcase, one line with one integer n (1≤n≤105) denoting the given n in one game.

Output
For each testcase, you should output the biggest choice, or 0 when Chino will win directly, or −1 when Chino will fail anyway.

Example
Input
4
3
4
8
666
Output
0
-1
4
111

#include <bits/stdc++.h>
using namespace std;
bool vis[100005] = {0};
int prime[100005];
int k = 0;
void get_prime() {                                     //存入素数
    int i, j;
    memset(vis, 0, sizeof(vis));
    vis[1] = 1;
    for (i = 2; i < 100000; i++) {
        if (vis[i] == 0) {
            for (j = i * 2; j < 100000; j += i) {
                vis[j] = 1;
            }
            prime[k++] = i;
        }
    }
}
int main() {
    get_prime();
    int t, n, m, i, j, s, sum, ans;
    int a[100004], b[100004];
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        if (n == 1) {
            printf("0\n");
            continue;
        }
        m = n;
        j = 0;
        sum = 0;
        for (i = 0; i < k; i++) {                    //将n的素数因子存入a[],将对应的因数个数存入b[]
            s = 0;
            while (m % prime[i] == 0) {
                m /= prime[i];
                s++;
                if (m == 1) break;
            }
            if (s) {
                a[j] = prime[i];
                b[j++] = s;
                sum += s;
            }
            if (m == 1) break;
        }
        if (sum < 2)
            printf("0\n");
        else if (sum == 2)
            printf("-1\n");
        else {                                     //使剩余数为两个最大素数因子的乘积,就能必赢,而且值尽可能最大
            if (b[j - 1] == 1)
                ans = a[j - 1] * a[j - 2];
            else
                ans = a[j - 1] * a[j - 1];
            printf("%d\n", ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值