zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    如何利用Typora编写博客,快速发布到多平台?
    bios怎么开启ahci模式
    IDENTITY_INSERT 设置
    【热门测试技术,建议收藏备用】项目实战、简历、笔试题、面试题、职业规划
    k8s节点简介、移除节点、新增节点
    正确理解jmeter线程组之RampUp
    Ingress资源规范
    使用dockercompose.yml安装rabbitmq集群
    使用sonarqube对java项目进行分析
    SonarQube支持Gitlab授权登录
  • 原文地址:https://www.cnblogs.com/heqinghui/p/3233143.html
Copyright © 2011-2022 走看看