Digit sum-----The Preliminary Contest for ICPC Asia Shanghai 2019

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

Given NN and bb, you need to calculate \sum_{n=1}^{N} S_b(n)∑n=1N?Sb?(n).

InputFile

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing two integers NN and bb.

1 \leq T \leq 1000001≤T≤100000

1 \leq N \leq 10^61≤N≤106

2 \leq b \leq 102≤b≤10

OutputFile

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

样例输入

210 10
8 2

样例输出

Case #1: 46
Case #2: 13
#include <stdio.h>

int main()
{
    long long t, tot, n, b, a[15], x, num, sum, i, j, N, temp;
    for(i=0;i<13;i++)
    {
        a[i] = i;
    }
    scanf("%lld", &t);
    tot = 0;
    while(t--)
    {
        scanf("%lld %lld", &n, &b);
        temp = (b-1)*b/2;
        x = 1;
        sum = 0;
        while(n>=x)
        {
            sum = sum + temp * x * (n / (x * b));
            N = n % (x * b);
            num = 0;
            for(j=0;j+x<=N;j+=x)
            {
                if(num==b) num = 0;
                sum = sum + a[num] * x;
                num++;
            }
            if(num==b) num=0;
            sum = sum + a[num]*(N-j+1);
            x *= b;
        }
        tot++;
        printf("Case #%lld: %lld\n", tot, sum);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/0xiaoyu/p/11528861.html

时间: 2024-08-30 09:43:27

Digit sum-----The Preliminary Contest for ICPC Asia Shanghai 2019的相关文章

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力) 传送门:https://nanti.jisuanke.com/t/41400 题意: 给你三个数组a,b,c,要你求有多少个三元组(i,j,k),使得 \[ \begin{array}{l}{\left|A_{i}-B_{j}\right| \leq C_{k}, \text { and }} \\ {\left|B_{j}-C_{k}\right| \leq

给定进制下1-n每一位数的共享(Digit sum)The Preliminary Contest for ICPC Asia Shanghai 2019

题意:https://nanti.jisuanke.com/t/41422 对每一位进行找循环节规律就行了. 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <io

The Preliminary Contest for ICPC Asia Shanghai 2019 L. Digit sum

题目:https://nanti.jisuanke.com/t/41422 思路:预处理 #include<bits/stdc++.h> using namespace std; int dp[11][1000001]={0}; int main() { for(int i=2;i<=10;i++) { for(int j=1;j<=1000000;j++) { int t=j; int res=0; res=j%i; dp[i][j]=res+dp[i][j/i]+dp[i][j

The Preliminary Contest for ICPC Asia Shanghai 2019

D. Counting Sequences I 暴力搜索. #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1000000007; map<vector<short>, short> m; vector<short> vec; void calc(int num1) { vector<short> tmp; if(num1) t

01背包方案数(变种题)Stone game--The Preliminary Contest for ICPC Asia Shanghai 2019

题意:https://nanti.jisuanke.com/t/41420 给你n个石子的重量,要求满足(Sum<=2*sum<=Sum+min)的方案数,min是你手里的最小值. 思路: 从最大重量的石子开始背包,每次ans+=dp[j-v[i]]就行了. 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include &

The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs

题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using namespace std; map<int,int>mp; int main() { int T; scanf("%d",&T); int n,m; int l,r; for(int i=1;i<=T;i++) { mp.clear(); scanf(&quo

The Preliminary Contest for ICPC Asia Shanghai 2019 D. Counting Sequences I

题目:https://nanti.jisuanke.com/t/41412思路:dfs           先取ai>2  2^12>3000 因此至多取11个 其余用1补           (3000*2)-(3000+2)=2998 那么需要加入2998个1 正好3000位 所以 3000是ai最大取值           计算ans时 有重复元素的排列组合 :如1112233 res=7!/(3!*2!*2!)           另外预处理阶乘及其逆元 #include<bit

The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game

题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum(S−S′))恒成立            先算01背包方案数 再从小到大排序进行退背包 #include<bits/stdc++.h> using namespace std; const int mod=1e9+7; int a[400]; int dp[150001]; int main()

The Preliminary Contest for ICPC Asia Shanghai 2019 G. Substring (滑窗+哈希)

G. Substring 哎 一直超时or超内存 然后一直改一直改 然后 是 答案错误 然后 然后 最后结论是哈希姿势不对 我在别的地方找了这个: //https://www.cnblogs.com/napoleon_liu/archive/2010/12/29/1920839.html uint32_t hash( uint32_t a) { a = (a+0x7ed55d16) + (a<<12); a = (a^0xc761c23c) ^ (a>>19); a = (a+0x