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, ...
随机推荐
- abap method中的异常处理
1: 抛出异常 2: 处理异常
- SAP 创建 component
1: 进入x3c 系统,输入 T-CODE BSP_WD_CMPWB 2: 输入以Z开头的组件名. 点击create using wizard 3: 输入应用属性 4: 定义 bol mod ...
- [vue]vue路由篇vue-router
spa单页开发及vue-router基础: https://www.cnblogs.com/iiiiiher/p/9034496.html url两种传参方式 query: $route.query ...
- Py中reshape中的-1表示什么【转载】
转自:https://blog.csdn.net/weixin_39449570/article/details/78619196 1.新数组的shape属性应该要与原来数组的一致,即新数组元素数量与 ...
- 【LeetCode每天一题】Merge Two Sorted Lists(合并两个排序链表)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 2019.03.30 图解HTTP
文章来源<图解HTTP> 第一章 了解Web及网络基础 你有想过当你在浏览器(web browser)的地址栏上输入URL时,Web页面是如何实现的吗? 嗯,好像也没想过 web使用一种名 ...
- 启动及更改tomcat 配置
到tomcat安装的bin目录下面,双击那个startup.bat文件,启动Tomcat 去浏览器输入 http://localhost:8080/ 然后出现下面的界面,那就说明你的tomcat配置 ...
- ####### Scripts Summary #######
Scripts Summary Version: 1.0.1 issueDate: 2017-11-11 modifiedDate: 2017-11-28 0.configuration 1.oper ...
- Join The Future (剪枝 + 状态压缩)
一道暴力搜索的恶心剪枝题目. 先处理好某个点确定之后其他点的也确定的是谁,还有分别为什么情况,分别用vis,sta来记录.当然可以直接使用一个3进制数来表示,但是这里需要额外写一个三进制数求值的函数较 ...
- c++学习笔记(六)- vector使用和内存分配
-----------------------------2019/01/15------------------------------- 复习了下迭代器,其实c++参考里讲的很清楚,主要需要辨析规 ...