Kattis之旅——Prime Reduction
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  | 
2 1  | 
大意就是:给你一个数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的更多相关文章
- Kattis之旅——Prime Path
		
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
 - Kattis之旅——Number Sets
		
You start with a sequence of consecutive integers. You want to group them into sets. You are given t ...
 - Kattis之旅——Chinese Remainder
		
Input The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Th ...
 - Kattis之旅——Fractional Lotion
		
Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...
 - Kattis之旅——Factovisors
		
The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...
 - Kattis之旅——Rational Arithmetic
		
Input The first line of input contains one integer, giving the number of operations to perform. Then ...
 - Kattis之旅——Divisible Subsequences
		
Given a sequence of positive integers, count all contiguous subsequences (sometimes called substring ...
 - Kattis之旅——Inverse Factorial
		
题目意思就是已知n的阶乘,求n. 当输入的阶乘小于10位数的时候,我们可以用long long将字符串转化成数字,直接计算. 而当输入的阶乘很大的时候,我们就可以利用位数去大概的估计n. //Asim ...
 - 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, ...
 
随机推荐
- drf权限组件
			
1.简介 设置哪种用户的权限可以做什么事 2.用法 在MyAuth文件编写权限类, from rest_framework.permissions import BasePermission 代码如下 ...
 - sap  component 导航 链接
			
1: 定义一个导航链接名字,这个名子如果在程序中遇到(该名字会在程序中使用),就会触发这样一个导航. 导航有两个view,一个原来的view,一个出发abc之后的target view,也就是目标视图 ...
 - 大牛推荐的10本学习 Python 的好书
			
Python:蛇亚目蟒科,主要包括分布于非洲及亚洲的无毒蟒蛇. Python:Richard Clabaugh拍摄的恐怖电影,2000年发行. Python:澳大利亚汽车公司. Python:英国偶发 ...
 - 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:(十)使用JRTPLIB传输RTP数据
			
myRtspClient通过简单修改JRTPLIB的官方例程作为其RTP传输层实现.因为JRTPLIB使用的是CMAKE编译工具,这就是为什么编译myRtspClient时需要预装CMAKE. 该部分 ...
 - python之进程(池)
			
获得进程id import osfrom multiprocessing import Process def info(title): print(title) print('模块名:',__nam ...
 - Cocos Code IDE (下载地址)
			
Cocos Code IDE 1.2.0 下载地址 Cocos Code IDE 1.2.0 Win32 下载地址: http://www.cocos2d-x.org/filedown/c ...
 - cocos2dx 3.x(游戏中的储存-UserDefault)
			
引擎提供的UserDefault可以简单地储存游戏中的信息,例如背景音乐音效的开关状态,用户名等等. UserDefault是一个单例对象,可以用getInstance方法来获得. 如果是第一次调 ...
 - 41.SEO----前端SEO技巧
			
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...
 - 000-js判断电脑或手机登录
			
<script type="text/javascript"> try{ if(/Android|webOS|iPhone|iPod|BlackBerry/i.test ...
 - Visual Studio下运行PowerShell脚本自增小版本号并发布到Nuget服务器上
			
Visual Studio下运行PowerShell脚本自动更新项目里AssemblyInfo.cs文件的版本(自增小版本号)并发布到Nuget服务器上 附脚本[ update.ps1文件内容]: $ ...