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

使用唯一分解定理的时候不一定要打出素数表,这句话是相对上一篇来讲的。做这道题目之前我对唯一分解定理方法的理解不完全。

现在多想到了一些

唯一分解,将当前需要分解的n用因子将其分解表达。需要试因子。

因子的枚举应该是从2开始(从1开始没有意义),当当前数字n可以整除当前因子i时,就使其不断除以i,直到不能整除。

这个步骤实际上已经在根本上避免了出现像4、6这种因子在唯一分解式中的出现——之前的因子2和3已经将其代替了。所以可证明唯一分解时并不一定需要构造素数表

针对本题来说,最小公倍数的最小和,有些细节需要注意

将在代码中加上注释,自己分析题目时想不到这么全面,希望下次可以多想到几点,就可以省去很多修改完善代码的时间。

虽然我的数学不大好,但是喜欢做数学题,原因在于喜欢其代码的缜密性。

代码不是我的,我的没有注释,人家这个注释很好,细节都考虑到了

#include <cstdio>
#include <cstring>
#include <cmath>
int main()
{
    int n, cct=0;
    long long sum;
    while(scanf("%d", &n) && n)
    {
        printf("Case %d: ", ++cct);
        int m = sqrt((double)n)+2;
        int tn = n, flagct = 0;
        sum = 0;
        for(int i=2; i<=m; i++) // 分解质因子
            if(tn%i == 0)
            {
                ++flagct; // 记录质因子个数
                int tem = 1;
                while(tn%i == 0)
                {
                    tem *= i;
                    tn /= i;
                }
                sum += tem;
            }
        if(n == tn) // 本身为素数的情况
            sum = (long long)n + 1; // n也必须是long long(2147483647 + 1)
        else if(flagct == 1 || tn != 1) // 单质因子或是剩下一个大于sqrt(n)的质因子的情况(注:很容易证明,剩下的质因子个数最多为一个)
            sum += tn; // 单质因子情况下tn为1,剩余质因子情况下tn为剩余质因子数
        printf("%lld\n", sum);
    }
    return 0;
} 

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

时间: 2024-08-02 19:50:12

uva 10791 Minimum Sum LCM ( 唯一分解定理 )的相关文章

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 (数论相关!)

题目链接: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 数论

题目链接: 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

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> #includ

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+=[

Choose and divide UVA - 10375(筛素法+唯一分解定理的应用)

通过题目给的定义C(m,n)=m!/(n!(m-n)!),以及题目要求计算的C(p,q)/C(r,s)联立可得 p!s!(r-s)!/q!r!(p-q)! 看到这个式子,我们可以分析一下,我们可以将每个阶乘,都通过唯一分解定理将它们分解 (具体教程可见:https://blog.csdn.net/qq_39439314/article/details/78270905) 所以,首先我们应该先求出10000以内的所有素数,然后通过唯一分解定理将各个阶乘都分解,求出所有存在的可用质数以及对应的质数的

UVa 10375 Choose and divide (唯一分解定理)

题目 题目大意 已知\(C(m, n) = m! / (n!(m - n)!)\), 输入整数\(p\), \(q\), \(r\), \(s\)(\(p ≥ q\), \(r ≥ s\), \(p\), \(q\), \(r\), \(s ≤ 10000\)), 计算\(C(p, q) / C(r, s)\).输出保证不超过\(10^8\), 保留\(5\)位小数 题解 这道题还是挺水吧... 首先如果直接算出\(C(p, q)\)和\(C(r, s)\)是肯定不可能的, C++存不下这么大的