HDU-1164-Eddy's research I(分解质因数)
由于这道题目数据范围小,所以属于水题。可以采取暴力的做法来解决。
代码如下:
#include"bits/stdc++.h"
using namespace std;
const int maxn=;
bool tag[maxn+];
vector<int>v;int n;
bool judge(int m){
while(n%m==){
if(n==m){
printf("%d\n",m);
return true;
}
else printf("%d*",m);
n/=m;
}
return false;
}
int main(){
for(int i=;i<=maxn;i++){
if(!tag[i])v.push_back(i);
for(int j=i<<;j<=maxn;j+=i)
tag[j]=true;
}
while(~scanf("%d",&n)){
for(int i=;i<v.size();i++)
if(n%v[i]==&&judge(v[i]))
break;
}
return ;
}
但是如果把这题的数据范围加到1e8,那么用这种暴力的方法光是打一个素数表都很耗时。如何快速解决1e8的因式分解呢?可以这样想:
1e8以内的数大于1e4的质因子最多只能出现一次(因为1e4的平方等于1e8,所以如果出现一次以上就会超过1e8),而且如果这个数出现了大于1e4的质因子,那么当我们把小于1e4的质因子都除尽时,留下的就是这个大于1e4的质因子。所以我们打素数表时其实不用打到1e8,只要1e4就够了(如果范围是n就打到根号n),这样可以加速了1e4倍。
代码如下:
#include"bits/stdc++.h"
using namespace std;
const int maxn=1e4;
bool tag[maxn+];
vector<int>v;int n;
bool judge(int m){
while(n%m==){
if(n==m) return true;
else printf("%d*",m);
n/=m;
}
return false;
}
int main(){
for(int i=;i<=maxn;i++){
if(!tag[i])v.push_back(i);
for(int j=i<<;j<=maxn;j+=i)
tag[j]=true;
}
while(~scanf("%d",&n)){
for(int i=;i<v.size();i++)
if(n%v[i]==&&judge(v[i]))
break;
printf("%d\n",n);
}
return ;
}
对于这一题65535的范围maxn只要设为256就够了,其他基本没有改动
还有一种解决因式分解的方法,如果如果内存足够并且测试数据有很多组的情况下这种方式会比较优
代码如下:
#include"bits/stdc++.h"
using namespace std;
const int maxn=1e4;
const int maxm=1e8;
int tag[maxm+];
vector<int>v;int n;
int main(){
for(int i=;i<=maxn;i++){
if(!tag[i])v.push_back(i);
for(int j=i<<;j<=maxm;j+=i)
if(!tag[j])tag[j]=i;
}
while(~scanf("%d",&n)){
while(tag[n]){
printf("%d*",tag[n]);
n/=tag[n];
}
printf("%d\n",n);
}
return ;
}
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( 试除法 & 筛法改造试除法 分解整数 )
链接:传送门 题意:给出一个整数 n ,输出整数 n 的分解成若干个素因子的方案 思路:经典的整数分解题目,这里采用试除法 和 用筛法改造后的试除法 对正整数 n 进行分解 方法一:试除法对正整数 n ...
- 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 ...
- HDU 4497 GCD and LCM (分解质因数)
链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4497 假设G不是L的约数 就不可能找到三个数. L的全部素因子一定包括G的全部素因子 而且次方数 ...
- 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 ...
随机推荐
- 网页时不时打不开?试试阿里DNS 233.5.5.5 /233.6..6.6
最经上网都是用手机热点,但发现用谷歌浏览器时,时不时打不开网页.最后发现是DNS的问题,原来我的dns是8.8.8.8. 最后更改成阿里的DNS 233.5.5.5 /233.6..6.6,打开网页流 ...
- 用c语言实现的几个小项目
1.参考:Linux系统编程 2.参考:制作简单计算器 3.参考:制作2048小游戏 4.参考:五子棋实现
- pandas(二)
1.Series序列 一维的数组数据,构建是传二维数据会报错,数据具有索引,构建时如果不传索引,默认为数字rang索引. series存在列名和索引,sr.at[0]是通过列名来定位数据(iat定位行 ...
- 1016D.Vasya And The Matrix#矩阵存在
题目出处:http://codeforces.com/contest/1016/problem/D #include<iostream> #define ll long long int ...
- MobileNets: Open-Source Models for Efficient On-Device Vision
https://research.googleblog.com/2017/06/mobilenets-open-source-models-for.html Wednesday, June 14, ...
- nginx 配合jersey+netty的奇怪问题
角色 client proxy nginx server jersey+netty 问题表现 client 直接请求server 正常,返回准确json数据 jsondat client->ng ...
- Crowd Control
我的图论还是只会套模板 嘤嘤嘤 题目描述 The BAPC draws a large number of visitors to Amsterdam. Many of these people ar ...
- 关于JDBC、JdbcTemplate使用遇到的坑
1.如果数据源是oracle(mysql结尾是可以使用";"的),sql字符串中结尾处禁止使用分号";",不然会报错:java.sql.SQLException ...
- EmailService
package me.zhengjie.tools.service; import me.zhengjie.tools.domain.EmailConfig; import me.zhengjie.t ...
- 修改mysql密码报错: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
使用这种格式报错: 格式:mysql> set password for 用户名@localhost = password('新密码'); 找到另一种方法解决: ALTER USER 'root ...