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 ...
随机推荐
- 安装 bochs-x
sudo apt-get install bochs-x
- 快递100API接口调用代码示例
package com.util; import java.io.IOException; import java.io.InputStream; import java.net.MalformedU ...
- JAVA基础知识详解
1. JVM是什么 JVM是Java Virtual Mechine的缩写.它是一种基于计算设备的规范,是一台虚拟机,即虚构的计算机. JVM屏蔽了具体操作系统平台的信息(显然,就像是我们在电脑上开了 ...
- Java 字节的常用封装
一. Java 的字节 byte (字节) 是 Java 中的基本数据类型,一个 byte 包含8个 bit(位),byte 的取值范围是-128到+127. byte 跟 Java 其他基本类型的关 ...
- Linux 常用环境搭建
已有环境 python 2.6.6 jdk 1.7 —tomcat— —jenkins— —jq— —Python 2.7— —pip— —PIL— —Android SDK— —yum or apt ...
- webSocket协议与Socket的区别
WebSocket介绍与原理WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex).一开始的握手需要借助HTTP请求完成. ——百度 ...
- JavaScript变量: 变量命名原则
变量的命名相对而言没有太多的技术含量,今天整理有关于变量命名相关的原则,主要是想告诉大家,虽然命名没有技术含量,但对于个人编码,或者说一个团队的再次开发及阅读是相当有用的.良好的书写规范可以让你的Ja ...
- const关键字对C++成员函数的修饰
const对C++成员函数的修饰分为三种:1. 修饰参数:2. 修饰返回值:3. 修饰this指针.简述一下知识点如下,以后找功夫再完善. 1. 对函数参数的修饰. 1)const只能用来修饰输入参数 ...
- HDU 4540 线性DP
威威猫系列故事——打地鼠 Time Limit: 300/100 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- javascript---关于字符串和数组的方法
在学习javascript过程中,遇到过很多关于数组和字符串的一些操作.之前也总结了不少方法,可是一遇到自己用的时候,就忘了.不是忘了方法叫什么名,就是忘了方法的参数有什么,返回的是什么? 现在就再次 ...