A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composite integer is one which is not prime. The fundamental theorem of arithmetic says that any integer x can be expressed uniquely as a set of prime factors – those prime numbers which, when multiplied together, give x. Consider the prime factorization of the following numbers:
10=2×5 16=2×2×2×2 231=3×7×11
Consider the following process, which we’ll call prime reduction. Given an input x:

if xx is prime, print xx and stop
factor xx into its prime factors p1,p2,…,pk
let x=p1+p2+⋯+pk
go back to step 1
Write a program that implements prime reduction.

Input
Input consists of a sequence of up to 2000020000 integers, one per line, in the range 22 to 109109. The number 44 will not be included in the sequence (try it to see why it’s excluded). Input ends with a line containing only the number 4.

Output
For each integer, print the value produced by prime reduction executed on that input, followed by the number of times the first line of the process executed.

Sample Input 1 Sample Output 1
2
3
5
76
100
2001
4
2 1
3 1
5 1
23 2
5 5
5 6

大意就是:给你一个数x——1、如果x是素数,直接输出x以及循环的步数。2、如果不是,那就x分解质因数,把所有质因数之和给x,步数+1,执行第一步。

 //Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, s, res, ans, len, T, k, num; bool is_pr(ll n) {
for(int i=; i*i<=n; i++) {
if( n%i== ) return false;
}
return true;
} ll solve(ll n){
ll ans = ;
int i = ;
while( n> ) {
if( n%i== ) {
ans += i;
n /= i;
if( is_pr(n) ) {//这步是关键,不写超时
ans += n;
break;
}
} else ++i;
}
return ans;
} void input() {
while( cin >> n) {
res = ;
if( n == ) break;
while( !is_pr(n) ) {
n = solve(n);
res ++;
}
cout << n << " " << res << endl;
}
} int main(){
input();
return ;
}

Kattis之旅——Prime Reduction的更多相关文章

  1. Kattis之旅——Prime Path

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  2. Kattis之旅——Number Sets

    You start with a sequence of consecutive integers. You want to group them into sets. You are given t ...

  3. Kattis之旅——Chinese Remainder

    Input The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Th ...

  4. Kattis之旅——Fractional Lotion

    Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...

  5. Kattis之旅——Factovisors

    The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...

  6. Kattis之旅——Rational Arithmetic

    Input The first line of input contains one integer, giving the number of operations to perform. Then ...

  7. Kattis之旅——Divisible Subsequences

    Given a sequence of positive integers, count all contiguous subsequences (sometimes called substring ...

  8. Kattis之旅——Inverse Factorial

    题目意思就是已知n的阶乘,求n. 当输入的阶乘小于10位数的时候,我们可以用long long将字符串转化成数字,直接计算. 而当输入的阶乘很大的时候,我们就可以利用位数去大概的估计n. //Asim ...

  9. Kattis之旅——Perfect Pth Powers

    We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, ...

随机推荐

  1. [py]Python locals() 函数

    Python locals() 函数作用 http://www.runoob.com/python/python-func-locals.html locals() 函数会以字典类型返回当前位置的全部 ...

  2. IOT-web替换某一个前台版本

    比如 替换endpoint 1,首先改写 package.json文件,记得去掉^,改写成某一个版本 2.删除相应的内容 3.执行 npm install 4.最后 build

  3. Py中reshape中的-1表示什么【转载】

    转自:https://blog.csdn.net/weixin_39449570/article/details/78619196 1.新数组的shape属性应该要与原来数组的一致,即新数组元素数量与 ...

  4. 对于jquery实现原理的浅谈

    关键词:prototype(原型).它能让javascript的方法(也可看成:类)能够动态地追加方法(猜测:目的是为了代码实现引入“类的思想”) 废话少说,代码见真义. <html> & ...

  5. mysql----------阿里云RDS导入导出

    1.这是阿里云rds如何将导出的物理备份文件,导入到自建库里面: https://help.aliyun.com/knowledge_detail/5973700.html?spm=5176.7766 ...

  6. ssh整合not found class 异常总结

    (1)org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.microsoft.sqls ...

  7. TP图片上传

    //控制器文件 public function index(){ if(!empty($_POST)){ $file = $_FILES["file"]; if(!isset($f ...

  8. linux中截取文件的特定字节(去掉utf-8 bom头)

    事出有因,之所以要截取特定字节,是为了给utf-8编码的文件去掉bom头. bom头好去啊,notepad++文本编辑器中就有这个功能啊.可是,问题所在是要编辑的文件太大了,300MB,小电脑卡shi ...

  9. 软工网络15团队作业4——Alpha阶段敏捷冲刺6.0

    软工网络15团队作业4--Alpha阶段敏捷冲刺6.0 1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 成员 昨天已完成 今天计划完成 郭炜埕 ...

  10. Codeforces Round #322 (Div. 2) E F

    E. Kojiro and Furrari 题意说的是 在一条直线上 有n个加油站, 每加一单位体积的汽油 可以走1km 然后每个加油站只有一种类型的汽油,汽油的种类有3种 求从起点出发到达终点要求使 ...