2019上海網絡賽(L)Digit sum枚舉

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

Given N and b, you need to calculate n=1NSb(n)\sum_{n=1}^{N} S_b(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.

1T1000001≤T≤100000

1N1061≤N≤10^{6}

2b102≤b≤10

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 <bits/stdc++.h>
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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章