首页 > ACM题库 > HDU-杭电 > HDU 4432-Sum of divisors-模拟-[解题报告]HOJ
2015
07-16

HDU 4432-Sum of divisors-模拟-[解题报告]HOJ

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

题目大意就是找出n的约数,然后把约数在m进制下展开,各个数位的每一位平方求和,然后按m进制输出。

直接模拟即可。

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/kqzxcmh/article/details/8117989