UVA11005 Semi-prime H-numbers(筛法)

UVA - 11105

Semi-prime H-numbers

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

Problem A: Semi-prime H-numbers

This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 numbers. Here, we do only
a bit of that.

An H-number is a positive number which is one more than a multiple of four: 1, 5, 9, 13, 17, 21,... are the H-numbers. For this problem we pretend that these are the only numbers.
The H-numbers are closed under multiplication.

As with regular integers, we partition the H-numbers into units, H-primes, and H-composites. 1 is the only unit. An H-number h is H-prime
if it is not the unit, and is the product of two H-numbers in only one way: 1 × h. The rest of the numbers are H-composite.

For examples, the first few H-composites are: 5 × 5 = 25, 5 × 9 = 45, 5 × 13 = 65, 9 × 9 = 81, 5 × 17 = 85.

Your task is to count the number of H-semi-primes. An H-semi-prime is an H-number which is the product of exactly two H-primes. The two H-primes
may be equal or different. In the example above, all five numbers are H-semi-primes. 125 = 5 × 5 × 5 is not an H-semi-prime, because it‘s the product of three H-primes.

Each line of input contains an H-number ≤ 1,000,001. The last line of input contains 0 and this line should not be processed.

For each inputted H-number h, print a line stating h and the number of H-semi-primes between 1 and h inclusive, separated
by one space in the format shown in the sample.

Sample input

21
85
789
0

Output for sample input

21 0
85 5
789 62

Don Reble

Source

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: More Advanced Topics :: Problem Decomposition :: Two
Components - Involving DP 1D RSQ/RMQ

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Exercises

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Number Theory :: Exercises:
Beginner

Submit Status

先筛出所有的H素数,然后暴力搞。。。不知道大家求H半素数有没有什么更好的算法呢?虽然程序跑的很快,但是还是想知道是否有更快的。

#include<bits/stdc++.h>
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
bool check[maxn];
int f[maxn];
void init(int n)
{
    memset(check,0,sizeof check);
    vector<int> res;
    for(int i = 5; i <= n; i += 4) {
        if(!check[i])res.push_back(i);
        int sz = res.size();
        for(int j = 0; j < sz; j++) {
            ll t = (ll)i*res[j];
            if(t>n)break;
            check[t] = true;
            if(i%res[j]==0)break;
        }
    }
    memset(f,0,sizeof f);
    int sz = res.size();
    for(int i = 0; i < sz; i++) {
        for(int j = i; j < sz; j++) {
            ll t = (ll)res[i] * res[j];
            if(t>n)break;
            f[t] = 1;
        }
    }
    for(int i = 1; i <= n; i++)f[i] += f[i-1];
}
int main()
{
    int n;
    init(maxn-5);
    while(~scanf("%d",&n)&&n) {
        printf("%d %d\n", n, f[n]);
    }
    return 0;
}
时间: 2024-08-09 01:16:45

UVA11005 Semi-prime H-numbers(筛法)的相关文章

JD 题目1040:Prime Number (筛法求素数)

OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下的数中选择最小的数是素数,然后去掉它的倍数. 依次类推.直到筛子为空时结束. 如有: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1不是素数.去掉.剩下的数中2最小,是素数,去掉2的

POJ-2689 Prime Distance(线性筛法)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17021   Accepted: 4536 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

AOJ - 0009 Prime Number (素数筛法) &amp;&amp; AOJ - 0005 (求最大公约数和最小公倍数)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. 1 /* *********************************************** 2 Author : zch 3 Created Time :2015/5/19 8:46:16 4 File Name :a.cpp 5 ************************************************ */

常见素数筛法

列出几种常用的素数筛选法,附上计时器... #include<cstdio> #include<cstdlib> #include<cmath> #include<map> #include<queue> #include<stack> #include<vector> #include<algorithm> #include<cstring> #include<string> #inc

欧拉筛法(线性筛)素数

#include<bits/stdc++.h> using namespace std; #define maxn 40 int prime[maxn]; int visit[maxn]; void Prime(){//埃氏筛法 memset(visit,0,sizeof(visit)); //初始化都是素数 visit[0] = visit[1] = 1; //0 和 1不是素数 for (int i = 2; i <= maxn; i++) { if (!visit[i]) { //

10001st prime

problem 7:10001st prime 题意:求第10001个质数 代码如下: 1 #ifndef PRO7_H_INCLUDED 2 #define PRO7_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro7{ 7 int solve(){ 8 int p[200005]; 9 getPrime(200000,p); 10 return p[10000]; 11 } 12 } 13 14 #endif // PRO7_H

Largest prime factor

problem 3:Largest prime factor 题意:求600851475143的最大的质因数 代码如下: 1 #ifndef PRO3_H_INCLUDED 2 #define PRO3_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro3{ 7 long long solve(){ 8 long long n=600851475143LL,maxn=0;; 9 for(long long i=1;i*i<=n;

F - Dima and Lisa

Problem description Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes. More formally, you are gi

CSE 216 – Homework

CSE 216 – Homework IThis homework document consists of 3 pages. Carefully read the entire document before you start coding.Note: All functions, unless otherwise specified, should be polymorphic (i.e., they should work with anydata type). For example,

Summation of primes

problem 10:Summation of primes 题意:求不大于200w的素数和 代码如下: 1 #ifndef PRO10_H_INCLUDED 2 #define PRO10_H_INCLUDED 3 4 #include "prime.h" 5 #include <cstring> 6 7 int p[2000000]; 8 bool vis[10000005]; 9 long long solve(){ 10 memset(vis,0,sizeof(vi