hdu4432 - Sum of divisors

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值