湖南大学ACM程序设计新生杯大赛(同步赛) D -Number 【打表+暴力】

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld
题目描述
We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), and prime fourth power(四次方).
The first four Shuaishuai numbers are:
湖南大学ACM程序设计新生杯大赛(同步赛) D -Number 【打表+暴力】_第1张图片
How many Shuaishuai numbers in [1,n]? (1<=n<=50 000 000)
输入描述:
The input will consist of a integer n.
输出描述:
You should output how many Shuaishuai numbers in [1…n]
示例1
输入

28
输出

1
说明

There is only one Shuaishuai number

分析:素数筛打表,暴力。

#include
#include
#include
#include
#include
#define ll long long int
using namespace std;
const int maxn = 2e6 + 10;
int a[maxn];
int vis[maxn];
int c[maxn];
int n;
int len;
void get_prime() {
    len = 0;
    for (int i = 2; i < maxn; i++)
    {
        if (vis[i])
            continue;
        a[len++] = i;
        for (int j = 2 * i; j < maxn; j += i)
        {
            vis[j] = 1;
        }
    }
}
int main()
{
    memset(vis, 0, sizeof vis);
    memset(c, 0, sizeof c);
    scanf("%d", &n);
    get_prime();
    int ans = 0;
    int cs = 0;
    for (int i = 0; i < len; i++) {
        int tmp1 = a[i] * a[i] * a[i] * a[i];
        if (tmp1 > n) break;
        for (int j = 0; j < len; j++) {
            int tmp2 = a[j] * a[j] * a[j] + tmp1;
            if (tmp2 > n) break;
            for (int k = 0; k < len; k++) {
                int tmp3 =  a[k] * a[k] + tmp2;
                if (tmp3 <= n) { c[cs++] = tmp3; }
                if (tmp3 > n) break;
            }
        }
    }
    sort(c, c + cs);
    if (c[0])
        ans = 1;
    for (int i = 1; i < cs; i++) {
        if (c[i] != c[i - 1]) ans++;
    }
    printf("%d\n", ans);
    return 0;
}

你可能感兴趣的:(Algorithm)