2012 天津站B題||hdu4432 進位制轉換 模擬
http://acm.hdu.edu.cn/showproblem.php?pid=4432
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.
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.
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其實就是一個模擬的問題,值得一提的是n==1要特殊考慮。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef __int64 LL;
int m;
LL n,a[555555];
int get_factor(LL n)
{
int sum=0;
memset(a,0,sizeof(a));
for(int i=1;i*i<n;i++)
if(n%i==0)
{
a[sum++]=i;
a[sum++]=n/i;
}
/*for(int i=0;i<sum;i++)
printf("%d ",a[i]);
printf("\n");*/
return sum;
}
void change(vector<int> &v,LL x)
{
// printf("====%d=====\n",x);
while(x)
{
v.push_back(x%m);
x/=m;
}
/*for(int i=0;i<v.size();i++)
printf("%d ",v[i]);
printf("\n");
*/
}
void jinzhi(LL x,int y)
{
if(x)
{
jinzhi(x/y,y);
printf("%c",x%y>9 ? x%y-10+'A':x%y+'0');
}
}
vector <int> v[555555];
int main()
{
while(~scanf("%I64d%d",&n,&m))
{
if(n==1)
{
printf("1\n");
continue;
}
for(int i=0;i<555555;i++)
v[i].clear();
int num=get_factor(n);
for(int i=0;i<num;i++)
change(v[i],a[i]);
LL sum=0;
for(int i=0;i<num;i++)
for(int j=0;j<v[i].size();j++)
sum+=v[i][j]*v[i][j];
jinzhi(sum,m);
printf("\n");
}
return 0;
}
相關文章
- PAT B1022 D進位制的A+B(進位制轉換板題,簡單模擬)
- NUMBER BASE CONVERSION(進位制轉換) 經典模擬
- 【進位制轉換】十進位制與十六進位制相互轉換方法
- Qt進位制轉換(十進位制轉十六進位制)QT
- JavaScript 進位制轉換(2進位制、8進位制、10進位制、16進位制之間的轉換)JavaScript
- 數字邏輯練習題-(二進位制/16進位制模擬)
- 進位制轉換
- 基礎問題:進位制轉換
- 十六進位制轉換為八進位制
- 十六進位制轉換為十進位制
- 進位制之間的轉換之“十六進位制 轉 十進位制 轉 二進位制 方案”
- 二進位制轉十進位制快速轉換方法
- lua之m進位制轉換為n進位制-任意進位制轉換演算法演算法
- 二進位制,八進位制,十進位制,十六進位制的相互轉換
- JavaScript 進位制轉換JavaScript
- 1.5進位制轉換
- 進位制的轉換
- JavaScript十進位制轉換為二進位制JavaScript
- shell 中轉換16進位制10進位制
- Oracle二進位制與十進位制轉換Oracle
- java中二進位制、八進位制、十進位制、十六進位制的轉換Java
- 二進位制,八進位制,十進位制,十六進位制之間的轉換
- Python 進位制互相轉換(二進位制、十進位制和十六進位制)Python
- 【進位制轉換】二進位制、十六進位制、十進位制、八進位制對應關係
- JAVA 二進位制,八進位制,十六進位制,十進位制間進行相互轉換Java
- 計算機基礎進位制轉換(二進位制、八進位制、十進位制、十六進位制)計算機
- 十進位制與二進位制互相轉換指南
- 進位制均值-進位制轉換+最大公約數
- 【轉帖】Oracle中的二進位制、八進位制、十進位制、十六進位制相互轉換函式Oracle函式
- 數的進位制轉換
- python 進位制轉換Python
- c#進位制轉換C#
- 進位制轉換圖解圖解
- 【python】進位制轉換Python
- (轉)【iOS 開發】二進位制、十進位制、十六進位制相互轉換的方法iOS
- 大話二進位制,八進位制,十進位制,十六進位制之間的轉換
- 一看就懂二進位制、八進位制、十六進位制數轉換十進位制
- python進位制轉換(二進位制、十進位制和十六進位制)及注意事項Python