程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA - 10791 - Minimum Sum LCM (數論相關!)

UVA - 10791 - Minimum Sum LCM (數論相關!)

編輯:C++入門知識

UVA - 10791 - Minimum Sum LCM (數論相關!)


題目鏈接:Minimum Sum LCM


UVA - 10791

Minimum Sum LCM Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

SubmitStatus

Description

Download as PDF


Minimum Sum LCM

\epsfbox{p10791.eps}

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$ \le$N$ \le$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



Problem setter: Md. Kamruzzaman
Special Thanks: Shahriar Manzoor
Miguel Revilla 2004-12-10

Source

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Examples
Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 6. Mathematical Concepts and Methods
Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 2. Mathematics :: Basic Problems
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Mathematics :: Number Theory :: Working with Prime Factors

Root :: Prominent Problemsetters :: Md. Kamruzzaman (KZaman)
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Mathematics :: Number Theory :: Working with Prime Factors


思路:分解質因子,將最小公倍數分解質因子,最小的ans便為各個質因子的相應次方數之和。

此題難點較多:

1、當N = 1時,應輸出2;

2、當N是素數的時候,輸出N+1;

3、當只有單質因子時,sum=質因子相應次方+1;

4、當N=2147483647時,它是一個素數,此時輸出2147483648,但是它超過int范圍,應考慮用long long。



AC代碼:

/*************************************************************************
	> File Name: e.cpp
	> Author: zzuspy
	> Mail: [email protected] 
	> Created Time: 2014年12月01日 星期一 19時18分33秒
 ************************************************************************/



#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LL long long
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
using namespace std;

int main()
{
	int n, cas = 1;
	while(scanf("%d", &n), n)
	{
		int m = (int)sqrt((double)n+0.5);
		int t = n, num = 0;
		LL ans = 0;
		for(int i=2; i<=m; i++)   //分解這個數
			if(t%i == 0)
			{
				num++;   //記錄質因子的個數
				int tmp = 1;
				while(t%i==0)
				{
					tmp*=i;
					t/=i;
				}
				ans+=tmp;
			}
		if(n==t)  //本身為素數時
			ans = (LL)n + 1;   //必須要加個(LL)
		else if(num == 1||t!=1)  // 單質因子或是剩下一個大於sqrt(n)的質因子的情況  
			ans += t;	// 單質因子情況下t為1,剩余一個大於sqrt(n)質因子時t為剩余質因子  		
		printf("Case %d: %lld\n", cas++, ans);
	}
	return 0;
}






  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved