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

题目链接

10791 - Minimum Sum LCM

题目分析

水题,刚开始看错题目了,以为和经典题目GCD&LCM一样,原来只需要求至少这么多个因数和相加最小.显然就是把所有的 prii 加起来就行了.,注意,因子数目只有一个的时候,还要加上1.

AC code

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include<cmath>
#include <cstring>
#include <map>
#define fi first
#define se second
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int MOD = 1e9+7;
const int MAX_P = 2e4+10;
const int maxn =2e5+10;

typedef long long LL;
LL gcd(LL a,LL b){
  return b == 0? a : gcd(b,a%b);
}
int factor[maxn],cnt;
int prime[maxn];
void init(){
  memset(prime,0,sizeof(prime));
  cnt =0;

  for(int i=2 ; i<maxn ; ++i){
    if(!prime[i]){
      prime[cnt++] =i;
      for( LL j= (LL)i*i ; j<(LL)maxn ; j+=i)
        prime[j] =1;
    }
  }

}

void get_factor(int n,std::map<int, int> & fact){
  for(int i=0 ; i<cnt && prime[i]*prime[i]<=n; ++i){
    while (n%prime[i]==0) {
      ++fact[prime[i]];
      n/=prime[i];
    }
  }
  if(n!=1)++fact[n];
}

LL power(LL x,LL n){
  LL res =1;
  while (n) {
    if(n&1)res*=x;
    x*=x;
    n>>=1;
  }
  return res;
}
int main(int argc, char const *argv[]) {
    //freopen("in.txt","r" ,stdin);

    int kase =0;
    int n;
    init();
      while (scanf("%d",&n) && n) {


        printf("Case %d: ",++kase );

        if(n==1){
          std::cout << "2" << '\n';
          continue;
        }
        map<int,int> m;
        get_factor(n,m);

        LL ans =0;
        for(auto kv: m){
          ans+= power(kv.fi,kv.se);
          //cout<< kv.fi<<" "<<kv.se<<" "<<"\n";
        }
        printf("%lld\n",m.size()<=1?ans+1:ans );
      }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值