#include "bits/stdc++.h"
using namespace std;
// Precompute the "zebra-coin" values.
// They are defined by: coin[0] = 1, and coin[i+1] = 4 * coin[i] + 1.
// In binary these are: 1, 101, 10101, 1010101, etc.
vector<unsigned long long> computeCoins() {
vector<unsigned long long> coins;
// Use __int128 to avoid overflow when computing 4*coin + 1.
__int128 cur = 1;
unsigned long long LIMIT = 1000000000000000000ULL; // 1e18
while(cur <= LIMIT) {
coins.push_back((unsigned long long)cur);
cur = 4 * cur + 1;
}
// Push one extra coin to serve as the upper bound for the maximum digit length.
coins.push_back((unsigned long long)cur);
return coins;
}
// Given a number X (which we know has exactly L digits in our zebra numeral system)
// convert it to its digit representation (most-significant digit first).
vector<int> convertToDigits(unsigned long long X, int L, const vector<unsigned long long>& coins) {
vector<int> digits(L, 0);
// The weight for the most-significant digit is coins[L-1], then coins[L-2], down to coins[0].
for (int i = 0; i < L; i++) {
unsigned long long weight = coins[L-1-i];
digits[i] = (int)(X / weight);
X %= weight;
}
return digits;
}
// A DP routine to count, for a fixed digit length L,
// the number of zebra–numbers (represented by a vector of digits)
// between the lower bound LB and upper bound UB (inclusive)
// that have digit sum (i.e. zebra value) exactly equal to target.
// The DP state is (pos, tightLow, tightHigh, sum) where pos is the current digit index.
long long dpDigits(int pos, int L, int tightL, int tightU, int sum, int target,
const vector<int>& LB, const vector<int>& UB,
vector<vector<vector<vector<long long>>>> &memo) {
if(pos == L){
return (sum == target) ? 1LL : 0LL;
}
if(sum > target) return 0; // no need to continue if we've overshot our target.
if(memo[pos][tightL][tightU][sum] != -1)
return memo[pos][tightL][tightU][sum];
int lowDigit = (tightL ? LB[pos] : (pos==0 ? 1 : 0)); // ensure the first digit is nonzero
int highDigit = (tightU ? UB[pos] : 4);
long long ways = 0;
for (int d = lowDigit; d <= highDigit; d++){
int ntl = (tightL && d == LB[pos]) ? 1 : 0;
int ntu = (tightU && d == UB[pos]) ? 1 : 0;
ways += dpDigits(pos+1, L, ntl, ntu, sum+d, target, LB, UB, memo);
}
memo[pos][tightL][tightU][sum] = ways;
return ways;
}
// For fixed digit-length L, count numbers in [A, B] (which are guaranteed to have L-digit zebra representation)
// that have zebra value exactly 'target'.
long long countForLength(int L, unsigned long long A, unsigned long long B, int target, const vector<unsigned long long>& coins) {
if(A > B) return 0;
if(target > 4 * L) return 0; // no chance if target is too high
vector<int> LB = convertToDigits(A, L, coins);
vector<int> UB = convertToDigits(B, L, coins);
int sumDim = 4 * L + 1;
// Create a 4D DP table: dimensions: pos (0..L), tightL (0,1), tightU (0,1), sum (0..4*L)
vector<vector<vector<vector<long long>>>> memo(
L+1, vector<vector<vector<long long>>>(2, vector<vector<long long>>(2, vector<long long>(sumDim, -1)))
);
long long res = dpDigits(0, L, 1, 1, 0, target, LB, UB, memo);
return res;
}
// Main – we partition the search range [l, r] by zebra-digit length and run digit-DP on each part.
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
vector<unsigned long long> coins = computeCoins();
// For a skeptical twist: Our coins grow so fast you might suspect a magic trick—
// but math (and our DP) never lies!
while(t--){
unsigned long long l, r;
long long k;
cin >> l >> r >> k;
long long ans = 0;
// The zebra–digit representation for numbers with L digits is in [coins[L-1], coins[L]-1].
// We loop over all possible L such that coins[L-1] ≤ r.
for (int L = 1; L < (int)coins.size(); L++){
if(coins[L-1] > r) break;
unsigned long long lowRange = coins[L-1];
unsigned long long highRange = coins[L] - 1; // full range for L-digit numbers
// Restrict to our query interval.
unsigned long long A = max(l, lowRange);
unsigned long long B = min(r, highRange);
if(A > B) continue;
// If k is outside the possible zebra value range for this digit–length, skip.
if(k > 4LL * L) continue;
long long cnt = countForLength(L, A, B, (int)k, coins);
ans += cnt;
}
cout << ans << "\n";
}
return 0;
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKLy8gUHJlY29tcHV0ZSB0aGUgInplYnJhLWNvaW4iIHZhbHVlcy4KLy8gVGhleSBhcmUgZGVmaW5lZCBieTogY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