HUD 4432 Sum of divisors 天津现场赛B题
Sum of divisors
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 416 Accepted Submission(s): 173
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.
Source
2012 Asia Tianjin Regional Contest
这一道是简单题目,找到因子,进制转换,然后求出各位数字的平方和。
看了大牛的做法,才明白我的那个数组 a其实并不需要,但这是我当时大脑短路想到的第一个方法,也是我第一次发表的博客,还是纪念一下
# include<stdio.h> # include<math.h> # include<string.h> int n,m; int a[1000000]; //存储因子 int out[10000];// 输出各位数字 int num;//因子数目 int sum; void f(){ //求出所有的因子 memset(a,0,sizeof(a)); for(int i=1; i<=sqrt(1.0*n); i++){ if(n%i==0){ a[num++]=i; if((n/i)!=i) a[num++]=n/i; } } } void qiuhe(){ //求出每个因子在对应进制下的各位和 for(int i=0; i<num; i++){ int k,temp=a[i]; while(temp){ k=temp%m; sum+=k*k; temp/=m; } } } void change(){ //进制转换 sum; num=0; memset(out,0,sizeof(out)); while(sum){ out[num++]=sum%m; sum/=m; } } int main(){ while(scanf("%d %d",&n,&m)!=EOF){ num=0; sum=0; f(); qiuhe(); change(); for(int i=num-1; i>=0; i--){ if(out[i]<=9)printf("%d",out[i]); else printf("%c",out[i]-10+'A'); } printf("\n"); } return 0; }