4631: 【GESP2406五级】小杨的幸运数字

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:1 解决:1

题目描述

⼩杨认为他的幸运数字应该恰好有两种不同的质因子,例如,$12=2×2×3$ 的质因子有$2,3$ ,恰好为两种不同的质因子,因此 是幸运数字,而$30=2×3×5$ 的质因子有$2,3,5$ ,不符合要求,不为幸运数字。
小杨现在有$n$ 个正整数,他想知道每个正整数是否是他的幸运数字。

输入

第一行包含一个正整数$n$ ,代表正整数个数。
之后$n$ 行,每行一个正整数。

输出

输出$n$ 行,对于每个正整数,如果是幸运数字,输出$1$ ,否则输出$0$ 。

样例输入 复制

3
7
12
30

样例输出 复制

0
1
0

提示

#include<bits/stdc++.h>
using namespace std;
map < int, int > mp;
const int N = 1e5 + 10;
int calc(int x) {
    int res = 0;
    set < int > s;
    for (int i = 2; i * i <= x; i++) {
        if (x % i == 0) {
            s.insert(i);
            while (x % i == 0) {
                x /= i;

            }
        }
    }
    if (x != 1) {
        s.insert(x);
    }
    return (int) s.size();
}
int a[N];
int main() {
    int n;
    cin >> n;
    long long ans = 0;
    int pre = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        int x = calc(a[i]);
        if (x == 2) cout << "1\n";
        else cout << "0\n";
    }

}