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=;
for(i=; i*i<=n; i++)
{
if(n%i==)
{
a[k++]=i;
if(i!=n/i)
a[k++]=n/i;
}
}
}
void change(int m)
{
sum=;
for(i=; 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=;
stack<char> st;
while(x)
{
tmp=x%m;
if(tmp>=)
st.push('A'+tmp-);
//b[j++]='A'+tmp-10;
else
st.push(tmp+'');
// 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("\n");
}
return ;
}
#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>)
printf("%c",'A'+(n%m)-);
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=;
for(i=;i*i<n;i++)
{
if(n%i==){
cal=;
Base(i,m);
sum+=cal;
cal=;
Base(n/i,m);
sum+=cal;
}
}
if(i*i==n){
cal=;
Base(i,m);
sum+=cal;
}
out(sum,m);
printf("\n");
}
return ;
}
/*错误代码,求解*/
#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=;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
a[k++]=i;
if(i!=n/i)
a[k++]=n/i;
}
}
}
void change(int m)
{
sum=;
for(i=;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=;
while(x)
{
tmp=x%m;
if(tmp>=)
b[j++]=tmp-+'A';
else
b[j++]=tmp+'';
x=x/m;
}
for(i=j-;i>=;i--)
{
printf("%c",b[i]);
}
printf("\n");
}
return ;
}
hdu-4432-Sum of divisors的更多相关文章
- hdu 4432 Sum of divisors(十进制转其他进制)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4432 代码: #include<cstdio> #include<cstring&g ...
- HDU 4432 Sum of divisors (水题,进制转换)
题意:给定 n,m,把 n 的所有因数转 m 进制,再把各都平方,求和. 析:按它的要求做就好,注意的是,是因数,不可能有重复的...比如4的因数只有一个2,还有就是输出10进制以上的,要用AB.. ...
- HDU 4432 Sum of divisors (进制模拟)
三个小函数 getdiv(); 求因子 getsum(); 求平方和 change(); 转换成该进制 #include <cstdio> #include ...
- hdu4432 Sum of divisors(数论)
Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU4432 Sum of Divisors
涉及知识点: 1. 进制转换. 2. 找因子时注意可以降低复杂度. Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum
Sum Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)
Sum Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Submit Status ...
随机推荐
- Python网络编程(Sockets)
一个简单的服务器 #!/usr/bin/python3 # This is server.py file import socket # create a socket object serverso ...
- CSS 再学习,文本处理
文本缩进(对p,div有效:对span无效) p {text-indent: 5em;} Tips:一般来说,可以为所有块级元素应用 text-indent,但无法将该属性应用于行内元素(span), ...
- WPF——RenderTransform特效
WPF: RenderTransform特效 WPF中的变形(RenderTransform)类是为了达到直接去改变某个Silverlight对象的形状(比如缩放.旋转一个元素)的目的而设计的,Ren ...
- Pave the Parallelepiped CodeForces - 1007B (计数)
大意: 给定A,B,C, 求有多少个三元组$(a,b,c)$, 满足$a \le b \le c$, 且以若干个$(a,b,c)$为三边的长方体能填满边长(A,B,C)的长方体. 暴力枚举出$A,B, ...
- Codeforces Round #415 (Div. 2)C
反正又是一个半小时没做出来... 先排序,然后求和,第i个和第j个,f(a)=a[j]-a[i]=a[i]*(2^(j-i-1))因为从j到i之间有j-i-1个数(存在或者不存在有两种情况) 又有a[ ...
- IOS-详解KVO底层实现
一.KVO (Key-Value Observing) KVO 是 Objective-C 对观察者模式(Observer Pattern)的实现.也是 Cocoa Binding 的基础.当被观察对 ...
- spring boot 之热部署
热部署:当发现程序修改时自动启动应用程序. spring boot使用的是spring-boot-devtools是一个为开发者服务的一个模块.其原理用了classLoader 其中一个加载不变的类, ...
- 修改oracle字符集合
SQL> conn /as sysdbaSQL> shutdown immediate;SQL> startup mountSQL> ALTER SYSTEM ENABLE R ...
- URAL 1040 Airline Company 构造,思路 难度:2
http://acm.timus.ru/problem.aspx?space=1&num=1040 题目要求在一个联通无向图中找出一种方法给边标号使得任意一个有多条边的点,边的号码的最大公约数 ...
- JS之replace
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...