UVA 10791 最小公倍数的最小和(唯一分解性定理)

该问题要求找到使得其最小公倍数为给定正整数N的至少两个正整数,同时这些数之和最小。例如,当N=12时,4和3的和最小,且它们的最小公倍数是12。输入包含最多100个测试用例,每个用例给出一个1到2^31-1之间的正整数N,以N=0结束。输出应显示每个测试用例的编号和满足条件的最小和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 1005
#define MAX 500005
#define ms memset
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") ///在c++中是防止暴栈用的
ll n,ans,ca=0;
/*
题目大意:给定一个n,
可选定m个数并且这m个数的最小公倍数是n,
求m个数的最小和是多少。

唯一分解性定理;
可以感受到把每个因子当成整数时效果最佳。
但要考虑特殊情况,比如n为素数时,
或者n为1时
*/
int main()
{
    while(scanf("%lld",&n)&&n)
    {
        ans=0;
        ///if(n==1) { puts("2"); continue; }
        ll ub=sqrt(n),cnt=0;
        for(int i=2;i<=ub;i++)
        {
            if(n%i==0)
            {
                cnt++;
                int c=1;
                while(n%i==0)
                {
                    c *= i;
                    n /= i;
                }
                ans += c;
            }
        }

        if(n>1 || cnt==0)
        {
            ans += n;
            cnt ++;
        }
        if(cnt==1) ans++;
        printf("Case %d: ",++ca);
        cout<< ans <<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值