UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)

题意:输入整数n(1<=n<231),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小。输出最小的和。

分析:

1、将n分解为a1p1*a2p2……,每个aipi作为一个单独的整数时最优。

2、n为1时,len==0;n为素数时,len==1。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
vector<LL> v;
void deal(LL n){//将n分解成因子
    v.clear();
    LL m = (LL)sqrt(n + 0.5);
    for(LL i = 2; i <= m; ++i){
        if(n % i == 0){//n中的质因子
            LL tmp = 1;
            while(n % i == 0 && n > 1){
                tmp *= i;
                n /= i;
            }
            v.push_back(tmp);//由质因子i合并成的因子
        }
        if(n <= 1) break;
    }
    if(n > 1) v.push_back(n);//素数本身
}
int main(){
    LL N;
    int kase = 0;
    while(scanf("%lld", &N) == 1){
        if(!N) return 0;
        deal(N);
        LL ans = 0;
        int len = v.size();
        if(len == 0 || len == 1){//1或素数
            ans = N + 1;
        }
        else{
            for(int i = 0; i < len; ++i){
                ans += v[i];
            }
        }
        printf("Case %d: %lld\n", ++kase, ans);
    }
    return 0;
}

  

时间: 2024-08-05 17:09:05

UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)的相关文章

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  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

uva 10791 Minimum Sum LCM ( 唯一分解定理 )

使用唯一分解定理的时候不一定要打出素数表,这句话是相对上一篇来讲的.做这道题目之前我对唯一分解定理方法的理解不完全. 现在多想到了一些 唯一分解,将当前需要分解的n用因子将其分解表达.需要试因子. 因子的枚举应该是从2开始(从1开始没有意义),当当前数字n可以整除当前因子i时,就使其不断除以i,直到不能整除. 这个步骤实际上已经在根本上避免了出现像4.6这种因子在唯一分解式中的出现--之前的因子2和3已经将其代替了.所以可证明唯一分解时并不一定需要构造素数表 针对本题来说,最小公倍数的最小和,有

UVA 10791 Minimum Sum LCM (数论)

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 exa

UVA 10791 Minimum Sum LCM 数论

题目链接: https://vjudge.net/problem/UVA-10791 题目描述: 给一个数n, 让你求至少两个数的lcm是n 的, 最小和 解题思路: 唯一分解, 每个单独的素数的幂加起来就是答案 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator

uva 10791 Minimum Sum LCM

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma

10791 - Minimum Sum LCM

唯一分解定律的应用,任何数都能分解成几个质数相乘的形式,那个质数部分说明了这个质数出现的次数.所以用while()循环来求得各个质数.用k来得出每个小部分.注意到有可能这个数会被一个小质数给除尽,例如n=4,则答案要+1=5 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int ans = 100000; int vis[ans+5]; vector<int> primes; voi

Minimum Sum LCM(uva10791+和最小的LCM+推理)

L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10791 题意:输入正整数n,<注意n=2^31-1是素数,结果是2^31已经超int,用long long,>找至少两个数,使得他们的LCM为n且要输出最小的和: 思路:既然LCM是n,那么一定是n的质因子组成的数,又要使和最小,那么就是ans+=[

Minimum Sum LCM(uva 10791)

题意(就是因为读错题意而wa了一次):给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 例如12,是1和12的最小公倍数,是3和4的最小公倍数,是1,2,3,4,6,12的最小公倍数,是12和12的最小公倍数……………… 那么找出一个序列,使他们的和最小,上面的例子中,他们的和分别为13,7,28,24……显然最小和为7 /* 我们很容易可以发现,将n唯一分解之后,把所有质因数乘以次数加起来就行了.比如:12=2^2*3^1,那么ans=2^2

UVa 10791 (唯一分解) Minimum Sum LCM

题意: 输入n,求至少两个正整数,使得这些数的最小公倍数为n且和最小. 分析: 设n的分解式为,很显然单独作为一项,和最小. 这里有两个小技巧: 从2开始不断的除n,直到不能整除为止.这样就省去了素数判断的问题,而且缩短了代码量.因为最开始把所有n的2的因数都出去了,后面便不会出现n % 4 == 0的情况,这样除n的都是素数. 从2除n一直到sqrt(n),如果n不为1,则此时除“剩下”的就是n最大的质因数.减少循环次数. 1 #include <cstdio> 2 #include <