【Uva10791】Minimum Sum LCM【LCM】【质因数分解】

Uva上不去,只能去vjudge啦。http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19594

大白上的题。

这个题解挺详细:http://www.cnblogs.com/scau20110726/archive/2013/01/18/2866101.html


一开始没注意只有单个质因数的情况,wa了几发...


/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <cmath>

typedef long long LL;

int n;

inline LL solve(int n) {
	int _sqrtn = sqrt(n);
	LL ans = 0; int cnt = 0;
	for(int i = 2; i <= _sqrtn && n > 1; i++) if(n % i == 0) {
		LL tmp = 1; cnt++;
		for(; n % i == 0 && n > 1; tmp *= i, n /= i);
		ans += tmp;
	}
	if(cnt == 0 || n > 1) {
		cnt++;
		ans += n;
	}
	if(cnt == 1) ans++;
	return ans;
}

int main() {
	for(int cas = 1; ; cas++) {
		scanf("%d", &n);
		if(!n) break;

		printf("Case %d: %lld\n", cas, solve(n));
	}
	return 0;
}


你可能感兴趣的:(uva)