Day7 - I - Semi-prime H-numbers POJ - 3292
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
85
789
0
Sample Output
21 0
85 5
789 62 思路:打表求出H-prime,再两两相乘,用树状数组优化求和问题即可
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e6+; bool prime[maxm];
int vis[maxm];
int jud[maxm], siz = , C[maxm]; void add(int x, int val) {
for(; x < maxm; x += lowbit(x))
C[x] += val;
} LL getsum(int x) {
LL ret = ;
for(; x; x -= lowbit(x))
ret += C[x];
return ret;
} void getHprime() {
for(int i = ; i < maxm; i += ) {
if(!prime[i]) {
for(int j = *i; j < maxm; j += i)
prime[j] = true;
jud[siz++] = i;
for(int k = ; k < siz; ++k) {
if(maxm / i >= jud[k]) {
if(!vis[jud[k] * i]++)
add(i*jud[k], );
} else
break;
} }
} } int main() {
getHprime();
int n;
while(scanf("%d", &n) && n) {
printf("%d %lld\n", n, getsum(n));
}
return ;
}
Day7 - I - Semi-prime H-numbers POJ - 3292的更多相关文章
- 【POJ 3292】 Semi-prime H-numbers
[POJ 3292] Semi-prime H-numbers 打个表 题意是1 5 9 13...这样的4的n次方+1定义为H-numbers H-numbers中仅仅由1*自己这一种方式组成 即没 ...
- POJ 3292 Semi-prime H-numbers (素数筛法变形)
题意:题目比较容易混淆,要搞清楚一点,这里面所有的定义都是在4×k+1(k>=0)这个封闭的集合而言的,不要跟我们常用的自然数集混淆. 题目要求我们计算 H-semi-primes, H-sem ...
- Day7 - J - Raising Modulo Numbers POJ - 1995
People are different. Some secretly read magazines full of interesting girls' pictures, others creat ...
- Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)
题意:给一个数 可以写出多少种 连续素数的合 思路:直接线性筛 筛素数 暴力找就行 (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...
- Greedy:Sum of Consecutive Prime Numbers(POJ 2739)
素数之和 题目大意:一些整数可以表示成一个连续素数之和,给定一个整数要你找出可以表示这一个整数的连续整数序列的个数 方法:打表,然后用游标卡尺法即可 #include <iostream> ...
- A - Smith Numbers POJ
While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,no ...
- POJ 3292 Semi-prime H-numbers
类似素数筛... Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6873 Accept ...
- POJ 3292
Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7059 Accepted: 3 ...
- Prime Path(poj 3126)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
随机推荐
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- 关于自学java的内容及感受
这周自学了关于java输入的知识:java输入的方法与c++和c有些不同,他需要在开头加一个import连接系统的包,才能进行输入语句的编写. 自己编写了一点简单的输入的程序: package mod ...
- Python环境搭建-2 编译器和解释器
编译器与解释器 编译器/解释器:高级语言与机器之间的翻译官 都是将代码翻译成机器可以执行的二进制机器码,只不过在运行原理和翻译过程有不同而已. 那么两者有什么区别呢? 编译器:先整体编译再执行 解释器 ...
- 二次代价函数、交叉熵(cross-entropy)、对数似然代价函数(log-likelihood cost)(04-1)
二次代价函数 $C = \frac{1} {2n} \sum_{x_1,...x_n} \|y(x)-a^L(x) \|^2$ 其中,C表示代价函数,x表示样本,y表示实际值,a表示输出值,n表示样本 ...
- 第2节 Scala中面向对象编程:7、继承的概念以及override和super关键字;8、isInstanceOf 和 asInstanceOf关键字
6.3. Scala面向对象编程之继承 6.3.1. Scala中继承(extends)的概念 Scala 中,让子类继承父类,与 Java 一样,也是使用 extends 关键字: 继承 ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)B(SET)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[2007];set<int& ...
- linux查漏补缺-linux命令行安装mysql
apt安装 sudo apt-get update sudo apt-get install mysql-server root@192:/sys/fs/cgroup# apt-get install ...
- js 字符串 常用处理方式(检索、截取、拼接、批量替换)
// 检索(字符串中判断是否包含某个字符) 字符串.search('检索的内容');// 返回-1,不包含: 返回非-1,包含 字符串.indexOf("待判断的内容"); // ...
- Python学习第四课——基本数据类型一之int and str
1.数字(int) - int() 方法 # 定义 a1=123 a2=456 #功能1:将字符串转换为数字 #例子1: a = " print(type(a)) # type()为查看类型 ...
- c++存储区域
来自:https://www.cnblogs.com/simonote/articles/3146038.html 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储 ...