日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Kattis之旅——Prime Reduction

發布時間:2025/7/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 1Sample 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=2; i*i<=n; i++) {if( n%i==0 ) return false;}return true; }ll solve(ll n){ll ans = 0;int i = 2;while( n>1 ) {if( n%i==0 ) {ans += i;n /= i;if( is_pr(n) ) {//這步是關鍵,不寫超時ans += n;break;} } else ++i;}return ans; } void input() {while( cin >> n) {res = 1;if( n == 4 ) break;while( !is_pr(n) ) {n = solve(n);res ++;}cout << n << " " << res << endl;} }int main(){input();return 0; }

?

轉載于:https://www.cnblogs.com/Asimple/p/6785476.html

總結

以上是生活随笔為你收集整理的Kattis之旅——Prime Reduction的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。