2019上海网络赛(L)Digit sum枚举

A digit sum S b ( n ) S_b(n) Sb(n) is a sum of the base-b digits of n. Such as S 10 ( 233 ) = 2 + 3 + 3 = 8 S_{10}(233) = 2 + 3 + 3 = 8 S10(233)=2+3+3=8, S 2 ( 8 ) = 1 + 0 + 0 = 1 S_{2}(8)=1 + 0 + 0 = 1 S2(8)=1+0+0=1, S 2 ( 7 ) = 1 + 1 + 1 = 3 S_{2}(7)=1 + 1 + 1 = 3 S2(7)=1+1+1=3.

Given N and b, you need to calculate ∑ n = 1 N S b ( n ) \sum_{n=1}^{N} S_b(n) 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≤T≤100000 1T100000

1 ≤ N ≤ 1 0 6 1≤N≤10^{6} 1N106

2 ≤ b ≤ 10 2≤b≤10 2b10

OutputFile

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

样例输入复制

2
10 10
8 2

样例输出复制

Case #1: 46
Case #2: 13

题意:

给你n和b,从1到n,求每个数转换为b进制后每位的和的累加和。

链接:

https://nanti.jisuanke.com/t/41422

思路:

暴力枚举,打表

代码:

#include 
using namespace std;
int f[15][1000000+20];
int main()
{
    int sum = 0;
    for(int i = 2; i <= 10; i++) {
        for(int j = 1; j <= 1000000; j++) {
            sum = 0;
            int k = j;
            while(k) {
                sum += k % i;
                k /= i;
            }
            f[i][j] = f[i][j-1]+sum;
        }
    }
    int t;
    scanf("%d", &t);
    int Case = 1;
    while(t--) {
        int n, b;
        scanf("%d %d", &n, &b);
        printf("Case #%d: %d\n", Case++, f[b][n]);
    }
    return 0;
}

你可能感兴趣的:(枚举,网络赛,枚举)