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, ...
随机推荐
- strlen函数细节
strlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头,中间某个位置,甚至是某个不确定的内存区域)开始扫描,直到碰到第一个字符串结束符'\0'为止,然后返回计数器值. 原型: ...
- Django进阶Model篇—数据库操作(ORM)
一.数据库配置 django 默认支持sqlite.mysql.oracle.postgresql数据库,像db2和sqlserver之类的数据库需要第三方的支持,具体详见https://docs.d ...
- (转)以太坊 钱包 创建 导入 Keystore
最近闲来无事 研究了下以太坊钱包 下边分享下 准备工作 : 需要用到的加密:BIP32 BIP39 BIP44 SCRYPT 加密算法 githab地址 https://github.com/Nova ...
- git 冲突解决办法
1. Pull is not possible because you have unmerged files. 症状:pull的时候 $ git pull Pull is not possible ...
- Oracle如何查询当前的crs/has自启动状态
我们知道在某些停机测试场景,是需要人为禁用crs/has的自启动的,防止过程中主机反复重启对数据库集群造成影响. 使用crsctl disable/enable crs命令可以禁用/启用crs的自启动 ...
- redhat7.5在H3C机器上黑屏无显
现象:H3C机器上,PXE安装/ISO安装系统,多用户模式启动,过内核启动界面后,屏幕黑屏无显,但是可以通过SSH登陆系统,服务正常 环境:redhat7.5/H3C R4900G3/Purely平台 ...
- G 面经 && Leetcode: Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- 001-http协议-请求报文以及服务器响应状态
Http协议的几个概念: 1.连接(Connection):浏览器和服务器之间传输数据的通道. 一般请求完毕就关闭,http不保持连接.不保持连接会降低处理速度(因为建立连接速度很慢),保持连接的话就 ...
- Android -- 实现RecyclerView可拖拽Item
1,今天和大家一起实现RecyclerView可拖拽Item,主要是使用RecyclerView结合ItemTouchHelper来实现的,来看一下效果 2,看一下怎么实现的呢,很简单,只需要给rec ...
- report源码分析——report_handle和report_server和report_catcher
report_handle主要实现对message的action,severity,file的设置,然后将message传递给server: 主要的function有两个:initial和proces ...