Sum Squared Digits Function

The Sum Squared Digits function, SSD(b, n)SSD(b, n) of a positive integer n, in base bb is defined by representing nn in base b as in: 

n = a0 + a1*b + a2*b^2 +........

then:

SSD(b, n) = a0^2 + a1^2 + a2^2 +........

is the sum of squares of the digits of the representation.  

Write a program to compute the Sum Squared Digits function of an input positive number.

Input Format

The first line of input contains a single decimal integer P(1 \le P \le 10000)P(1≤P≤10000), which is the number of data sets that follow. Each data set should be processed identically and independently.

Each data set consists of a single line of input. It contains the data set number, KK, followed by the base, b (3 \le b \le 16)b(3≤b≤16) as a decimal integer, followed by the positive integer, nn (as a decimal integer) for which the Sum Squared Digits function is to be computed with respect to the base bb. nn will fit in a 3232 bit unsigned integer.

Output Format

For each data set there is a single line of output.  The single line of output consists of the data set number, KK, followed by a single space followed by the value of SSD(b,\ n)SSD(b, n) as a decimal integer.

样例输入  

3

1 10 1234

2 3 98765

3 16 987654321

样例输出  

1 30

2 19

3 696

本题大概意思就是根据上面两个公式,给出两个参数算出SSD(b,n)的值

其实就是求n可以除尽的b的各次方的除数的和。

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    while(n --){
        int m,b,i = 0, ans = 0;
        unsigned k;
        cin >> m >> b >> k;
        while(pow(b,i) <= k){
            i++;
        }
        for(; i >=0; i--){
            ans += int(k/pow(b,i))*int(k/pow(b,i));
            k = k % int(pow(b,i));
        }
        cout << m << " " << ans << endl;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值