HDU 3826 squarefree Number Theory

Source: Internet
Author: User
#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.