uva 10791 minimum sum lcm

本文介绍了一种将整数分解为质因子的方法,并通过累加这些因子来求解特定数学问题。针对素数和1的特殊情况,文章特别说明了答案的计算方式。

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

1.将一个数分解成质因子,将相同的因子乘起来作为一个处理后的因子

2.将处理后得到的多个因子直接相加就是答案

3.因为题目说只要需要两个数字,所以对于1和素数我们需要小心。对于素数,我们只能分解出一个因子就它自己,对于1一个因子都分解不出来(我们不把1当做因子),他们的答案都是n+1,因为只有1和n的LCM是n 

//  Created by Chenhongwei in 2015.
//  Copyright (c) 2015 Chenhongwei. All rights reserved.

#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
int main()
{	
	//ios::sync_with_stdio(false);
	// freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	ll num;
	int Case=0;
	while(scanf("%lld",&num)&&num)
	{
		Case++;
		printf("Case %d: ",Case);
		ll sum=0,cnt=0,n=num;
		ll m=(ll)sqrt(num+0.5);
		for(ll i=2;i<=m&&n!=1;i++)
			if(!(n%i))
			{
				cnt++;
				ll tmp=1;
				while(!(n%i))
				{
					tmp*=i;
					n=n/i;
				}
				sum+=tmp;
				// cout<<sum<<endl;
			}
		if(cnt==0||(cnt==1&&n==1))
			printf("%lld\n",num+1);
		else if(n!=1)
			printf("%lld\n",sum+n);
		else 
			printf("%lld\n",sum);
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值