問題一覧 > 通常問題

No.3308 One-time Changed Formula

レベル : / AI推定難易度 : 1150 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 62
作問者 : 👑 loop0919 / テスター : Cafe1942
お気に入りにしたユーザー ProblemId : 12788 / 自分の提出
問題文最終更新日: 2025-10-24 21:22:19
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 486 オムニバス (順位表) の他の問題:

Problem Statement

You are given a positive integer $N$ and a formula $F$ of length $N$ consisting of digits other than $0$, +, -, and *.
More precisely, you are given a string $F$ of length $N$ that conforms to $\color{red} ^{*1}$ the <expr> symbol of the following BNF.

<expr>   ::= <term> | <expr> "+" <term> | <expr> "-" <term>
<term>   ::= <number> | <term> "*" <number>
<number> ::= <digit> | <digit> <number>
<digit>  ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
$\color{red} ^{*1}$ Examples of strings conforming to the <expr> symbol (click to expand)

For example, the following strings conform to the <expr> symbol.

  • 3+14*15 : $3 + 14 \times 15$
  • 123-987 : $123 - 987$
  • 9 : $9$

On the other hand, the following strings do not conform to the <expr> symbol.

  • 0
  • -1
  • (1+2)*3
  • 9*+8

Find the number of pairs $(p, c)$ of an integer $p$ with $1 \le p \le N$ and a digit $c$ other than $\color{red}0$ that satisfy the following condition.

  • Let $G$ be the string obtained by replacing the $p$-th character of $F$ with $c$. The value of $G$ is strictly greater than the value of $F$.

Here, it can be proved that in this problem $G$ conforms to the <expr> symbol for any $(p, c)$.

You are given $T$ test cases; answer each of them.

Constraints

  • $T$ is an integer satisfying $1 \leq T \leq 10^4$.
  • Each test case satisfies all of the following constraints.
    • $N$ is an integer satisfying $1 \leq N \leq 2.5 \times 10^5$.
    • $F$ is a string of length $N$ that conforms to the <expr> symbol in the problem statement.
  • The sum of $N$ in a single input file does not exceed $5 \times 10^5$.

Input

The input is given from standard input in the following format. Here, $\mathrm{case}_t ~ (1 \leq t \leq T)$ denotes the $t$-th test case.

$T$
$\mathrm{case}_1$
$\mathrm{case}_2$
$\vdots$
$\mathrm{case}_T$

Each test case is given from standard input in the following format.

$N$
$F$

Output

Output $T$ lines. On the $t$-th line, output the answer to the $t$-th test case.

Sample

Sample 1
Input
4
7
3+14*15
7
123-987
1
9
63
122333444455555666666777777788888888999999999+1-2*3+4-5*6+7*8-9
Output
49
51
0
201

For the $1$-st test case, for example, the following hold.

  • Changing the $1$-st character to 4 gives $G =$ 4+14*15. $4 + 14 \times 15$ is strictly greater than the value of $F$.
  • Changing the $5$-th character to 9 gives $G =$ 3+14915. $3 + 14915$ is strictly greater than the value of $F$.

The number of pairs $(p, c)$ satisfying the condition is $49$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。