L Digit sum

A digit sum Sb(n)  is a sum of the base-bbb digits of nnn. Such as S10(233)=2+3+3=8 , S2(8)=1+0+0=1  S2(7)=1+1+1=3

Given N and b, you need to calculate ∑n=1NSb(n)

InputFile

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing two integers N and b.

1≤T≤100000

1≤N≤10e6

2≤b≤10

OutputFile

For each test case, output one line containing Case #x: y, where xxx is the test case number (starting from 1) and y is answer.

样例输入复制

2
10 10
8 2

样例输出复制

Case #1: 46
Case #2: 13

 

题意:求一个数b进制,各个位数的和

一个个算的话必T,所以打表直接套之前求二进制位数的板子就过了,这次是2—10进制,简单总结下

for(int i=1; i<=maxn; i++) {
        a2[i]=a2[i>>1]+i%2;
        a3[i]=a3[i/3]+i%3;
        a4[i]=a4[i/4]+i%4;
        a5[i]=a5[i/5]+i%5;
        a6[i]=a6[i/6]+i%6;
        a7[i]=a7[i/7]+i%7;
        a8[i]=a8[i/8]+i%8;
        a9[i]=a9[i/9]+i%9;
        a10[i]=a10[i/10]+i%10;
    }
    for(int i=1; i<=maxn; i++) {
        a2[i]+=a2[i-1];
        a3[i]+=a3[i-1];
        a4[i]+=a4[i-1];
        a5[i]+=a5[i-1];
        a6[i]+=a6[i-1];
        a7[i]+=a7[i-1];
        a8[i]+=a8[i-1];
        a9[i]+=a9[i-1];
        a10[i]+=a10[i-1];
    }

AC代码  

代码太丑了,还是不写在博客里了,直接贴出来

https://paste.ubuntu.com/p/KPHFn539Q8/

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值