zoukankan      html  css  js  c++  java
  • 唯一分解定理应用

    UVA 10791

    题意:

    输入n,求最少两个数,使得他们的最小公倍数为n,使他们的和最小。

    分析:根据唯一分解定理,可以得出  N = p1^n1 * p2^n2 *...* pn^nn

    即:当把pi^n1看成整体时和最小。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    using namespace std;
    typedef long long ll;
    int main()
    {
        int t=1;
        ll n;
        while(cin>>n)
        {
            if(!n)
                break;
            printf("Case %d: ",t);
            t++;
            if(n==1)
            {
                printf("2
    ");
                continue;
            }
            ll sum=0,ant=0,temp;
            int max_=sqrt(n+1);
            for(int i=2;i<=max_;i++)
            {
                int temp=1;
                if(n%i==0)
                {
                    ant++;
                    while(n%i==0)
                    {
                        temp*=i;
                        n/=i;
                    }
                    sum+=temp;
                }
               // cout<<temp<<endl;
    
                if(n==1)
                    break;
            }
           // cout<<ant<<endl;
           if(ant==0)
           {
               printf("%lld
    ",n+1);
           }
           else if(ant==1||n!=1)
           {
               printf("%lld
    ",sum+n);
           }
           else
           {
               printf("%lld
    ",sum);
           }
        }
    }
  • 相关阅读:
    MongoDB小结25
    MongoDB小结24
    MongoDB小结23
    MongoDB小结22
    MongoDB小结21
    MongoDB小结20
    MongoDB小结19
    MongoDB小结18
    hdu 4606 Occupy Cities
    hdu 4610 Cards
  • 原文地址:https://www.cnblogs.com/linhaitai/p/9975189.html
Copyright © 2011-2022 走看看