zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    C#调用C++ memcpy实现各种参数类型的内存拷贝 VS marshal.copy的实现 效率对比
    UGUI 事件穿透规则
    UGUI 锚点设置为四方扩充模式然后设置局部坐标为0将出现什么问题
    UNITY polygon collider不随物体旋转
    android Handler机制 消息机制
    java final finally finalize
    collection 和 collections
    java 不通过第三个字符串,实现一个字符串倒序
    ArrayList,Vector,LinkedList
    String StringBuffer stringbuilder 区别
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3379291.html
Copyright © 2011-2022 走看看