题目链接
题目分析
水题,刚开始看错题目了,以为和经典题目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;
}