HDU 4432 Sum of divisors (进制模拟)

三个小函数

getdiv();        求因子
getsum();     求平方和
change();     转换成该进制

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,cnt,ans,num;
int di[555555];
char str[111111];
void getdiv() {
    int up = sqrt(n);
    cnt = 0;
    for(int i=1; i<=up; i++) {
        if(n % i == 0) {
            di[cnt++] = i;
            if(n != i*i)
                di[cnt++] = n/i;
        }
    }
}

void getsum() {
    ans = 0;
    for(int i=0; i<cnt; i++) {
        int tmp = di[i];
        while(tmp) {
            int t = tmp % m;
            ans += t*t;
            tmp = tmp / m;
        }
    }
}

void change() {
    num = 0;
    while(ans) {
        int t = ans % m;
        if(t >= 10) {
            str[num++] = t - 10 + 'A';
        } else str[num++] = '0' + t;
        ans = ans / m;
    }
}

int main() {
    while(cin >> n >> m) {
        getdiv();
        getsum();
        change();
        for(int i=num-1; i>=0; i--) {
            printf("%c",str[i]);
        }
        puts("");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/jiangu66/p/3236818.html