Test instructions:
The number of integers in the given interval [x, y] that meet the following conditions: This number is exactly equal to the integer of K's unequal B
The sum of the power of several times.
Idea: For binary (picture excerpt from Liu Cong's discussion of digital statistics problem paper)
Now extended to B-binary
Because for each bit of the B-binary, we just need to discuss whether this bit is one, so we can convert this number to an equivalent binary number,
The method is to change the number from left to right the first bit is not 0 or one bit to 1, and to the right of all positions one, to find the binary number.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack > #include <string> #include <map> #include <set> #define EPS 1e-6#define LL long long#define PII (pair <int, int>)//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace Std;//const int MAXN = 100 + 5; const int INF = 0X3F3F3F3F; LL f[40][40];int x, y, K, b;void init () {f[0][0] = 1; for (int i = 1; i <=; i++) {f[i][0] = 1; for (int j = 1; J <= I; j + +) {F[i][j] = F[i-1][j] + f[i-1][j-1]; }}}int cal (int x, int k) {int tot = 0, ans = 0; for (int i = +; i > 0; i--) {if ((1<<i) &x) {tot++; if (Tot > k) break; x ^= (1<<i); } if ((1<< (i-1)) <=x) {ans + = F[i-1][k-tot]; }} if (tot+x = = k) ans++; return ans;} int conv (int x) {int ans = 0; Vector<int> v; while (x) {v.push_back (x%b); x/= B; } int sz = V.size (); for (int i = sz-1; I >= 0; i--) {if (V[i]==1 | |!v[i]) {ans + = (v[i]==0 0: (1<<i)); } else {for (int j = 0; J <= I; j + +) ans + = (1<<j); Break }} return ans; int main () {//freopen ("Input.txt", "R", stdin); Init (); while (scanf ("%d%d%d%d", &x, &y, &k, &b) = = 4) {//cout << conv (y) << Endl << Conv (x) << Endl; Cout << cal (Conv (y), K)-cal (CONV (x-1), k) << Endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
URAL 1057 Amount of Degrees (digital statistics)