You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int l, r; // 数位DP的区间范围
int k, b; // k:个进制位为1,b进制
int a[N], al; // 用来分解b进制的数组
// 将x转化为b进制
void calc(int x, int b) {
al = 0;
memset(a, 0, sizeof a);
while (x) a[++al] = x % b, x /= b;
}
/*
7/14
OIer
*/
int res;
int main() {
cin >> l >> r >> k >> b;
// 1、将[l,r]之间所有数字进行b进制分解
for (int i = l; i <= r; i++) {
calc(i, b);
int cnt = 0;
bool bad = false; // 是不是全是0或1一旦是bad=true,表示出现了大于数字1的数位此时一票否决~
// 2、检查是不是存在大于1的大于1的是无效数字跳过
for (int j = 1; j <= al; j++) {
if (a[j] > 1) {
bad = true;
break;
}
// 3、统计一下1出现的次数
if (a[j] == 1) cnt++;
}
// 4、判断出现的次数是不是等于k,如果是,那么找到了一个答案,计数
if (!bad && cnt == k) res++;
}
printf("%d\n", res);
return 0;
}