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 1e61≤n≤1e6,且2≤b≤102\le b\le 102≤b≤10,所以可以將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【暴力+打表】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVA401 Palind
- 下一篇: Give Candies【快速幂+欧拉】