题目描述:
A digit sum Sb(n)S_b(n)Sb(n) is a sum of the base-bbb digits of nnn. Such as S10(233)=2+3+3=8S_{10}(233) = 2 + 3 + 3 = 8S10(233)=2+3+3=8, S2(8)=1+0+0=1S_{2}(8)=1 + 0 + 0 = 1S2(8)=1+0+0=1, S2(7)=1+1+1=3S_{2}(7)=1 + 1 + 1 = 3S2(7)=1+1+1=3.Given NNN and bbb, you need to calculate ∑n=1NSb(n)\sum_{n=1}^{N} S_b(n)∑n=1NSb(n)
输入:
The first line of the input gives the number of test cases, TTT. TTT test cases follow. Each test case starts with a line containing two integers NNN and bbb.1≤T≤1000001 \leq T \leq 1000001≤T≤1000001≤N≤1061 \leq N \leq 10^61≤N≤1062≤b≤102 \leq b \leq 102≤b≤10
输出:
For each test case, output one line containing Case #x: y, where xxx is the test case number (starting from 111) and yyy is answer.
样例输入:
2
10 10
8 2
样例输出:
Case #1: 46
Case #2: 13
这道题应该算是这一组题目中的签到题吧,不卡时间,不卡空间,直接打表求解
code:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int num[15][1000005];
int jinzhi(int n,int m)
{
int res=0;
while(n)
{
res+=n%m;
n/=m;
}
return res;
}
int main()
{
memset(num,0,sizeof(num));
for(int i=2;i<=10;i++)
{
for(int j=1;j<=1000000;j++)
{
num[i][j]=num[i][j-1]+jinzhi(j,i);
}
}
int ttt;
scanf("%d",&ttt);
for(int kk=1;kk<=ttt;kk++)
{
int n,m;
scanf("%d%d",&n,&m);
printf("Case #%d: %d\n",kk,num[m][n]);
}
return 0;
}