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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值