湖南大学ACM程序设计新生杯大赛(同步赛)D - Number
题目描述
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:
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 <cstdio> #include <algorithm> using namespace std; const int maxn = 50000000; int a[1200000 + 10]; int sz = 0, cnt = 0; int b[1200000 + 10]; bool noprime[maxn + 10]; int n; void init() { noprime[1] = 1; for(int i = 2; i <= maxn; i ++) { if(noprime[i]) continue; for(int j = i + i; j <= maxn; j = j + i) { noprime[j] = 1; } } for(int i = 1; i * i <= maxn; i ++) { if(noprime[i]) continue; for(int j = 1; i * i + j * j * j <= maxn; j ++) { if(noprime[j]) continue; for(int k = 1; i * i + j * j * j + k * k * k * k <= maxn; k ++) { if(noprime[k]) continue; a[sz ++] = i * i + j * j * j + k * k * k * k; } } } sort(a, a + sz); b[cnt ++] = a[0]; for(int i = 1; i < sz; i ++) { if(a[i] == a[i - 1]) continue; b[cnt ++] = a[i]; } } int main() { init(); while(~scanf("%d", &n)) { int L = 0, R = cnt - 1, pos = -1; while(L <= R) { int mid = (L + R) / 2; if(b[mid] <= n) pos = mid, L = mid + 1; else R = mid - 1; } printf("%d\n", pos + 1); } return 0; }