Sum of divisors

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2063    Accepted Submission(s): 718

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
9

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
 
现在看觉得挺简单的,第一眼看时觉得挺难就没做,谁知道人家一下就AC了,直接一般的方法做加一点优化就行了
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int vet[10000];
int main()
{
int n,m,all,sum,s,ii,i,t;
char str[17]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E'};
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==1)
{
printf("1\n");
continue;
}
all=n;sum=0;
for(i=2;i<all;i++)
{
if(n%i==0)
{
all=n/i;//注意这一步优化,最关键的一步,如果i是n的因数,那么我们直接把i和n/i一起算了,i的上界变成了n/i-1
// printf("%d %d",i,n/i);
s=0;
ii=i;
while(ii)
{
t=ii%m;
s+=t*t;
ii=ii/m;
}
ii=n/i;
while(ii)
{
t=ii%m;
s+=t*t;
ii=ii/m;
}
sum+=s;
}
}
s=0;
ii=n;
while(ii)
{
t=ii%m;
s+=t*t;
ii=ii/m;
}
sum+=s;
sum++;
t=0;
while(sum)
{
vet[t++]=sum%m;
sum=sum/m;
}
for(i=t-1;i>=0;i--)
{
printf("%c",str[vet[i]]);
} printf("\n"); }
return 0;
}

hdu4432 Sum of divisors(数论)的更多相关文章

  1. HDU4432 Sum of Divisors

    涉及知识点: 1. 进制转换. 2. 找因子时注意可以降低复杂度. Sum of divisors Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  2. Sum of divisors

    Problem Description mmm is learning division, she's so proud of herself that she can figure out the ...

  3. Project Euler 501 Eight Divisors (数论)

    题目链接: https://projecteuler.net/problem=501 题意: \(f(n)\) be the count of numbers not exceeding \(n\) ...

  4. codeforces 616E Sum of Remainders (数论,找规律)

    E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. zoj 2286 Sum of Divisors

    // f(n)表示 n的约数和 不包括自己// 给你一个m 求1 到 100万里面 f(n)<=m 的个数// 那么首先要用筛选求出所有出 f(n)// 然后就好办了 // 写好后 看见别人好快 ...

  6. hdu 4432 Sum of divisors(十进制转其他进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4432 代码: #include<cstdio> #include<cstring&g ...

  7. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

  8. GCD SUM 强大的数论,容斥定理

    GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...

  9. ZOJ2286 Sum of Divisors 筛选式打表

    我想我是和Segmentation Fault有仇,我一直以为是空间开大的问题,然后一直减少空间,还是SF,谁让n没有给范围了,qwq. 教训:以后注意输入范围和开的空间大小. #include< ...

随机推荐

  1. Jquery Ajax Get示例

      $.ajax({ type: "GET", url:"ajax_url.php", cache: false, data:{'action':'ABC',' ...

  2. 编写delegate(明天补充)

    delegate基本上是一种callback机制,让别的类在

  3. Oracle PL/SQL 多重选择句

    Oracle中语句块的基本格式: declare --变量定义,初始化赋值. begin --变量的赋值,函数调用,if,while等. end: Oracle中的语句:关系运算符:= <> ...

  4. 暑假集训(3)第三弹 -----Til the Cows Come Home(Poj2387)

    题意梗概:据说母牛在产奶的时候,因为奶量太充足,希望有人帮它挤奶,它回家就很快.我们便能喝到鲜美的 牛奶,不过,贫奶季节却大不相同,它会懒洋洋的在大草原上晃来晃去的晒太阳,而不会想到马上回家,这可不 ...

  5. 学习C++ Primer 的个人理解(十一)

    关联容器 就像是个字典, 其元素是 键 - 值 对. 关键字起到索引作用. 有序: map:关联数组:保存 健-值 对 set : 关键字既是值. multimap : 关键字可重复出现的map mu ...

  6. hdu 1316 How many Fibs?(高精度斐波那契数)

    //  大数继续 Problem Description Recall the definition of the Fibonacci numbers:  f1 := 1  f2 := 2  fn : ...

  7. 九度OJ 1410 垒积木 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1410 题目描述: 给你一些长方体的积木,问按以下规则能最多垒几个积木. 1 一个积木上面最多只能垒另一个积木. 2 ...

  8. 第36条:坚持使用Override注解

    @Override 注解只能用在方法声明中,表示被注解的方法声明覆盖了超类型中的一个声明. @Target(ElementType.METHOD) @Retention(RetentionPolicy ...

  9. android的liveview装载数据

    设置布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro ...

  10. 关于解决JQuery发送Ajax请求后,IE缓存数据不更新的问题

    http://www.cnblogs.com/lys_013/archive/2013/08/07/3243435.html 今天在做ajax页面无刷新请求后台服务器数据的时候,IE下遭遇Ajax缓存 ...