PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.
Output Specification:
Factor N in the format N = p1^k1*p2^k2*…*pm^km, where pi's are prime factors of N in increasing order, and the exponent ki is the number of pi -- hence when there is only one pi, ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
题目大意:给出一个数n,在long int内,给出其素数分解,如果某个素数有多个是其分数,那么用指数形式表示;如果n本身就是素数,那么指数1不输出。
//猛一看,自己就想到了是建立大数素数表,但是具体操作我似乎不大会。以前看过,但是不知道具体怎么写了。
//自己写了一点点就蔫了。
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stdio.h>
#include <map>
#include<cmath>
using namespace std;
//需要先求出long int内所有的素数。
//但是有一个问题,如果是long int,那么我定义多大的数组呢?要么用向量?
#define maxn 1e7
int a[maxn];
vector<int> vt;
bool isPrime(int n){
int m=sqrt(n);
for(int i=;i<=n;i++){
if(n%i==)return false;
}
return true;
} void getPrime(int n){
for(int i=;i<=n;i++){
if(a[i]==){
if(isPrime(i)){
a[i]=;
for(int j=i*i;j<=n;j=j*i){//是按照立方去标记吗? }
}
}
}
}
int main()
{
long int n;
cin>>n;
getPrime(n);
return ;
}
下面是柳神的代码:


1.原来求大数内素数还可以这么求,学习了。
2.并且在判断时可以边判断边输出。
PAT 1059 Prime Factors[难]的更多相关文章
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- PAT 1059. Prime Factors
反正知道了就是知道,不知道也想不到,很快 #include <cstdio> #include <cstdlib> #include <vector> using ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- 1059 Prime Factors (25分)
1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]
题目 Given any positive integer N, you are supposed to find all of its prime factors, and write them i ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- 1059 Prime Factors(25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
随机推荐
- svn & git 问题汇总
svn: warning: W150002: '/Users/piercalex/svn/azzz/1' is already under version control svn: E200009: ...
- Hadoop家族系列文章
转自:http://blog.fens.me/series-hadoop-family/ Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, H ...
- php -- 魔术方法 之 对象克隆 : __clone()
产生对象的方法: 1.从类产生对象.new,通过new去堆区开辟一块内存空间 2.从对象产生对象.clone,通过关键字clone,将一个对象完整的复制一份,新开辟一块内存空间,将复制的结果存放到该内 ...
- 【NOIP模拟题】“与”(位运算)
因为是与运算,所以我们可以贪心地每次找最高位的,将他们加入到新的序列中,然后每一次在这个新的序列继续找下一个位. 然后最后序列中任意两个的与运算的值都是一样的且是最大的. #include <c ...
- 【BZOJ】1644: [Usaco2007 Oct]Obstacle Course 障碍训练课(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1644 这和原来一题用dp来做的bfs很像啊orz.. 我们设f[i][j][k]代表i,j这个点之前 ...
- w.5 Y k J.COm 9 如何做好总经理助理
总经理助理的工作职责:(指要做的事—做事的责任) 忠诚的总管家----总经理的代言人和执行官. 总经理助理一方面要为企业发展运筹帷幄,另一方面更要分担繁重的行政事务,所以他务必履行对该职业的使命感. ...
- 【openwrt+arduion】案例
http://www.geek-workshop.com/thread-4950-1-1.html http://www.guokr.com/article/319356/ http://www.gu ...
- UESTC 1511(差分约束)
题目链接:http://acm.uestc.edu.cn/problem.php?pid=1511 思路:我们可以等到这样的5个关系式: k=1:dsit[a]-dist[b]>=0&& ...
- Spring源码从开始到放弃(一)
参考<Spring技术内幕>分析. github上面有spring的源码(https://github.com/spring-projects/spring-framework) spri ...
- Shell脚本编程与文件系统修复
导读 Linux基金会发起了LFCS认证(Linux 基金会认证系统管理员)Linux Foundation Certified Sysadmin,这是一个全新的认证体系,旨在让世界各地的人能够参与到 ...