poj3292 Semi-prime H-numbers
来源:互联网 发布:《算法》pdf 编辑:程序博客网 时间:2024/06/09 15:35
Description
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.
Input
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.
Output
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 857890
Sample Output
21 085 5789 62
题意:在这里我们认为只有4n+1的数就是5...9...13....4n+1,然后我们规定有3类数:
1.H-prim 顾名思义,就是在这个系统里面的质数,因子没有别的4n+1的数。注意是在这个4n+1系统里的而不是整个实数哦~
2.H-semi-prim两个素数的乘积,注意!是准确的两个素数的乘积,像125可能有3个就不对了。
3.其余的那些数就是H-composites~了。
思路:类似素数筛的方法,把1-1000001的素数筛出来,然后依次枚举两个的乘积,用一个数组加标记,最后累加就可以辣~
其余的也没什么好说的。
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int MAXN=1000000+10;bool prim[MAXN];int primnum[MAXN];int ans[MAXN];int ha[MAXN];int hcnt;void get_prim(){ int i,j; hcnt=0; for(i=5; i<=1000001; i+=4) { if(prim[i])continue; primnum[hcnt++]=i; for(j=i; j<=1000001/i; j+=4)prim[i*j]=1; }}void get_ans(){ int i,j; for(i=0; i<hcnt; ++i) { for(j=i; j<hcnt; ++j) { long long x=(long long)primnum[i]*primnum[j]; if(x>1000001)break; ha[x]++; } } int cnt=0; for(i=5;i<=1000001;i+=4) { if(ha[i])ans[i]=++cnt; else ans[i]=cnt; }}int main(){ int n; get_prim(); get_ans(); while(~scanf("%d",&n)&&n) { printf("%d %d\n",n,ans[n]); } return 0;}
还有一种做法就是,整体来筛选。(看起来就像是直接暴力枚举两个数的乘积)
因为这个系统无非就是3种数:素数,我们要找的数,和那个其余数。
不难想,任何数都可以最终分解成n个素数乘积的形式。所以我们可以这样做:
起始全标记为0(默认为素数),依次枚举两个的乘积,如果是两个素数的乘积(我们要找的数)就标记上1,否则就标记上个别的(其余数)。
最后累加1的就可以了~
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int MAXN=1000000+10;int prim[MAXN];int ans[MAXN];int ha[MAXN];int hcnt;void get_prim(){ int i,j; hcnt=0; for(i=5; i<=sqrt(1000001); i+=4)//其实思想依旧是欧拉筛的思想 { for(j=i; j<=1000001/i; j+=4) { if(!prim[i]&&!prim[j])prim[i*j]=1; else prim[i*j]=-1; } }}void get_ans(){ int i; int cnt=0; for(i=5; i<=1000001; i+=4) { if(prim[i]==1)ans[i]=++cnt; else ans[i]=cnt; } /* for(i=5; i<=10001; ++i) { if(prim[i]==1) printf("%d ",i); }*/}int main(){ int n; get_prim(); get_ans(); while(~scanf("%d",&n)&&n) { printf("%d %d\n",n,ans[n]); } return 0;}
- POJ3292 Semi-prime H-numbers
- POJ3292--Semi-prime H-numbers
- poj3292 Semi-prime H-numbers
- poj3292 Semi-prime H-numbers
- POJ3292 Semi-prime H-numbers
- poj3292 Semi-prime H-numbers
- poj3292 Semi-prime H-numbers
- [POJ3292]Semi-prime H-numbers
- POJ3292 Semi-prime H-numbers 素数筛选
- POJ3292——Semi-prime H-numbers
- POJ3292——Semi-prime H-numbers
- Semi-prime H-numbers poj3292 (类比欧拉筛)
- POJ3292 UVA11105 Semi-prime H-numbers【筛法打表】
- poj3292--Semi-prime H-numbers(数论篇2)
- poj3292——Semi-prime H-numbers(数论)
- Semi-prime H-number(poj3292)
- (POJ3292)Semi-prime H-numbers <素数筛法的变形>
- pku3292 Semi-prime H-numbers
- JZOJ.4714【NOIP2016提高A组模拟8.19】公约数
- Rails中的config.assets.debug配置项
- 找到图中最小有向圈的一个时间复杂度为O(mn)的算法
- 写一个程序,从文件(c:\test.txt)中查出字符串”mobnet”出现的次数?
- 数据对接中的安全性保证及接口设计
- poj3292 Semi-prime H-numbers
- 树-堆结构练习——合并果子之哈夫曼树
- 利用pair,sort,unique去重
- C语言再学习 -- 声明与定义
- Android 存储数据的五种方法总结。。。。。。。
- 制作自己的IOC框架
- zabbix系列(七)zabbix3.0添加对tcp连接数及状态的监控
- AVL树(平衡二叉树)及其实现
- springmvc+mybatis+ehcache配置详解