UVa 10791 Minimum Sum LCM(素因子分解)
题意:
求几个数的最小公倍数为n的最小和。
思路:
1. 同样的素因子都放在同一个数中
2. 不同的素因子都放在不同的数中
按照这样一个方法,题目就可以解决了。
#include <cstdio> #include <cstring> #include <cstring> #include <cmath> long long int solve(int num) { int n = num; long long int ans = 0; int count = 0; for (int i = 2; i <= (int)sqrt(1.0*num); ++i) { if ((n % i) == 0) { int t = 1; while ((n % i) == 0) n /= i, t *= i;
++count; ans += t; } } if (n != 1 || count == 0) ans += n, ++count; if (count == 1) ++ans; return ans; } int main() { int n, count = 0; while (scanf("%d", &n) && n) { printf("Case %d: ", ++count); printf("%lld\n", solve(n)); } return 0; }
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------