zoukankan      html  css  js  c++  java
  • Minimum Sum LCM UVA

    对于一个数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
    ",++kase, nut);
    
        }
    
    
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    Cookie
    精英讲师培训笔记03-如何与台下观众有效互动
    精英讲师培训笔记02-培训师手势如何做
    精英讲师培训笔记01-提升口才的三个心法
    "怒海争锋"沙盘培训思考
    logback问题集
    spring boot2 启动过程
    Connect reset
    ELK 安装及使用
    常用中文教程网站
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9321315.html
Copyright © 2011-2022 走看看