http://acm.hdu.edu.cn/showproblem.php?pid=4432
下面一种是直接模拟,一种就是进行优化了;
理解题意即可:
给你两个数:n 和m
先将n转换成m进制数,然后将各个位置上的数值平方求和,最后在按照m进制输出即可
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
#include "algorithm"
#include "iostream"
using namespace std;
int num[ 105 ] ;
int main()
{
int n , m , i ,s;
while( ~scanf( "%d%d" ,&n ,&m ) )
{
s = 0 ;
for( i = 1 ; i <= sqrt( n ) ; i++ )
{
if( n % i == 0 )
{
int k = i ;
while( k )
{
s += ( k % m ) * ( k % m ) ;
k /= m ;
}
if( i * i != n )
{
int k = n / i ;
while( k )
{
s += ( k % m ) * ( k % m ) ;
k /= m ;
}
}
}
}
num[ 0 ] = 0 ;
int t = 0 ;
while( s )
{
num[ t++ ] = s % m ;
s /= m ;
}
t-- ;
for( i = t ; i >= 0 ; i-- )
{
if( num[ i ] > 9 )
{
printf( "%c" , num[ i ] - 10 + 'A' ) ;
}
else
printf( "%d" , num[ i ] ) ;
}
printf( "\n" );
}
return 0;
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "iostream"
#define LL long long
using namespace std;
int m, buf2;
LL buf[999999];
LL cal(LL temp)
{
LL buf2 = 0, res = 0;
while (temp)
{
buf2 = temp % m;
temp /= m;
res += (buf2 * buf2);
}
return res;
}
int main()
{
LL n, res;
while (~scanf("%lld%d", &n, &m))
{
res = 0;
memset(buf, 0, sizeof(buf));
int i = 1;
for (int j = 1; j <= sqrt(n); j++)
{
if (!(n % j))
{
buf[i] = j;
i += 1;
res += cal(j);
if (j != n / j)
{
buf[i] = n / j;
i += 1;
res += cal(n / j);
}
}
}
i = 1;
while (res)
{
buf[i++] = res % m;
res /= m;
}
for (int j = i - 1; j >= 1; j--)
{
if (buf[j] <= 9)
printf ("%d", buf[j]);
else
printf ("%c", buf[j] + 'A' - 10);
}
printf ("\n");
}
return 0;
}