日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Digit sum【暴力+打表】

發布時間:2025/3/8 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Digit sum【暴力+打表】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Digit sum
33.57% 2000ms 131072K
A digit sum S_b(n)S
b
?
(n) is a sum of the base-bb digits of nn. Such as S_{10}(233) = 2 + 3 + 3 = 8S
10
?
(233)=2+3+3=8, S_{2}(8)=1 + 0 + 0 = 1S
2
?
(8)=1+0+0=1, S_{2}(7)=1 + 1 + 1 = 3S
2
?
(7)=1+1+1=3.

Given NN and bb, you need to calculate \sum_{n=1}^{N} S_b(n)∑
n=1
N
?
S
b
?
(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≤10
6

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.

樣例輸入 復制
2
10 10
8 2
樣例輸出 復制
Case #1: 46
Case #2: 13

題目大意:
先輸入一個整數TTT,代表有TTT組測試數據,下面TTT行每行輸入兩個整數n,bn,bn,b,先假設函數f(x,b)f(x,b)f(x,b)xxx轉換為bbb進制后各數位的和,例如f(8,2)=1,f(10,10)=1f(8,2)=1,f(10,10)=1f(8,2)=1,f(10,10)=1,求∑i=1nf(i,b)\sum_{i=1}^nf(i,b)i=1n?f(i,b)

解題思路:
此題無需想的太復雜,因為1≤n≤1e61\le n\le 1e61n1e6,且2≤b≤102\le b\le 102b10,所以可以將1e61e61e6以內所有的情況全都暴力打出來,最后O(1)O(1)O(1)輸出即可。

代碼:

//#pragma GCC optimize(3,"Ofast","inline") #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> #include <stack> #include <queue> #include <vector> #include <bitset> #include <set> #include <utility> #include <sstream> #include <iomanip> using namespace std; typedef long long ll; typedef unsigned long long ull; #define inf 0x3f3f3f3f #define rep(i,l,r) for(int i=l;i<=r;i++) #define lep(i,l,r) for(int i=l;i>=r;i--) #define ms(arr) memset(arr,0,sizeof(arr)) //priority_queue<int,vector<int> ,greater<int> >q; const int maxn = (int)1e5 + 5; const ll mod = 1e9+7; int get_sum(int n,int b) {int sum=0;while(n) {sum+=n%b;n=n/b;}return sum; } int sum[1000100][11]; int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);#endif//ios::sync_with_stdio(0),cin.tie(0);rep(i,1,1000000) {rep(j,2,10) {sum[i][j]=sum[i-1][j]+get_sum(i,j);}}int T;scanf("%d",&T);rep(t,1,T) {int n,b;scanf("%d %d",&n,&b);printf("Case #%d: %d\n",t,sum[n][b]);}return 0; }

總結

以上是生活随笔為你收集整理的Digit sum【暴力+打表】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。