Uva10791 Minimum Sum LCM(数论、唯一分解定理)

    xiaoxiao2024-10-18  6

    Uva10791 Minimum Sum LCM(数论、唯一分解定理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1732


    题目

    Time Limit:3000MS Memory Limit:0KB Description LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For example 12 can be expressed as the LCM of 1, 12 or 12, 12 or 3, 4 or 4, 6 or 1, 2, 3, 4 etc. In this problem, you will be given a positive integer N. You have to find out a set of at least two positive integers whose LCM is N. As infinite such sequences are possible, you have to pick the sequence whose summation of elements is minimum. We will be quite happy if you just print the summation of the elements of this set. So, for N = 12, you should print 4+3 = 7 as LCM of 4 and 3 is 12 and 7 is the minimum possible summation.

    Input The input file contains at most 100 test cases. Each test case consists of a positive integer N (1 ≤ N ≤ 231 − 1). Input is terminated by a case where N = 0. This case should not be processed. There can be at most 100 test cases.

    Output Output of each test case should consist of a line starting with ‘Case #: ’ where # is the test case number. It should be followed by the summation as specified in the problem statement. Look at the output for sample input for details.

    Sample Input 12 10 5 0

    Sample Output Case 1: 7 Case 2: 7 Case 3: 6


    题意

    给你一个最小公倍数n,让你求他的最小质因子之和


    分析

    既然是最小,不难想出,最小之和即为他的质因子*各自指数之和。 即对于唯一分解定理之后的 x1p1 x2p2 xnpn ,最小和为 x1p1+x2p2+...+xnpn

    提示:本题还存在以下陷阱: 1.当只能分出一个质因子时,需要用1补齐。比如n为素数,那么答案为n+1(因为lcm的条件就是至少有两个数) 2.有些情况会爆int,建议用long long 3.补充情况1,对于1这个数,答案应为1+1。(原理与1相同)


    源码

    #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> #include<algorithm> #include<string> #include<sstream> #include<cmath> #include<set> #include<map> #include<vector> #include<stack> #include<utility> #include<sstream> #define mem0(x) memset(x,0,sizeof x) #define mem1(x) memset(x,-1,sizeof x) #define dbug cout<<"here"<<endl; //#define LOCAL using namespace std; typedef long long ll; typedef unsigned long long ull; const int INF = 0x3f3f3f3f; const int MAXN = 1e6+10; const int MOD = 1000000007; ull solve(ull n){ ull sum = 0; int cnt = 0; for(int i = 2;i <= sqrt(n+1); ++i){ ull tmp = 1; if(n%i == 0){ cnt++; while(n%i == 0){ tmp *= i; n /= i; } sum += tmp; } if(n == 1) break; } if(cnt==0 || n>1){ sum += n; cnt++; } if(cnt == 1) return sum+1; return sum; } int main(){ #ifdef LOCAL freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin); freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout); #endif ull n; int kase = 0; while(cin >> n, n){ ull ans = solve(n); printf("Case %d: ",++kase); cout << ans << endl; } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1292750.html
    最新回复(0)