您的位置:首页 > 其它

HDU 4432 Sum of divisors (进制模拟)

2013-08-03 20:52 447 查看
三个小函数

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: