• Sum of divisors HDU 4432 简单数学题


    Sum of divisors

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2461    Accepted Submission(s): 889


    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.
     

    Source
     

    Recommend
    zhoujiaqi2010
     注意十进制以上的字母的输出问题
    /*
     * Author:  
     * Created Time:  2013/10/19 9:31:03
     * File Name: G.cpp
     * solve: G.cpp
     */
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<stack>
    #include<set>
    #include<iostream>
    #include<vector>
    #include<queue>
    //ios_base::sync_with_stdio(false);
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    #define sz(v) ((int)(v).size())
    #define rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define repf(i, a, b) for (int i = (a); i <= (b); ++i)
    #define repd(i, a, b) for (int i = (a); i >= (b); --i)
    #define clr(x) memset(x,0,sizeof(x))
    #define clrs( x , y ) memset(x,y,sizeof(x))
    #define out(x) printf(#x" %d
    ", x)
    #define sqr(x) ((x) * (x))
    typedef long long LL;
    
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 30000;
    
    int sgn(const double &x) {  return (x > eps) - (x < -eps); }
    
    int ans;
    int cal(int n,int m)
    {
        int ans = 0;
        while(n)
        {
            int temp = n%m;
            n/=m;
            ans += temp*temp;
        }  
        return ans;
    }
    stack<int> q;
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int n,m;
        while(scanf("%d%d",&n,&m) == 2)
        {
            while(!q.empty())
                q.pop();
            
            ans = 0;
            for(int i = 1;i*i<=n;++i)
            {
                if(n%i == 0)
                {
                    //cout<<i<<endl;
                    if(n/i != i)
                    {
                        ans += cal(i,m);
                        ans += cal(n/i,m);
                    }else
                    {
                        ans += cal(i,m);
                    }
                }
            }
            
            //cout<<ans<<endl;
            while(ans)
            {
                int temp = ans%m;
                q.push(temp);
                ans/=m;
            }
            
            while(!q.empty())
            {
                int a = q.top();
                q.pop();
                if(a > 9)
                {
                    printf("%c",a - 10 + 'A');
                }else
                    printf("%d",a);
            }    
            printf("
    ");  
        }
        return 0;
    }

  • 相关阅读:
    熟悉常用的HDFS操作
    爬虫爬取小说网站
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习:词频统计
    最近在学习多元分析,有空放上来分享
    机器学习基石作业一15-20题(Python实现)
    2018十月份
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3379291.html
Copyright © 2020-2023  润新知