PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488
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
代码:
- #include <bits/stdc++.h>
- using namespace std;
- long int P;
- int prime[500010];
- int main() {
- memset(prime, 1, sizeof(prime));
- for(int i = 2; i * i <= 500010; i ++)
- for(int j = 2; j * i < 500010; j ++)
- prime[j * i] = 0;
- scanf("%ld", &P);
- printf("%ld=", P);
- if(P == 1) printf("1");
- bool First = false;
- for(int i = 2; P >= 2; i ++) {
- int cnt = 0, flag = 0;
- while(prime[i] && P % i == 0) {
- cnt ++;
- P /= i;
- flag = 1;
- }
- if(flag) {
- if(First)
- printf("*");
- printf("%d", i);
- First = true;
- }
- if(cnt > 1)
- printf("^%d", cnt);
- }
- return 0;
- }
素数表! Get
PAT 甲级 1059 Prime Factors的更多相关文章
- 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 ...
- PAT甲级——A1059 Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- 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 ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
- 1059 Prime Factors (25分)
1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 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 分)
题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...
随机推荐
- (数据科学学习手札55)利用ggthemr来美化ggplot2图像
一.简介 R中的ggplot2是一个非常强大灵活的数据可视化包,熟悉其绘图规则后便可以自由地生成各种可视化图像,但其默认的色彩和样式在很多时候难免有些过于朴素,本文将要介绍的ggthemr包专门针对原 ...
- 柱体内温度分布图 MATLAB
对于下底面和侧面绝热,上底面温度与半径平方成正比的柱体,绘制柱体内温度分布图. 这里给出两种尝试:1.散点图:2.切片云图 1. 散点图仿真 首先使用解析算法求的场解值的解析表达,其次求解Bessel ...
- Django模型中的元选项和object对象
1.元选项,在模型类型中定义一个Meta类,用于设置元信息 class Student(models.Model): name = models.CharField(max_length=20) se ...
- 20155305 2016-2017-2 《Java程序设计》 实验五 Java网络编程及安全实验报告
20155305 2016-2017-2 <Java程序设计> 实验五 Java网络编程及安全实验报告 实验内容 1.掌握Socket程序的编写. 2.掌握密码技术的使用. 3.设计安全传 ...
- 20155313 2016-2017-2 《Java程序设计》第四周学习总结
20155313 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 6 继承与多态 面对对象中,子类继承父类,避免重复的行为定义,不过并非为了避免重复定义行为就 ...
- 20155330 2016-2017-2 《Java程序设计》第二周学习总结
20155330 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 学习目标 了解Java编程风格 认识Java的类型与变量 掌握Java流程控制的方法(分支. ...
- P3940 分组
P3940 分组 https://www.luogu.org/problemnew/show/P3940 官方题解http://pan.baidu.com/s/1eSAMuXk 分析: 并查集. 首先 ...
- jaron插件的用法
一.dict字典插件的基本用法: <%@ taglib prefix="dict" uri="http://www.evan.jaron.com/tags/dict ...
- 「日常训练」 Fire!(UVA-11624)
与其说是训练不如说是重温.重新写了Java版本的代码. import java.util.*; import java.math.*; import java.io.BufferedInputStre ...
- Phaser3 场景Scene之间的传值 -- HTML JAVASCRIPT 网页游戏开发
PHASERJS3 一.首先当然得有至少有二个场景sceneA.js,sceneB.js 二.从场景A传值到场景B二种方法 1)通过事件this.events.emit('event key',{ ...