POJ3292 Semi-prime H-numbers [数论,素数筛]
Semi-prime H-numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10871 | Accepted: 4881 |
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
85
789
0
Sample Output
21 0
85 5
789 62
Source
分析:
一道素数筛法的变式题。
把素数筛法改一下,预处理出所有答案,然后直接输出每个答案就行了。
Code:
//It is made by HolseLee on 2nd Sep 2018
//POJ3292
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std; const int N=1e6+;
int n,ans[N],q[N],top;
bool no[N],yes[N]; int main()
{
ios::sync_with_stdio(false);
for(int i=; i<N; i+=) {
if(no[i])continue;
q[++top]=i;
for(int j=*i; j<N; j+=i*) no[j]=;
}
for(int i=; i<=top; ++i)
for(int j=; j<=i && q[i]*q[j]<N; ++j)
yes[q[i]*q[j]]=;
for(int i=; i<N; ++i)
ans[i]=ans[i-]+yes[i]; while() {
cin>>n; if(!n) break;
cout<<n<<" "<<ans[n]<<"\n";
}
return ;
}
POJ3292 Semi-prime H-numbers [数论,素数筛]的更多相关文章
- Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)
385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...
- UVALive-3399-Sum of Consecutive Prime Numbers(素数筛,暴力)
原题链接 写个素数筛暴力打表一波就AC了: #include <iostream> using namespace std; const int N = 10001; int i, j, ...
- LightOJ-1259 Goldbach`s Conjecture 数论 素数筛
题目链接:https://cn.vjudge.net/problem/LightOJ-1259 题意 给一个整数n,问有多少对素数a和b,使得a+b=n 思路 素数筛 埃氏筛O(nloglogn),这 ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- HDU 1016 Prime Ring Problem (素数筛+DFS)
题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...
- Bi-shoe and Phi-shoe LightOJ - 1370(数论+素数筛)
题目链接:https://vjudge.net/problem/LightOJ-1370 题意:给你N个欧拉函数值,找出每一个大于等于该欧拉函数值的数,并且要求相加和最小. 题解:因为素数i的欧拉函数 ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
- codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- POJ 3126 Prime Path (bfs+欧拉线性素数筛)
Description The ministers of the cabinet were quite upset by the message from the Chief of Security ...
随机推荐
- LeetCode-Sort List[AC源码]
package com.lw.leet4; /** * @ClassName:Solution * @Description: * Sort List * Sort a linked list in ...
- ASP.NET Core的身份认证框架IdentityServer4--(5)自定义用户登录(通过接口登录,无UI版本)
官网接口详解文档地址:文档地址 (PS:可通过接口名称搜索相应接口信息.) 源码地址:https://github.com/YANGKANG01/IdentityServer4-IdentityAut ...
- CF540 D 概率 DP
石头剪刀布三种生物(?) 随机战斗,最后三方各自只有自己方存活下来的概率是多少. 局面的转移明显,注意任选两方决定战斗时有可能出现选了同个种类的,因此注意排除掉同种的组合,也就是条件概率什么的. /* ...
- stat命令--文件权限属性设置
stat命令文件权限属性设置 stat命令用于显示文件的状态信息. stat命令的输出信息比ls命令的输出信息要更详细. 语法 stat(选项)(参数) 选项 -L:支持符号连接: -f:显示文件系统 ...
- 你知道吗?31种 CSS 选择器的应用
选择器(selector)是CSS中很重要的概念,所有HTML语言中的标记都是通过不同的CSS选择器进行控制的.用户只需要通过选择器对不同的HTML标签进行控制,并赋予各种样式声明,即可实现各种效果. ...
- 如何写出高性能SQL语句
优化SQL查询:如何写出高性能SQL语句 1.首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生欀如一条SQL语句如果 ...
- [数据库中间件]将用户添加到DB2组授权
1.将用户oracle添加到db2的用户组中,命令如下: usermod -a -G db2iam #将用户添加到组中并不改变当前所属组 注:以下与主题无关,只是列举一些关于用户的命令 id user ...
- spring 添加controller返回值绑定
@EnableWebMvc @Configuration public class Config { @Autowired private RequestMappingHandlerAdapter h ...
- CentOS 怎么设置某个目录包括子目录的写入权限 777呢
chmod -R 777 文件夹例如:chmod -R 777 /var var的权限就变成777,var下的所有子目录和文件权限都将变成777
- 【BZOJ】1711: [Usaco2007 Open]Dining吃饭
[算法]最大流 [题解] S连向食物连向牛连向牛‘连向饮料连向T. 经典的一个元素依赖于两个元素的建图方式. #include<cstdio> #include<algorithm& ...