【POJ 3292】 Semi-prime H-numbers

【POJ 3292】 Semi-prime H-numbers

打个表

题意是1 5 9 13...这样的4的n次方+1定义为H-numbers

H-numbers中仅仅由1*自己这一种方式组成 即没有其它因子的 叫做H-prime

两个H-prime的乘积叫做H-semi-prime 另一个要求是H-semi-prime仅仅能由两个H-prime组成 即4个H-number 不可由3个或几个H-number构成

筛出来个满足题意的表 把每一个数内满足的个数存起来O(1)输出就可以

代码例如以下:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int sz = 1000001;

int IsPrim[sz+1];
int p[sz];
int tp;

void Init()
{
    memset(IsPrim,0,sizeof(IsPrim));//H-numbers都初始化0 即默认都为H-prime
    int i,j,cnt;
    tp = 1;
    for(i = 5; i <= sz; i += 4)
    {
        for(j = 5; j*i <= sz; j += 4)
        {
            if(IsPrim[i] || IsPrim[j])//两个数有一个不是H-prime 组合就不为H-semi-prime
                IsPrim[i*j] = -1;
            else IsPrim[i*j] = 1;//否则组合为H-semi-prime 注意 H-semi-prime就不为H-prime了 因为顺序枚举 后面遍历到的之前肯定会推断一下 故不会漏判
        }
    }
    cnt = 0;
    for(i = 1; i <= 1000001; ++i)
    {
        if(IsPrim[i] == 1) cnt++;

        p[tp++] = cnt;
    }
}

int main()
{
    Init();
    int h;
    while(~scanf("%d",&h) && h)
    {
        h = (h-1)/4*4+1;
        printf("%d %d\n",h,p[h]);
    }
    return 0;
}
时间: 2024-10-13 15:11:46

【POJ 3292】 Semi-prime H-numbers的相关文章

【POJ 3252】 Round Numbers

[POJ 3252] Round Numbers 组合数学里的题..在我飞的引导下走上了数位dp的不归路...这样算不算开挂....好羞涩 数位dp真的真的真的好好用!!! 数位dp真的真的真的好好用!!! 数位dp真的真的真的好好用!!! 重要的事说三遍 一入数位dp深似海 再也粗不来了... 用数位很好想--都不知道该怎么写题解... 代码如下: #include <iostream> #include <cstdio> #include <cstring> #de

【POJ 2411】Mondriaan&#39;s Dream(状压dp)

[POJ 2411]Mondriaan's Dream(状压dp) Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14107   Accepted: 8152 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in hi

【POJ 1739】Tony&#39;s Tour

Tony's Tour Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3545   Accepted: 1653 Description A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked.

【POJ 2728】Desert King

Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21361   Accepted: 5974 Description David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his coun

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

【POJ 2750】 Potted Flower(线段树套dp)

[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   Accepted: 1739 Description The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrou

【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)

[POJ 3321] Apple Tree (dfs重标号设区间+树状数组求和) Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21966   Accepted: 6654 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. K

【POJ 1741】Tree

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11570   Accepted: 3626 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an

【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3824 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, an