zoukankan      html  css  js  c++  java
  • Sum of divisors

    Sum of divisors

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 52   Accepted Submission(s) : 14
    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≤10[sup]9[/sup] 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.[/hint]
     
    Source
    2012 Asia Tianjin Regional Contest
     
     1 #include <stdio.h>
     2 #include <string.h>
     3 int main()
     4 {
     5     int j,i,m,n,sum,Len,k,a;
     6     char NUM[1000];
     7     while(scanf("%d %d",&m,&n)!=EOF)
     8     {
     9         for(i=1,sum=0;i*i<=m;i++)
    10         {
    11             if(m%i==0)
    12             {
    13                 itoa(i,NUM,n);
    14                 Len=strlen(NUM);
    15                 for(j=0;j<Len;j++)
    16                 {if(NUM[j]<='f'&&NUM[j]>='a') a=NUM[j]-'a'+10;
    17                     else a=NUM[j]-'0';
    18                     sum+=a*a;}
    19                 if(i==m/i)continue;
    20                 itoa(m/i,NUM,n);
    21                 Len=strlen(NUM);
    22                 for(j=0;j<Len;j++)
    23                 {if(NUM[j]<='f'&&NUM[j]>='a') a=NUM[j]-'a'+10;
    24                     else a=NUM[j]-'0';
    25                     sum+=a*a;}
    26             }
    27 
    28         }
    29         itoa(sum,NUM,n);
    30         Len=strlen(NUM);
    31         for(i=0;i<Len;i++)
    32          {
    33             if(NUM[i]<='f'&&NUM[i]>='a')
    34                 NUM[i]-=32;
    35             printf("%c",NUM[i]);
    36         }
    37         printf("
    ");
    38     }
    39     return 0;
    40 }
    View Code
    转载请备注:
    **************************************
    * 作者: Wurq
    * 博客: https://www.cnblogs.com/Wurq/
    * Gitee: https://gitee.com/wurq
    **************************************
  • 相关阅读:
    用Java实现四则运算
    敏捷开发角色分配
    需求分析之WBS
    需求分析之NABCD
    电梯演说
    开发流程的选择
    软件团队模式的选择
    维护日程管理项目
    日程管理系统中找错误
    Android的测试
  • 原文地址:https://www.cnblogs.com/Wurq/p/3750289.html
Copyright © 2011-2022 走看看