HDU 1164 Eddy's research I( 试除法 & 筛法改造试除法 分解整数 )
**链接:****传送门 **
题意:给出一个整数 n ,输出整数 n 的分解成若干个素因子的方案
思路:经典的整数分解题目,这里采用试除法 和 用筛法改造后的试除法 对正整数 n 进行分解
方法一:试除法对正整数 n 进行分解
/*************************************************************************
> File Name: hdu1164.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月23日 星期二 22时40分34秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
vector<int> fac[65536];
void init(){
for(int x = 1 ; x <= 65535 ; x++){
int tmp = x , cnt = 0;
for(int i = 2 ; i*i <= tmp ; i++){
while ( tmp % i == 0 ){
fac[x].push_back(i);
tmp /= i;
}
}
if( tmp != 1 ) fac[x].push_back(tmp);
}
}
int main(){
init();
int n;
while(~scanf("%d",&n)){
int len = fac[n].size();
for(int i = 0 ; i < len - 1 ; i++)
printf("%d*",fac[n][i]);
printf("%d\n",fac[n][len-1]);
}
return 0;
}
方法二:筛法对试除法进行优化
原理:相较以试除法不经挑选跑遍整个 [ 1 , sqrt(n) ] ,其中 i = 合数的时候实际上是无效的操作,不如直接打出素数表,跑一下素数表来避免判断大量的合数情况来加速
/*************************************************************************
> File Name: hdu1164t2.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月23日 星期二 23时06分28秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 65535 + 10;
vector<int> fac[MAX_N];
int prime[MAX_N] = {0} , pri_list[MAX_N] , pri_cnt = 0;
void init_prime(){
for(int i = 2 ; i*i < MAX_N ; i++){
if( prime[i] == 0 ){
pri_list[ pri_cnt++ ] = i;
for(int j = 2*i ; j < MAX_N ; j += i) prime[i] = 1;
}
}
}
void init_ans_list(){
for(int x = 1 ; x < MAX_N ; x++){
int tmp = x , cnt = 0;
for(int i = 0 ; pri_list[i] <= tmp && i < pri_cnt ; i++){
while( tmp % pri_list[i] == 0 ){
fac[x].push_back(pri_list[i]);
tmp /= pri_list[i];
}
}
if( tmp != 1 ) fac[x].push_back(tmp);
}
}
int main(){
init_prime();
init_ans_list();
int n;
while(~scanf("%d",&n)){
int len = fac[n].size();
for(int i = 0 ; i < len-1 ; i++) printf("%d*",fac[n][i]);
printf("%d\n",fac[n][len-1]);
}
return 0;
}
HDU 1164 Eddy's research I( 试除法 & 筛法改造试除法 分解整数 )的更多相关文章
- hdu 1164:Eddy's research I(水题,数学题,筛法)
Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1164 Eddy's research I
http://acm.hdu.edu.cn/showproblem.php?pid=1164 题意很简单,只是写代码的时候需要注意几个问题 一.筛选素数的时候记得用埃式筛选法,要是直接找可能会WA. ...
- HDU 1164 Eddy's research I
题目链接 题意 : 给你一个数,让你用它的素数质因子表示出来. 思路 : 先打一下表,因为会有重复的质因子,所以从大到小开始找,并且找到一个之后不能就接着往下找,要再找一遍这个数. #include ...
- HDU 1164 Eddy's research I【素数筛选法】
思路:将输入的这个数分成n个素数的相乘的结果,用一个数组存储起来.之后再输出就能够了 Eddy's research I Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1165 Eddy's research II(给出递归公式,然后找规律)
- Eddy's research II Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDOJ 1164 Eddy's research I
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...
- HDOJ 1164 Eddy's research I(拆分成素数因子)
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...
- HDU 1165 Eddy's research II (找规律)
题意:给定一个表达式,然后让你求表达式的值. 析:多写几个就会发现规律. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...
- HDU 1165 Eddy's research II
题意:已知,求A(m, n). 分析:根据样例模拟一下过程就可以找出递推关系. #include<cstdio> #include<cstring> #include<c ...
随机推荐
- MySQL:浅析 Impossible WHERE noticed after reading const tables
使用 EXPLAIN 执行计划的时候,在 Extra 中偶尔会看到这样的描述: Impossible WHERE noticed after reading const tables 字面上的意思是: ...
- mysql 易错误理解
MySQL作为数据库的一大主力军,到处存在于我们各种系统中,相信大家都不陌生!但是,你知道你能用不代表你知道细节,那我们就来盘点盘点其中一些我们平时不太注意的地方,一来为了有趣,二来为了不让自己踩坑. ...
- ssh的tunnel设置+autossh设置
tunnel设置 一.说明 用于通过ssh转发数据 二.设置 编辑ssh server的'2Fetc/ssh/sshd_config 加入下面: #反向遂道 GatewayPorts yes #正向 ...
- solr实战-(一)
实现用户数据索引及查询 1. 启动solr solr start 2. 创建collection solr create -c user 3. schema中加入field ...
- golang自己定义数据类型查询与插入postgresql中point数据
golang自己定义数据类型查询与插入postgresql中point数据 详细代码例如以下: package main import ( "bytes" "databa ...
- python基于selenium+cv2+numpy实现登录某大型电商系统
首先贴上我的安装包 一.selenium安装 I.打开pycharm,点击Settings,找到Project Interpreter,点击右边的下拉菜单下的show All...选项 II.点击sh ...
- P4396 [AHOI2013]作业 分块+莫队
这个题正解是莫队+树状数组,但是我个人非常不喜欢树状数组这种东西,所以决定用分块来水这个题.直接在莫队维护信息的时候,维护单点同时维护块内信息就行了. 莫队就是这几行核心代码: void add(in ...
- Node.js:文件系统
ylbtech-Node.js:文件系统 1.返回顶部 1. Node.js 文件系统 Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API. Node 导入文件系统模块(fs)语 ...
- div向右偏移设置 css让div靠右移一定距离
转自:https://www.thinkcss.com/shili/1372.shtml div对象盒子向右偏移设置,使用css让div靠右一定距离-div向右移教程实例篇 div向右偏移一定距离,可 ...
- [jzoj 6073] 河 解题报告 (DP)
interlinkage: https://jzoj.net/senior/#main/show/6073 description: solution: 考虑一条河$x$被染的效果 显然对于一条河$i ...