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. windos server 2008 r2 ftp重启教程

    1: 开始菜单输入IIS 2: SERVER> 网站> 站点 >重新启动. 3: 输入ftp://localhost   ,  输入用户名和密码.登录.

  2. JAVA基础——时间Date类型转换

    在java中有六大时间类,分别是: 1.java.util包下的Date类, 2.java.sql包下的Date类, 3.java.text包下的DateFormat类,(抽象类) 4.java.te ...

  3. DataFrame WordCount

    测试数据: ** * 使用DataFrame实现WordCount */ object DataFrameWordCount { def main(args: Array[String]): Unit ...

  4. 在Java应用中通过SparkLauncher启动Spark任务

    本博客内容基于Spark2.2版本,在阅读文章并想实际操作前,请确保你有: 一台配置好Spark和yarn的服务器 支持正常spark-submit --master yarn xxxx的任务提交 老 ...

  5. 启动Jenkins

    启动Jenkins? 进入到Jenkins的war包所在的目录  ->CMD 到 Jenkins的war包所在的目录 输入: Java -jar jenkins.war 启动Jenkins服务n ...

  6. Ubuntu install mysql database

    简要说下ubuntu Linux下安装MySql数据库 一. 安装 # apt-get install mysql-server# apt-get install mysql-client 二.启动 ...

  7. java基础 逻辑

    1, 用for循环打印一个4*5的矩形 public class Test { public static void main(String[] args){ for (int i = 1; i &l ...

  8. sqlserver字符集问题(中文出乱码,排序错误等)

    在创建sqlserver 数据库时未指定排序字符集,databases则会使用instances的排序规则.为了支持中文,需要设置成Chinese_PRC_CI_AS. (1)通过sql脚本修改 -- ...

  9. Some Useful Resources for the Future Usage

    并发编程 http://ifeve.com/ 美国各州 http://114.xixik.com/usa-stats/ 美国各州邮编zip code -> https://www.douban. ...

  10. Uva297 Quadtrees【递归建四分树】【例题6-11】

    白书 例题6-11 用四分树来表示一个黑白图像:最大的图为根,然后按照图中的方式编号,从左到右对应4个子结点.如果某子结点对应的区域全黑或者全白,则直接用一个黑结点或者白结点表示:如果既有黑又有白,则 ...