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 ...
随机推荐
- Pyhton语言的优缺点
python作为一门高级编程语言,它的诞生虽然很偶然,但是它得到程序员的喜爱却是必然之路. 龟叔给Python的定位是“优雅”.“明确”.“简单”,所以Python程序看上去总是简单易懂,初学者学Py ...
- iOS 开发之 开发一款自己的美颜相机
以前在公司做项目时很少遇到对相机.图片进行处理的(非公司业务),只是偶尔上传,裁剪,预览下.近期自己准备写个相机应用,把图片处理的这些技术细节整理下.包含美颜相机,图片美化,简单拼图,艺术拼图等主要模 ...
- json 常用的方法
JSON 是用于存储和传输数据的格式. JSON 通常用于服务端向网页传递数据 . ------- 菜鸟网 1. JSON.parse() :用于将一个 JSON 字符串转换为 JavaScrip ...
- python学习 —— python3简单使用pymysql包操作数据库
python3只支持pymysql(cpython >= 2.6 or >= 3.3,mysql >= 4.1),python2支持mysqldb. 两个例子: import pym ...
- 分支预测(branch prediction)
记录一个在StackOverflow上看到一个十分有趣的问题:问题. 高票答案的优化方法: 首先找到罪魁祸首: if (data[c] >= 128) sum += data[c]; 优化方案使 ...
- Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全
通过Java日期时间API系列10-----Jdk8中java.time包中的新的日期时间API类的DateTimeFormatter, 可以看出java8的DateTimeFormatter完美解决 ...
- 嵊州普及Day6T3
题意:n个点,对于q个询问,有t秒及一个矩形的范围.在此矩形内的数每秒加1,若等于c,则下一秒变为0. 思路:t可能很大,%c+1就可以了.然后一个一个加起来就可以了. 见代码: #include&l ...
- 搭建springboot的ssm(spring + springmvc + mybatis)的maven项目
最终项目目录结构 创建过程 1.创建开关SpringBootApplication 为了创建快速.我们使用idea自带的创建springboot来创建结构,当然创建普通的web项目也是可以的.(使用e ...
- sqlserver链接字符串
string connectionStr = "Data Source=LJJ-FF\\LJJ;Initial Catalog=TestDapper;User ID=sa;Password= ...
- 警示框UIAlertController的使用(看完马上会用!!)
本文尽量图文并茂,并且提供对应的代码,确保看到这篇文章马上能够上手使用UIAlertController控件.-我要兑现我的务实宣言- 本文构思: 1.出具效果图,通过这种最直接方式了解该控件的展示效 ...