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;
}
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;
}
相关文章推荐
- HDU 4432 Sum of divisors (进制模拟)
- HDU 4432 Sum of divisors (进制模拟)
- HDU 4432 Sum of divisors
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
- hdu 4432 Sum of divisors
- HDU-4432-Sum of divisors ( 2012 Asia Tianjin Regional Contest )
- HDU 4432 Sum of divisors (水题,进制转换)
- hdu 4432 Sum of divisors(十进制转其他进制)
- hdu 4432 Sum of divisors(模拟)
- HDU 4432 Sum of divisors 2012 Asia Tianjin Regional Contest
- HDU 4432 Sum of divisors 质因数分解
- B - Sum of divisors HDU - 4432
- HDU 4432 Sum of divisors
- HDU—— 4432 Sum of divisors
- HDU 2100 (模拟进制加法)
- hdu 4432 Sum of divisors
- hdoj 4432 Sum of divisors
- HDU 4432 Sum of divisors (进制转换模板)
- hdu 5237 2015上海邀请赛 B - Base64 (进制转化,模拟)
- HUD 4432 Sum of divisors 天津现场赛B题