uva 10791

来源:互联网 发布:怎么查看手机端口号 编辑:程序博客网 时间:2024/06/04 20:00

参考紫书:先贴一个唯一分解定理的公式:N=P1a1P2a2P3a3......Pnan。

题意:给一个数n,求出一些数,他们的最小公倍数为n(至少两个数)。

那么根据唯一分解定理,这些数就是P1a1P2a2P3a3......Pnan。

因为这些数是互质的,所以他们就是n的最小的因子们。

求因子的时候最多有一个因子大于floor(sqrt(n) + 0.5)。如果有两个他们的乘积不是比n还大了吗?-_-

/*ID: DickensToneLANG: C++PROB: palsquare*/#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<cmath>using namespace std;const int maxn = 33;int div_num(int &n, int f){    int x = 1;    while(n % f == 0) {n = n / f; x *= f;}    return x;}void to_get(int n){    int cnt = 0, t = n;    long long ans = 0;    int m = floor(sqrt(n) + 0.5);    for(int i = 2; i <= m; i++)    {        if(n % i == 0)        {            cnt++;            ans += div_num(n, i);            //printf("%d\n", ans);        }    }   // printf("%d\n", cnt);   if(n > 1) {ans = ans + n; cnt++;}   if(cnt <= 1) ans = t + 1LL;   printf("%lld\n", ans);}int main(){    //freopen("palsquare.in","r",stdin);    //freopen("palsquare.out","w",stdout);    int n;    int kase = 1;    while(scanf("%d", &n) == 1 && n)    {        printf("Case %d: ", kase++);        to_get(n);    }    return 0;}


原创粉丝点击