您的位置:首页 > 其它

HDU 4432 Sum of divisors (进制转换模板)

2016-07-26 20:42 387 查看
代码:
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
int n,m,an;
int change(int n,int m)
{
int i,ui=0;
while(n)
{
i=n;
ui=ui+(i%m)*(i%m);
n=n/m;
}
return ui;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int t=sqrt(n),an=0;
for(int i=1;i<=t;i++)
{
if(n%i==0)
{
an=an+change(i,m);
if(n/i!=i)
{
an=an+change(n/i,m);
}
}
}
char a[102];
int j=0;
//printf("%d\n",an);
while(an)
{
a[j++]=an%m>9?(an%m)-10+'A':an%m+'0';
an=an/m;
}
for(int i=j-1;i>=0;i--)
{
printf("%c",a[i]);
}
printf("\n");
}
}


Sum of divisors
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
4432

Description

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. 

 

Input

Multiple test cases, each test cases is one line with two integers. 

n and m.(n, m would be given in 10-based) 

1≤n≤10 9

2≤m≤16 

There are less then 10 test cases. 

 

Output

Output the answer base m.
 

Sample Input

10 2
30 5

 

Sample Output

110
112

Hint

Use A, B, C...... for 10, 11, 12...... Test case 1: divisors are 1, 2, 5, 10 which means 1, 10, 101, 1010 under base 2, the square sum of digits is 1^2+ (1^2 + 0^2) + (1^2 + 0^2 + 1^2) + .... = 6 = 110 under base 2.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: