2012 天津站B題||hdu4432 進位制轉換 模擬

life4711發表於2014-09-17

http://acm.hdu.edu.cn/showproblem.php?pid=4432

Problem 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≤109
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
其實就是一個模擬的問題,值得一提的是n==1要特殊考慮。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

typedef __int64 LL;

int m;
LL n,a[555555];

int get_factor(LL n)
{
    int sum=0;
    memset(a,0,sizeof(a));
    for(int i=1;i*i<n;i++)
        if(n%i==0)
        {
            a[sum++]=i;
            a[sum++]=n/i;
        }
    /*for(int i=0;i<sum;i++)
        printf("%d ",a[i]);
      printf("\n");*/
    return sum;
}

void change(vector<int> &v,LL x)
{
    // printf("====%d=====\n",x);
    while(x)
    {
      v.push_back(x%m);
      x/=m;
    }
    /*for(int i=0;i<v.size();i++)
        printf("%d ",v[i]);
      printf("\n");
    */
}

void jinzhi(LL x,int y)
{
    if(x)
    {
        jinzhi(x/y,y);
        printf("%c",x%y>9 ? x%y-10+'A':x%y+'0');
    }
}

vector <int> v[555555];
int main()
{
    while(~scanf("%I64d%d",&n,&m))
    {
        if(n==1)
        {
            printf("1\n");
            continue;
        }
        for(int i=0;i<555555;i++)
              v[i].clear();
        int num=get_factor(n);
        for(int i=0;i<num;i++)
            change(v[i],a[i]);
        LL sum=0;
        for(int i=0;i<num;i++)
            for(int j=0;j<v[i].size();j++)
                sum+=v[i][j]*v[i][j];
        jinzhi(sum,m);
        printf("\n");
    }
    return 0;
}


相關文章