Minimum Sum LCM UVA - 10791(分解质因子)

对于一个数n 设它有两个不是互质的因子a和b   即lcm(a,b) = n 且gcd为a和b的最大公约数

则n = a/gcd * b;

因为a/gcd 与 b 的最大公约数也是n

且 a/gcd + b < a + b 

又因为a/gcd 与 b 互质  所以n的最小的因子和为 所有质因子的和

同理推广到多个质因子

由算术基本定理求出所有的质因子

则 nut = 所有质因子 ^ 个数 的和  自己想一想为什么把。。。

注意n为1时

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10000010, INF = 0x7fffffff;
LL primes[maxn], vis[maxn];
int ans;
void init()
{
    mem(vis, 0);
    ans = 0;
    for(int i=2; i<maxn; i++)
        if(!vis[i])
        {
            primes[ans++] = i;
            for(LL j=(LL)i*i; j<maxn; j+=i)
                vis[j] = 1;
        }
}

LL qpow(LL a, LL b)
{
    LL res = 1;
    while(b)
    {
        if(b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

int main()
{
    LL n;
    init();
    int kase = 0;
    while(cin>> n && n)
    {
        LL temp = n;
        LL nut = 0;
        int cnt = 0;
        for(int i=0; i<ans && primes[i]*primes[i] <= n; i++)
        {
            LL cnt2 = 1;
            while(n % primes[i] == 0)
            {
                n /= primes[i];
                cnt2 *= primes[i];
            }
            if(cnt2 > 1)
            {
                nut += cnt2;
            }
        }
        if(n > 1)
        {
            nut += n;
        }
        if(nut == temp)
            nut++;
        if(temp == 1)
            nut += 2;
        printf("Case %d: %lld\n",++kase, nut);

    }


    return 0;
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9321315.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值