1059 Prime Factors(25 分)(cj)
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无法被 之内的素数整除,那么n就是素数本身,打印本身。
筛选素数 用埃拉托色尼(Eratosthenes)筛法就可以。
code
#pragma warning(disable:4996)
#include <iostream>
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
void init(int n);
vector<int> varr;
int main(){long long n;cin >> n;cout << n << '=';int maxn = sqrt(n);init(maxn);bool f = 1;for (int i = 0; i < varr.size(); ++i) {int a = varr[i];if (a > n) break;int b = 0;while (n%a == 0) {++b;n /= a;}if (b > 0) {if (f) f = 0;else cout << '*';if (b == 1) cout << a;else cout << a << '^' << b;}}if (f) cout << n << endl;system("pause");return 0;
}
void init(int n) {int* arr = new int[n+1];for (int i = 0; i <= n; ++i) arr[i] = 1;arr[1] = 0;for (int i = 2; i <= n; ++i) {if (arr[i] == 1) {varr.push_back(i);int p = i*2;while (p <= n) {arr[p] = 0;p += i;}}}
}
1059 Prime Factors(25 分)(cj)相关推荐
- 【题目分析】1059 Prime Factors (25 分)
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any positive integer N, you are supposed to find all of its ...
- 1059 Prime Factors (25 分)【难度: 一般 / 知识点: 分解质因子 】
注意: 判断1000000007这种只有一个质数特别大的情况 #include<bits/stdc++.h> using namespace std; map<int,int> ...
- 1059. Prime Factors (25)
题目如下: Given any positive integer N, you are supposed to find all of its prime factors, and write the ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- 1059. Prime Factors (25)--taste
点击打开链接 注意: 1.对于输入为1的情况要特判 2.factor在int范围内开到10就足够 3.sqrt是范围,由于n后面被修改故范围要单写,且sqrt是double int sqr=sqrt( ...
- 1059 Prime Factors
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- PAT甲级1059 Prime Factors :[C++题解]分解质因子
文章目录 题目分析 题目链接 题目分析 暴力求质因数 下面i就是质因子,s是质因子i的阶数. 暴力的时间复杂度O(n),会超时 void divide(int n){for(int i=2;i< ...
- 1059 Prime Factors(25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
最新文章
- 常见一致性协议(一)
- Nat. Methods | 学习微生物与代谢产物之间相互作用的神经网络
- windows2016重新配置sid
- 经常收到信用卡邀请短信,但为什么总是办不下来?
- 【CF1189D】Add on a Tree【结论】【构造】
- (组合数求模=乘法逆元+快速幂) Problem Makes Problem
- selenium 2定位方式实例
- 贪心 穷举 动态规划 区别
- htmL全栈开发项目实例,【译】基于MEAN的全栈开发实例教程6(完)
- matlab d函数,matlab常用函数大集合
- 手机拍摄的身份证怎么制作为复印件?
- 硬盘损坏,怪我咯?3分钟拯救硬盘里的小姐姐!
- CSDN独家全网首发专栏 | 《目标检测YOLO改进指南》改进涨点推荐!
- 2023年计算机考研数学考一还是二?考研数学一二三区别
- windows如何查看自己的ip地址
- 计算机三维设计ppt,1 三维建模技术发展史.ppt
- Trinity的安装与使用
- 生活中,真的有那种令人驻足的美女吗?
- 最新全国手机号码归属地信息SQLite数据库2019年2月更新 1
- 苹果再次确立其移动王者地位