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 = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

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 = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ 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)相关推荐

  1. 【题目分析】1059 Prime Factors (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any positive integer N, you are supposed to find all of its ...

  2. 1059 Prime Factors (25 分)【难度: 一般 / 知识点: 分解质因子 】

    注意: 判断1000000007这种只有一个质数特别大的情况 #include<bits/stdc++.h> using namespace std; map<int,int> ...

  3. 1059. Prime Factors (25)

    题目如下: Given any positive integer N, you are supposed to find all of its prime factors, and write the ...

  4. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  5. 1059. Prime Factors (25)--taste

    点击打开链接 注意: 1.对于输入为1的情况要特判 2.factor在int范围内开到10就足够 3.sqrt是范围,由于n后面被修改故范围要单写,且sqrt是double int sqr=sqrt( ...

  6. 1059 Prime Factors

    1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...

  7. PAT 1059 Prime Factors[难]

    1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...

  8. PAT甲级1059 Prime Factors :[C++题解]分解质因子

    文章目录 题目分析 题目链接 题目分析 暴力求质因数 下面i就是质因子,s是质因子i的阶数. 暴力的时间复杂度O(n),会超时 void divide(int n){for(int i=2;i< ...

  9. 1059 Prime Factors(25 分)

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

最新文章

  1. 常见一致性协议(一)
  2. Nat. Methods | 学习微生物与代谢产物之间相互作用的神经网络
  3. windows2016重新配置sid
  4. 经常收到信用卡邀请短信,但为什么总是办不下来?
  5. 【CF1189D】Add on a Tree【结论】【构造】
  6. (组合数求模=乘法逆元+快速幂) Problem Makes Problem
  7. selenium 2定位方式实例
  8. 贪心 穷举 动态规划 区别
  9. htmL全栈开发项目实例,【译】基于MEAN的全栈开发实例教程6(完)
  10. matlab d函数,matlab常用函数大集合
  11. 手机拍摄的身份证怎么制作为复印件?
  12. 硬盘损坏,怪我咯?3分钟拯救硬盘里的小姐姐!
  13. CSDN独家全网首发专栏 | 《目标检测YOLO改进指南》改进涨点推荐!
  14. 2023年计算机考研数学考一还是二?考研数学一二三区别
  15. windows如何查看自己的ip地址
  16. 计算机三维设计ppt,1 三维建模技术发展史.ppt
  17. Trinity的安装与使用
  18. 生活中,真的有那种令人驻足的美女吗?
  19. 最新全国手机号码归属地信息SQLite数据库2019年2月更新 1
  20. 苹果再次确立其移动王者地位

热门文章

  1. 多目标跟踪算法,零基础极速入门(一)
  2. spark学习之处理数据倾斜
  3. Charles抓包神器
  4. 如何彻底删除MySQL服务
  5. 证件照半身照合成服装PNG素材-463张
  6. 天池NLP学习赛(1)赛题理解
  7. 中南民族大学计算机科学学院转专业好吗,中南民族大学计算机科学学院
  8. 华为2014机试样题1: 计票统计 java实现
  9. 手把手教你做一个Java贪吃蛇小游戏
  10. 软件发布中的不同版本