• Sum of divisors(进制转换) HDU


    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.

    InputMultiple 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 9
    2≤m≤16
    There are less then 10 test cases.
    OutputOutput 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.
    
    题意:比较好理解
    分析:二进制转换基本应用
    AC代码:
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 const int maxn = 1e5+10;
     7 #define LL long long
     8 #define INF 0x3f3f3f3f
     9 LL n,m;
    10 int pos=0;
    11 int a[110];
    12 
    13 LL trans(LL x){
    14     LL res=0;
    15     while(x){
    16         res+=(x%m)*(x%m);
    17         x/=m;
    18     }
    19     return res;
    20 }
    21 
    22 int main(){
    23     while(~scanf("%lld%lld",&n,&m)){
    24         LL ans=0;
    25         for(int i=1;i<=sqrt(n);i++){
    26             if(n%i==0){
    27                 ans+=trans(i);
    28                 if(i!=n/i) ans+=trans(n/i);
    29             }
    30         }
    31         pos=0;
    32         memset(a,0,sizeof(a));
    33         while(ans){
    34             a[pos++]=ans%m;
    35             ans/=m;
    36         }
    37         for(int i=pos-1;i>=0;i--){
    38             if(a[i]>=10) printf("%c",a[i]-10+'A');
    39             else printf("%d",a[i]);
    40         }
    41         printf("
    ");
    42     }
    43 
    44 
    45 
    46     return 0;
    47 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    Delphi xe8 FMX StringGrid根据内容自适应列宽。
    Delphi 10.3.1 Secure File Sharing解决应用间文件共享
    分享一个求时间差大于多少秒的函数
    解决android 9上无法使用http协议
    【转】FMX 动态创建及销毁(释放free)对象
    ChinaCock界面控件介绍-TCCBarcodeCreator
    IDE Fix Pack 6.4.4 released (bugfix release)
    Android & iOS 启动画面工具
    REST easy with kbmMW #24 使用kbmMW实现JSON/XML/YAML转换成对象
    关于ElasticSearch的聚类时出现fielddata=true问题
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/11363493.html
Copyright © 2020-2023  润新知