• 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;
    }
  • 相关阅读:
    ACM/ICPC 之 一道不太简单的DP面试题(Geeksforgeeks)
    ACM/ICPC 之 简单DP-记忆化搜索与递推(POJ1088-滑雪)
    ACM/ICPC 之 递归(POJ2663-完全覆盖+POJ1057(百练2775)-旧式文件结构图)
    ACM/ICPC 之 枚举(POJ1681-画家问题+POJ1166-拨钟问题+POJ1054-讨厌的青蛙)
    JAVA手记 JAVA入门(安装+Dos下运行)
    ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
    ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))
    ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)
    JAVA中的数据结构——集合类(序):枚举器、拷贝、集合类的排序
    JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
  • 原文地址:https://www.cnblogs.com/heqinghui/p/3233143.html
Copyright © 2020-2023  润新知