//目录

Uva 10791 最小公倍数的最小和 唯一分解定理

题目链接:https://vjudge.net/contest/156903#problem/C

题意:给一个数 n ,求至少 2个正整数,使得他们的最小公倍数为 n ,而且这些数之和最小。

分析:

利用唯一分解定理:

可以知道,最好是把每一个ai^pi为一个整数;

1、ai^pi不能再分,否则最小公倍数就将小于 n;题目就变成了将 n 唯一分解。

2、由于小于 n 的最大素数是一个界限,不然会超时。处理方案是:m = sqrt(n) + 0.5,最后判断一下 n;或者如上一个题目一样,数据时2^31次方,循环检查到10^5;

3、特例 1,输出2;是素数 n+1;

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int divide_all(int& n,int d) {
 6     int x = 1;
 7     while(n%d==0) {
 8         n/=d;
 9         x*=d;
10     }
11     return x;
12 }
13 
14 long long solve(int n) {
15     if(n==1) return 2;
16     int m = sqrt(n)+0.5;
17     int pf = 0;
18     long long ans = 0;
19     for(int i=2;i<=m;i++) {
20         if(n%i==0) {
21             pf++;
22             ans+=divide_all(n,i);
23         }
24     }
25     if(n>1) {pf++,ans+=n;}
26     if(pf<=1) ans++;    //是素数
27     return ans;
28 }
29 
30 int main()
31 {
32     int n;
33     int kase=1;
34     while(scanf("%d",&n),n) {
35         cout<<"Case "<<kase++<<": "<<solve(n)<<endl;
36     }
37     return 0;
38 }
View Code

 

posted @ 2017-04-02 16:41  小草的大树梦  阅读(460)  评论(0编辑  收藏  举报