创建于 2026/07/18 · 更新于 2026/07/18
You are given a base and a positive integer . Every non-negative integer is written in base , with leading zeros added so that all numbers involved have enough digits.
For a non-negative integer , let be the -th base- digit of counted from the least significant digit (so is the least significant digit). Define
that is, is the number of positions at which the base- digit of is **strictly smaller** than the corresponding digit of .
You must answer queries. Each query gives four integers . For that query, count the number of integers satisfying **both** of the following conditions:
1. and ; 2. and .
The first line contains three integers , and — the base, the reference integer, and the number of queries.
Each of the next lines contains four integers — the parameters of one query.
Output lines. The -th line contains a single integer — the answer to the -th query.
10 25 4
0 1 30 2
5 1 30 2
5 1 30 1
1 1 20 0
9
0
5
0
10 9 3
0 1 8 0
1 1 8 1
0 1 8 1
0
7
8