原题:
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≤2^31− 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 <bits/stdc++.h>
using namespace std;
vector<long long> ans;
long long decomposition(long long x)
{
if(x==2)
return 3;
if(x==1)
return 2;
ans.clear();
long long t=x;
long long inde=(long long)sqrt((double)x*1.0)+1;//
for(int i=2;i<=inde&&t>1;i++)
{
while(t%i==0&&t>1)
{
t/=i;
ans.push_back(i);
}
}
if(t>1)//
ans.push_back(t);
if(ans.size()==0)
return x+1;
long long sum=0;
long long tmp=ans[0],pre=ans[0];
for(int i=1;i<ans.size();i++)
{
if(ans[i]==tmp)
pre*=ans[i];
else
{
sum+=pre;
pre=tmp=ans[i];
}
}
if(sum==0)
sum=pre+1;
else
sum+=pre;
return sum;
}
int main()
{
ios::sync_with_stdio(false);
long long n,k=1;
while(cin>>n,n)
{
long long ans=decomposition(n);
cout<<"Case "<<k++<<": "<<ans<<endl;
}
return 0;
}
解答:
这题真是给我错出花来了= =!
首先如果两个数的最小公倍数是n,那么n一定可以分解成一堆互质的因子相乘的形式。如果一个数能分解出互质的因子x和y。那么x^n与y^m一定也是互质的。所以分解以后的质数因子对应次幂的和就是最小和,否则不会产生最小公倍数n。这里注意素数和1的情况。
而且要用时间复杂度o(sqrt(n))的算法,和long long 数据类型。否则tle和re