题目连接
题意 :
=(N) 把N的十进制转换为 b 进制,然后每位相加 (233) = 2 + 3 + 3 = 8
(8)=1 + 0 + 0 = 1, (7)=1 + 1 + 1 = 3
求
思路:
1)时间限制 2e9
T个样例 1e5
N 1e6
2<=b <= 10
2)如果我每次都求 , 时间复杂度 (T * N * 20)(20,是进制转化消耗的时间)
3) 应该打表 这样 T次就是O(1),然后打表需要的时间 O(9 * 20 * N)2e8就行了 + 上 查询操作是 1e5。
4)然后开个二维数组存每数个之前的b进制 的和(前缀和)
AC:
#include<iostream>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6;
ll F[11][maxn + 20];
//进制转换打表
void Solve() {
int tmp;
for(int base = 2; base <= 10; ++base) {
for(int i = 1; i <= maxn; ++i) {
tmp = i;
while(tmp) {
F[base][i] += tmp%base;
tmp /= base;
}
F[base][i] += F[base][i - 1];
}
}
}
int main() {
//freopen("inL.txt", "r", stdin);
int N, b;
Solve();
int T, Case = 0;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &N, &b);
printf("Case #%d: %lld\n", ++Case, F[b][N]);
}
return 0;
}