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/

发布了87 篇原创文章 · 获赞 56 · 访问量 9150

猜你喜欢

转载自blog.csdn.net/Ven21959/article/details/100916682