2015
07-16

# 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.

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≤109
2≤m≤16
There are less then 10 test cases.

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≤109
2≤m≤16
There are less then 10 test cases.

10 2
30 5

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. 

2012 Asia Tianjin Regional Contest-B

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXP = 40000;
int prime[MAXP] = {0},num_prime = 0;
int isNotPrime[MAXP] = {1, 1};
int digit[MAXP];
int n,m;
/*void GetPrime()
{
for(int i = 2 ; i <  MAXP ; i ++){
if(! isNotPrime[i])
prime[num_prime ++]=i;
for(int j = 0 ; j < num_prime && i * prime[j] <  MAXP ; j ++)
{
isNotPrime[i * prime[j]] = 1;
if( !(i % prime[j]))  break;
}
}
}*/
int   sqr(int x){ return x*x;}
int   GetSqrValue(int i,int m){
int sum=0;
while (i){
sum+=sqr(i%m);
i/=m;
}
return sum;
}

void  PrintBaseM(int ans,int m){
int nPs=0;
while (ans){
digit[nPs++]=ans%m;
ans/=m;
}
for (int i=nPs-1;i>=0;--i)
if (digit[i]<10)cout<<digit[i]; else cout<<char(digit[i]+55);
cout<<endl;
}
int main()
{
//GetPrime();
while (cin>>n>>m){
int ans=0;
int N= int(sqrt(n*1.0));
for (int i=1;i<=N;++i)
if (!(n%i))
{
ans+=GetSqrValue(i,m);
if (n/i != i)
ans+=GetSqrValue(n/i,m);
}
PrintBaseM(ans,m);
}
return 0;
}

1. 长得太漂亮,无法自拍…果断的8~[食物:我很漂亮的!(强烈抗议ing)咖啡:~~~~(>_<)~~~~ (想不通中ing)]←两自恋,你要是长成那样我追你…