HDU 4432 Sum of divisors

题目:mmm is learning division, she's so proud of herself that she can figure out the sum of all the divisors of numbers no larger than 100 within one day!

        But her teacher said "What if I ask you to give not only the sum but the square-sums of all the divisors of numbers within hexadecimal number 100?"             mmm get stuck and she's asking for your help.

       Attention, because mmm has misunderstood teacher's words, you have to solve a problem that is a little bit different.

       Here's the problem, given n, you are to calculate the square sums of the digits of all the divisors of n, under the base m.

分析:数据较小,直接模拟即可。

#include <stdio.h>
#include <string.h>
#include <math.h>
long long res, k;
void cal(long long x)
{
    while(x)
    {
        res += (x%k) * (x%k);
        x /= k;
    }
}
void print(long long x)
{
    long long p[1000];
    int top = 0;
    int i;
    while(x)
    {
        p[top++] = x % k;
        x /= k;
    }
    for(i=top-1; i>=0; i--)
    {
        if(p[i] < 10)
        printf("%I64d",p[i]);
        else
        printf("%c",p[i]-10+'A');
    }
    putchar('\n');
}
int main()
{
    long long n, i;
    while(scanf("%I64d %I64d",&n, &k)!=EOF)
    {
        res = 0;
        for(i=1; i*i<n; i++)
        {
            if(n%i == 0)
            {
                cal(i);
                cal(n/i);
            }
        }
        if(i*i == n)
            cal(i);
        print(res);
    }
    return 0;
}

 

 

posted @ 2012-10-26 21:14  'wind  阅读(206)  评论(0编辑  收藏  举报