L. Digit sum(2019 icpc上海站)

題目描述:

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