UVA10490 Mr. Azad and his Son!!!!!【筛选法】

There are a lot of Abul Kalam Azad in Bangladesh. But, why is he so special? Not that he is my dad is the only reason. He can wonderfully do some calculation. If anyone gives him any positive integer k, he amazingly can say the relative perfect number 2k−1(2k − 1) without using neither calculator nor computer. Say, I have told him to find out the relative perfect number of 2, he replies 6 which is a perfect number. But perfect is not possible for all the integers. I have asked him the process, but he says that I should find this thing out by myself how an integer is related to a perfect number. Anyway, I have challenged him that it is very possible for me to do the same calculation using a computer. Although I could not figure out how he can do this, I know that the next ACM Online Programming Contest is near at hand and World’s top programmers are available to solve my very simple problem.
    Now, you are to write a program for me to win over my dad, which will take input n, and determine the perfect number p.
Input
An integer 1 < n ≤ 31 is given in each input line.
Output
Output will be in the following format:
• If perfect number is possible
Perfect: p!
• If perfect number is not possible, but given number is prime
Given number is prime. But, NO perfect number is available.
• If perfect number is not possible and given number is not prime
Given number is NOT prime! NO perfect number is available.
Sample Input
2
3
6
0
Sample Output
Perfect: 6!
Perfect: 28!
Given number is NOT prime! NO perfect number is available.

问题链接UVA10490 Mr. Azad and his Son!!!
问题简述:(略)
问题分析:有关解释参见参考链接的第1个,牛人的解法参见参考链接的第2个。
程序说明:(略)
参考链接
UVa 10490 - Mr. Azad and his Son!!!
UVa10490 - Mr. Azad and his Son!!!
题记:(略)

AC的C++语言程序如下:

/* UVA10490 Mr. Azad and his Son!!!!! */

#include <bits/stdc++.h>

using namespace std;

const int N2 = 31;
const int N = 50000;
bool isprime[N + 1];
int prime[N / 3], pcnt = 0;
int perfect[N2 + 1], pfcnt = 1;
// 欧拉筛
void eulersieve(void)
{
    memset(isprime, true, sizeof(isprime));

    isprime[0] = isprime[1] = false;
    for(int i = 2; i <= N; i++) {
        if(isprime[i])
            prime[pcnt++] = i;
        for(int j = 0; j < pcnt && i * prime[j] <= N; j++) {  //筛选
            isprime[i * prime[j]] = false;
            if(i % prime[j] == 0) break;
        }
    }

    memset(perfect, 0, sizeof(perfect));
    for (int i = 2 ; i <= N2 ; i++) {
        int v = (1 << i) - 1, cnt = 0;
        for (int j = 0 ; j < pcnt ; j++)
            while (v % prime[j] == 0) {
                v /= prime[j];
                cnt++;
            }
        if (v != 1) cnt++;
        if (cnt == 1) perfect[i] = 1;
    }
}

int main()
{
    eulersieve();

    int n;
    while (~scanf("%d", &n) && n)
        if (perfect[n])
            printf("Perfect: %lld!\n",(1LL << (n - 1))*((1LL << n) -1LL));
        else if (isprime[n])
            printf("Given number is prime. But, NO perfect number is available.\n");
        else printf("Given number is NOT prime! NO perfect number is available.\n");

    return 0;
}
原创文章 2323 获赞 2382 访问量 269万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105695125