zoukankan      html  css  js  c++  java
  • F

    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

    被这个题目卡了好久,,,,好久

    题目大意:给你一个整数n,让你求这个n的因数构成的和最小。

    题解:我们需要将其进行分解,对n进行唯一分解定理 n = a1^p1 * a2^p2 * a3^p3…,当ai^pi作为一个单独的整数时最优。

    注意事项:

    1 分解出来的质因子数目必须大于等于2 ,比如 8 只能分出来2 所以对这一类的数值要特判

    2 素数特判,对于素数,,不能分解质因子,,特判

    3对1进行特判

    4注意n ,在分解n的时候可能会改变n的大小,所以要提前保存一下n

    5 TLE 注意 分解质数时,不能用i*i<=n 来判断要先对取根号,否则会TLE

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    
    int main(){
        ll n,m;
        int k=0;
        while(~scanf("%lld",&n,&m)&&n){
            ll nn=n;
            if(n==1) {
                printf("Case %d: %lld
    ",++k,n+1);
                continue ;
            }
            ll cnt=0;
            ll m=sqrt(n+1);
            ll ans=0;
            for(int i=2;i<=m;i++){
                if(n%i==0){
                    cnt++;
                    ll sum=1;
                    while(n%i==0){
                        sum*=i;
                        n/=i;
                    }
                    ans+=sum; 
                }
            }
            if(n>1){//这一步的判断必须放在下一步的判断前边
                cnt++;
                ans+=n;
            }
            if(cnt==1||cnt==0){
                printf("Case %d: %lld
    ",++k,nn+1);
            }
            else {
                printf("Case %d: %lld
    ",++k,ans);
            }
            
            
            
        }
        
        return 0;
    }
  • 相关阅读:
    [论文收集]Web service security (尤其是RBAC)相关的论文 [更新中]
    [文章摘录] The Case for Cloud Computing (ITPro, 2009)
    [文章摘录] 网络计算系统的分类研究 (计算机学报, 2008)
    文献综述的写法
    [转]VS2005常用快捷键大全
    什么是存储过程
    使用冒泡法对数组排序
    ASP.NET中使用Global.asax文件
    轻松掌握Ajax.net系列教程
    客户端回调实现gridView无刷新分页
  • 原文地址:https://www.cnblogs.com/Accepting/p/11361035.html
Copyright © 2011-2022 走看看