uva 10791 (gcd,lcm,最小和表示法,单个质因子总和得凑起来)

Something happened in Uzhlyandia again… There are riots on the streets… Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

Input
The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.

The second line contains n integers a1, a2, …, an (-109 ≤ ai ≤ 109) — the array elements.

Output
Print the only integer — the maximum value of f.

Example
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].

In the second case maximal value of f is reachable only on the whole array.

题意描述:输入整数(n>=1&&n<2^31),求至少两个整数,使得他们的最小公倍数为n,且这些整数的和最小。输出最小和

解题思路:唯一分解定理
我们可以想象假如使整数和最小的且最小公倍数为n的数由x1,x2···,xm这些数组成,如果其中任意两个数有相同的约数,那么我们可以将其中一个除去约数,将使整体的和更小。因此可以肯定x1,x2···xm相互之间没有约数。将n转换为质数相乘的形式,可以发现当其中的每一个项作为一个x1,x2···xm中的一个数时能使整体和最小。因此我们可以把n运用唯一分解定理进行分解,将其中的每一项相加即可。

因为 如果不互质,那么因素的个数会是多个,但是全部互质 倍数只会是1,会减少总和。

#include <bits/stdc++.h>
using namespace std;
vector<int> prime;
typedef long long ll;
int prim[101010];

void init()
{
    for(int i=2;i<101010;i++)
    {
        if(!prim[i])
        {
        prime.push_back(i);
        for(int j=i*2;j<101010;j+=i){
            prim[j]=1;
        }
        }
    }
}

ll pp(ll xx)
{
    int len=prime.size();
    long long res=0;
    ll cnt=0;
    for(int i=0;i<len&&i<=sqrt(xx);i++)
    {
        if(xx%prime[i]==0)
        {
            cnt++;
            ll sum=1;
            while(xx%prime[i]==0)
            {
                sum*=prime[i];
                xx/=prime[i];
            }
            res+=sum;
        }
    }
    if(xx>1||cnt==1)// 单质因子或是剩下一个大于sqrt(n)的质因子的情况(注:很容易证
    {
        res+=xx;// 单质因子情况下tn为1,剩余质因子情况下tn为剩余质因子数  
    }
    return res;
}

int pr(ll x)
{
    ll nn=sqrt(x);
    for(int i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
            return 0;
    }
    return 1;
}
int main()
{
    long long n;
    init();
    int cc=1;
    while(cin>>n&&n)
    {
        if(pr(n)) printf("Case %d: %lld\n",cc++,n+1 );
         else printf("Case %d: %lld\n",cc++,pp(n) );
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值