PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000194. Tìm các cơ số hợp lệ (Base n)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tìm các cơ số hợp lệ (Base n)

Base n

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given a decimal digit string XX of length 1 to 60 with no leading zero and an integer MM, let dd be the maximum digit of XX.

For each integer base b≥d+1b\ge d+1, interpret XX as a representation in base bb. Its value is

Vb(X)=∑i=0∣X∣−1xib∣X∣−1−i.V_b(X)=\sum_{i=0}^{|X|-1}x_i b^{|X|-1-i}.

Count the distinct integer values Vb(X)V_b(X) not exceeding MM. If XX contains only one digit, every valid base gives the same value, so the answer is either 0 or 1.

Input

  • First line: the digit string XX.
  • Second line: the integer MM.

Output

  • Print the number of distinct representable integers not exceeding MM.

Subtasks

  • Subtask 1 (20 points): 1≤∣X∣≤61\le |X|\le 6; XX has no leading zero; 1≤M≤100000001\le M\le 10000000.
  • Subtask 2 (30 points): 1≤∣X∣≤301\le |X|\le 30; XX has no leading zero; 1≤M≤10000000000001\le M\le 1000000000000.
  • Subtask 3 (50 points): 1≤∣X∣≤601\le |X|\le 60; XX has no leading zero; 1≤M≤10000000000000000001\le M\le 1000000000000000000.

Examples

Input

22
10

Output

2

Explanation

For X=22X=22 and M=10M=10, the largest digit is d=2d=2, so the smallest base is 33.

Base bb Value of 22 Valid?
3 2⋅3+2=82\cdot3+2=8 Yes
4 2⋅4+2=102\cdot4+2=10 Yes
5 2⋅5+2=122\cdot5+2=12 No

Every larger base also fails, so the answer is 22.