For two integers a and b , we define f(a,b) as t...

創建於:2025年6月17日

使用 O4 Mini High 回答 Chat01

問題

For two integers a
and b
, we define f(a,b)
as the number of positions in the decimal representation of the numbers a
and b
where their digits are the same. For example, f(12,21)=0
, f(31,37)=1
, f(19891,18981)=2
, f(54321,24361)=3
.

You are given two integers l
and r
of the same length in decimal representation. Consider all integers l≤x≤r
. Your task is to find the minimum value of f(l,x)+f(x,r)
.

Input
Each test consists of multiple test cases. The first line contains a single integer t
(1≤t≤104
) — the number of test cases. The description of the test cases follows.

Each test case consists of a single line containing two integers l
and r
(1≤l≤r<109
).

It is guaranteed that the numbers l
and r
have the same length in decimal representation and do not have leading zeros.

Output
For each test case, output the minimum value of f(l,x)+f(x,r)
among all integer values l≤x≤r
.

Example
InputCopy
14
1 1
2 3
4 6
15 16
17 19
199 201
899 999
1990 2001
6309 6409
12345 12501
19987 20093
746814 747932
900990999 900991010
999999999 999999999
OutputCopy
2
1
0
3
2
2
1
3
3
4
3
5
12
18
Note
In the first test case, you can choose x=1
. Then f(1,1)+f(1,1)=1+1=2
.

In the second test case, you can choose x=2
. Then f(2,2)+f(2,3)=1+0=1
.

In the third test case, you can choose x=5
. Then f(4,5)+f(5,6)=0+0=0
.

In the fourth test case, you can choose x=15
. Then f(15,15)+f(15,16)=2+1=3
.

In the fifth test case, you can choose x=18
. Then f(17,18)+f(18,19)=1+1=2
.

In the sixth test case, you can choose x=200
. Then f(199,200)+f(200,201)=0+2=2
.

In the seventh test case, you can choose x=900
. Then f(899,900)+f(900,999)=0+1=1
.

In the eighth test case, you can choose x=1992
. Then f(1990,1992)+f(1992,2001)=3+0=3
.
code in java

思考

Evaluating programming task

The user is asking for a Java function to minimize a formula involving two numbers, l and r, with conditions for digits matching. They need this done efficiently for test cases up to 10,000 and values of l, r up to just under 1 billion. For this, we’ll need a solution that calculates costs for each position by comparing the digits of l, x, and r. I’ll need to write a function that minimizes the sum of cost contributions as specified.

Developing the DP solution

The task involves minimizing costs based on digit comparisons between l, r, and another number x. The cost at each position depends on whether digits match. To solve it, I’ll use dynamic programming (DP) with three states: pos, tightLow, and tightHigh. I'll iterate through possible digits for each position and store results, optimizing for cases where x is between l and r. Given that N is small (10 digits max), the approach will efficiently handle multiple test cases. I'll now write it out in Java.

Thought for 13 seconds

回答

java
import java.io.*; import java.util.*; public class Main { static String L, R; static int N; // memo[pos][tightLow?1:0][tightHigh?1:0] static int[][][] memo; static int[] lDigits, rDigits; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = Integer.parseInt(br.readLine().trim()); while (t-- > 0) { StringTokenizer st = new StringTokenizer(br.readLine()); L = st.nextToken(); R = st.nextToken(); N = L.length(); lDigits = new int[N]; rDigits = new int[N]; for (int i = 0; i < N; i++) { lDigits[i] = L.charAt(i) - '0'; rDigits[i] = R.charAt(i) - '0'; } memo = new int[N][2][2]; for (int i = 0; i < N; i++) { for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { memo[i][a][b] = -1; } } } int answer = dp(0, 1, 1); out.println(answer); } out.flush(); } // pos: current digit position [0..N) // tightLow: if prefix so far equals L's prefix (1=true, 0=false) // tightHigh: if prefix so far equals R's prefix (1=true, 0=false) static int dp(int pos, int tightLow, int tightHigh) { if (pos == N) { return 0; } if (memo[pos][tightLow][tightHigh] != -1) { return memo[pos][tightLow][tightHigh]; } int res = Integer.MAX_VALUE; int low = tightLow == 1 ? lDigits[pos] : 0; int high = tightHigh == 1 ? rDigits[pos] : 9; for (int d = low; d <= high; d++) { int nextTightLow = (tightLow == 1 && d == lDigits[pos]) ? 1 : 0; int nextTightHigh = (tightHigh == 1 && d == rDigits[pos]) ? 1 : 0; int cost = 0; if (d == lDigits[pos]) cost++; if (d == rDigits[pos]) cost++; int sub = dp(pos + 1, nextTightLow, nextTightHigh); res = Math.min(res, cost + sub); } memo[pos][tightLow][tightHigh] = res; return res; } }

Explanation of the approach:

  1. Digit DP setup. We process the numbers as digit arrays of equal length NN.
  2. State definition. Let dp(pos, tightLow, tightHigh) be the minimum total cost from digit position pos to the end, given whether we’re still “tight” to the lower bound LL (tightLow) and to the upper bound RR (tightHigh).
  3. Transitions. At each position, choose a digit dd between the allowed range:
    • If we’ve been tight to LL, dd must be L[pos]\ge L[pos]; otherwise dd can be 0.
    • If we’ve been tight to RR, dd must be R[pos]\le R[pos]; otherwise dd can be 9.
  4. Cost per digit. Choosing digit dd incurs: (d==L[pos])+(d==R[pos]) (\,d == L[pos]\,) + (\,d == R[pos]\,) which exactly matches the definition f(L,x)f(L, x) and f(x,R)f(x, R) at that position.
  5. Tightness updates. After choosing dd, we remain tight to LL if we were tight and d=L[pos]d = L[pos]; similarly for RR.
  6. Memoization. We cache results in a 3D array of size 10×2×2\le 10 \times 2 \times 2, yielding O(N×2×2×10)O(N \times 2 \times 2 \times 10) per test case, which is efficient even for 10410^4 test cases.

This solves for the minimum value of f(L,x)+f(x,R)f(L, x) + f(x, R) over all LxRL \le x \le R.

分享這個問答