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;
    }
  • 相关阅读:
    SD卡的控制方法(指令集和控制时序)
    MDK的优化应用(转)
    SD卡的SPI模式的初始化顺序(转)
    SD卡读写扇区注意事项(转)
    MDK下调试时提示AXF文件无法导入的解决方法(转)
    把一个 int 数字 n 格式化成16进制的字符串(前面补零成0位)
    DB9 公头母头引脚定义及连接、封装
    RS232 DB9 公头 母头 串口引脚定义
    Codeforces 91C Ski Base 加边求欧拉回路数量
    Cocos Code IDE + Lua初次使用FastTiledMap的坑
  • 原文地址:https://www.cnblogs.com/Accepting/p/11361035.html
Copyright © 2011-2022 走看看