题目收集
题目列表题单
返回列表

Number

CodeforcesP0055· 1000ms· 1024 MB· ZJU Summer 2026 Contest 5原题链接
数位 dp

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

You are given a base BBB and a positive integer AAA. Every non-negative integer is written in base BBB, with leading zeros added so that all numbers involved have enough digits.

For a non-negative integer xxx, let di(x)d_i(x)di​(x) be the iii-th base-BBB digit of xxx counted from the least significant digit (so d0(x)d_0(x)d0​(x) is the least significant digit). Define

h(x)=∣{ i≥0:di(x)<di(A) }∣,h(x) = \bigl|\{\, i \ge 0 : d_i(x) \lt d_i(A) \,\}\bigr|,h(x)=​{i≥0:di​(x)<di​(A)}​,

that is, h(x)h(x)h(x) is the number of positions at which the base-BBB digit of xxx is **strictly smaller** than the corresponding digit of AAA.

You must answer TTT queries. Each query gives four integers D,L,R,KD, L, R, KD,L,R,K. For that query, count the number of integers xxx satisfying **both** of the following conditions:

1. L≤xL \le xL≤x and x+D≤Rx + D \le Rx+D≤R; 2. h(x)=Kh(x) = Kh(x)=K and h(x+D)=Kh(x + D) = Kh(x+D)=K.

Constraints

  • 10≤B≤10010 \le B \le 10010≤B≤100
  • 1≤A≤10181 \le A \le 10^{18}1≤A≤1018
  • 1≤T≤50001 \le T \le 50001≤T≤5000
  • 1≤L≤R≤10181 \le L \le R \le 10^{18}1≤L≤R≤1018
  • 0≤D≤10180 \le D \le 10^{18}0≤D≤1018
  • 0≤K≤10180 \le K \le 10^{18}0≤K≤1018

输入格式

The first line contains three integers BBB, AAA and TTT — the base, the reference integer, and the number of queries.

Each of the next TTT lines contains four integers D,L,R,KD, L, R, KD,L,R,K — the parameters of one query.

输出格式

Output TTT lines. The iii-th line contains a single integer — the answer to the iii-th query.

样例输入 #1

10 25 4
0 1 30 2
5 1 30 2
5 1 30 1
1 1 20 0

样例输出 #1

9
0
5
0

样例输入 #2

10 9 3
0 1 8 0
1 1 8 1
0 1 8 1

样例输出 #2

0
7
8

提示

  • D=0D = 0D=0 is allowed; then the second condition reduces to h(x)=Kh(x) = Kh(x)=K. - From x+D≤Rx + D \le Rx+D≤R it follows that x≤R−Dx \le R - Dx≤R−D. When R<DR \lt DR<D the range of valid xxx is empty and the answer is 000.
  • All answers fit in a 64-bit signed integer. Use 64-bit integers throughout.