PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
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
题意:
将一个正整数分解质因数,注意坑点1=1.第一次学习质因数分解
题解:
Pollard Rho快速因数分解。时间复杂度为O(n^(1/4))。
程序分析:对 n 进行分解质因数,应先找到一个最小的质数 i,然后按下述步骤完成:
(1)如果这个质数 i 恰等于 n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n != i,但n能被 i 整除,则应打印出 i 的值,并用 n 除以 i 的商,作为新的正整数你n,
重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
AC代码:
#include<iostream>
#include<queue>
typedef long long ll;
using namespace std;
ll n;
queue<ll>q;
int main(){
cin>>n;
ll oldN=n;
if(n == ) // 这一段代码非常重要 ,需要考虑n=1的情况
{
cout<<n<<'='<<;
return ;
}
for(ll i=;i<=n;i++){
int num=;
while(n!=i)
{
if(n%i==){
n/=i;
num++;
}else{
break;
}
}
if(n==i){
num++;
}
if(num!=){
q.push(i);
q.push(num);
}
}
cout<<oldN<<"=";
int f=;
while(!q.empty()){
if(f==) cout<<"*";
else f=;
ll x=q.front();q.pop();
ll y=q.front();q.pop();
if(y!=) cout<<x<<"^"<<y;
else cout<<x;
}
return ;
}
PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)的更多相关文章
- 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 ...
- 【PAT甲级】1059 Prime Factors (25 分)
题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
随机推荐
- 【记忆化搜索】[NOIP-2017--普及组] -- 棋盘
[题目描述] 原题目链接地址: 有一个m × m的棋盘,棋盘上每一个格子可能是红色.黄色或没有任何颜色的.你现在要从棋盘的最左上角走到棋盘的最右下角. 任何一个时刻,你所站在的位置必须是有颜色的( ...
- 安装Android Studio开发环境
下载安装包 中文社区官网 http://android-studio.org/ 目前最新的是2.3.3版本 安装Android Studio 双击安装 等待安装包自动解压 下一步 选择安装Androi ...
- python 实现定时任务
需求: 想实现 每周一到周五下班钉钉打卡提醒,每周四发周报提醒 使用了二种方法实现 一:apscheduler,代码如下 import json,requests,datetime from apsc ...
- DML子句returing into用法举例
一.概述: ORACLE的DML语句中可以指定RETURNING语句.使用起来也很简单,和SELECT INTO语句没有多大区别.RETURNING语句的使用在很多情况下可以简化PL/SQL编程. I ...
- unable to import maven project see logs for details
2019-10-21 19:31:06,987 [40598655] WARN - ution.rmi.RemoteProcessSupport - SLF4J: Actual binding i ...
- AtCoder NIKKEI Programming Contest 2019 C. Different Strokes (贪心)
题目链接:https://nikkei2019-qual.contest.atcoder.jp/tasks/nikkei2019_qual_C 题意:给出 n 种食物,Takahashi 吃下获得 a ...
- Appium自动化测试教程-自学网-monkeyrunner简介
monkeyrunner简介 MonkeyRunner工具是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Py ...
- js中特殊的宏任务
一.setImmediate 目前只有IE10+和NodeJS支持该API. 立即触发回调函数,使其进入宏任务队列(macro task queue) 语法: // 只有一个参数 setImmedia ...
- LOJ P10151 分离与合体 题解
Analysis 区间dp+记录路径 用dfs倒着找倒数第几次合并 #include<iostream> #include<cstdio> #include<cstrin ...
- DOM操作2
一.API和WebAPI API就是接口,就是通道,负责一个程序和其他软件的沟通,本质是预先定义的函数. Web API是网络应用程序接口.包含了广泛的功能,网络应用通过API接口,可以实现存储服务. ...