• hdu-4432-Sum of divisors


    /*
    Sum of divisors
    
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1996    Accepted Submission(s): 679
    
    
    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.
     
    */
    #include <iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    #define maxn 26000
    int a[maxn];
    int sum;
    int k,i,j,tmp;
    char b[maxn];
    void yinzi(int n)
    {
        k=0;
        for(i=1; i*i<=n; i++)
        {
            if(n%i==0)
            {
                a[k++]=i;
                if(i!=n/i)
                    a[k++]=n/i;
            }
        }
    }
    void change(int m)
    {
        sum=0;
        for(i=0; i<k; i++)
        {
            while(a[i])
            {
                tmp=a[i]%m;
                sum+=tmp*tmp;
                a[i]=a[i]/m;
            }
        }
    }
    void rechange(int x,int m)
    {
        j=0;
        stack<char> st;
        while(x)
        {
            tmp=x%m;
            if(tmp>=10)
            st.push('A'+tmp-10);
                //b[j++]='A'+tmp-10;
            else
            st.push(tmp+'0');
               // b[j++]=tmp+'0';
            x=x/m;
        }
        while(!st.empty())
        {
            cout<<st.top();
            st.pop();
        }
        /*if(x)
        {
            rechange(x/m,m);
            tmp=x%m;
            if(tmp>=10)
            printf("%c",'A'+tmp-10);
            else
            printf("%d",tmp);
        }*/
    }
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
    
            yinzi(n);
            change(m);
            rechange(sum,m);
            //cout<<" j= "<<j<<endl;
            /*for(i=j-1;i>=0;i--)
            {
                //printf("%c",b[i]);
                cout<<b[i];
            }*/
            printf("
    ");
        }
        return 0;
    }
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <cmath>
    using namespace std;
    int cal;
    void Base(int n,int m){
        if(n)
         {
            Base(n/m,m);
            cal+=(n%m)*(n%m);
         }
    }
    void out(int n,int m){
        if(n){
            out(n/m,m);
            if(n%m>9)
                printf("%c",'A'+(n%m)-10);
            else printf("%d",n%m);
        }
    }
    int main(){
    
      int n,m;
      int i,k,sum;
      while(scanf("%d %d",&n,&m)!=EOF){
          //k=sqrt(n+1.0);
          sum=0;
          for(i=1;i*i<n;i++)
          {
              if(n%i==0){
                cal=0;
                Base(i,m);
                sum+=cal;
                cal=0;
                Base(n/i,m);
                sum+=cal;
              }
          }
          if(i*i==n){
                cal=0;
                Base(i,m);
                sum+=cal;
          }
          out(sum,m);
          printf("
    ");
      }
      return 0;
    }
    /*错误代码,求解*/
    #include <iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    #define maxn 260000
    int a[maxn];
    int sum;
    int k,i,tmp;
    char b[maxn];
    void yinzi(int n)
    {
        k=0;
        for(i=1;i*i<=n;i++)
        {
            if(n%i==0)
            {
                a[k++]=i;
                if(i!=n/i)
                    a[k++]=n/i;
            }
        }
    }
    void change(int m)
    {
        sum=0;
        for(i=0;i<k;i++)
        {
            while(a[i])
            {
                tmp=a[i]%m;
                sum+=tmp*tmp;
                a[i]=a[i]/m;
            }
        }
    }
    
    int main()
    {
        int n,m,x,j;
        while(~scanf("%d%d",&n,&m))
        {
            yinzi(n);
            change(m);
            // rechange(sum,m);
            x=sum;
            j=0;
            while(x)
            {
                tmp=x%m;
                if(tmp>=10)
                    b[j++]=tmp-10+'A';
                else
                    b[j++]=tmp+'0';
                x=x/m;
            }
            for(i=j-1;i>=0;i--)
            {
                printf("%c",b[i]);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    java设计模式演示样例
    一步一步写算法(之排序二叉树)
    收集经常使用的.net开源项目
    jdbc连接数据库
    Android开发系列(二十二):AdapterViewFlipper的功能和使用方法
    ProgressDialog使用总结
    HDU 4916 树分治
    [Unity3D]自制UnityForAndroid二维码扫描插件
    IOS ARC和非ARC文件混用
    让子弹飞Demo版
  • 原文地址:https://www.cnblogs.com/heqinghui/p/3233143.html
Copyright © 2020-2023  润新知