#include<stdio.h>#include<string.h>#include<math.h>#define nmax 1000001int prime[nmax], plen;void init() {memset(prime, -1, sizeof(prime));int i, j;for (i = 2; i < nmax; i++) {if (prime[i]) {for (j = i + i; j < nmax; j += i) {prime[j] = 0;}}}for (i = 2, plen = 0; i < nmax; i++) {if (prime[i]) {prime[plen++] = i;}}}int solve(long long n) {int i;for (i = 0; (i < plen) && (prime[i] < n); i++) {if (n % prime[i] == 0) {n /= prime[i];if (n % prime[i] == 0) {return 0;}}}return 1;}int main() {#ifndef ONLINE_JUDGEfreopen("t.txt", "r", stdin);#endifinit();int i, t, te, flag;long long n;while (scanf("%d", &t) != EOF) {for (i = 1; i <= t; i++) {scanf("%I64d", &n);flag = solve(n);printf("Case %d: ", i);if (flag) {te = (int) (sqrt(n * 1.0));if (((long long) (te)) * te == n) {printf("No\n");} else {printf("Yes\n");}} else {printf("No\n");}}}return 0;}
The following is reproduced in:
Http://hi.baidu.com/304467594/blog/item/f02407ea9eeecac62f2e2136.html
/*
Determine whether the given N (2 <= n <= 10 ^ 18) is squarefree number;
Because the given number can reach a maximum of 10 ^ 18, direct brute force is definitely a cup of TLE;
10 ^ 18 = 10 ^ 6 ^ 2*10 ^ 6;
*/
The following describes how X-Dragon solves the problem. I skipped step 3;
/*
1. Calculate the prime number below 1000000, stored in prime []
2. Calculate the factor p whose N is less than 1000000. If there is a square factor, no, to 5
Otherwise, n = N/P
3. After removing the factor p whose N is less than 1000000, if n = 1, yes, to 5
4. N must be one of three types: prime number, the square of prime number, and the product of two different prime numbers.
Therefore, determine whether N is the number of workers. If n is the number of workers, no is used. If n is not, yes is used.
5. End
*/
#include<stdio.h>#include<string.h>#include<math.h>#define nmax 1000001int prime[nmax], plen;void init() {memset(prime, -1, sizeof(prime));int i, j;for (i = 2; i < nmax; i++) {if (prime[i]) {for (j = i + i; j < nmax; j += i) {prime[j] = 0;}}}for (i = 2, plen = 0; i < nmax; i++) {if (prime[i]) {prime[plen++] = i;}}}int solve(long long n) {int i;for (i = 0; (i < plen) && (prime[i] < n); i++) {if (n % prime[i] == 0) {n /= prime[i];if (n % prime[i] == 0) {return 0;}}}return 1;}int main() {#ifndef ONLINE_JUDGEfreopen("t.txt", "r", stdin);#endifinit();int i, t, te, flag;long long n;while (scanf("%d", &t) != EOF) {for (i = 1; i <= t; i++) {scanf("%I64d", &n);flag = solve(n);printf("Case %d: ", i);if (flag) {te = (int) (sqrt(n * 1.0));if (((long long) (te)) * te == n) {printf("No\n");} else {printf("Yes\n");}} else {printf("No\n");}}}return 0;}