We need to parse the problem: "Panda is fond of numbers. Given a number, he subtracts it with squares of any one particular digit of that number to get new numbers. This operation can be applied any number of times (possibly zero) till he obtains a pandatic number. If he is able to reach to a pandatic number then he wins. A pandatic number is a number which can be expressed in the form A^A, where A is a positive integer."
Interpretation: Given a starting integer N, you can repeatedly perform the following operation: choose one digit d of the current number (presumably in base 10 representation) and subtract the square of that digit? But description: "he subtracts it with squares of any one particular digit of that number" -> ambiguous. It could mean: From the current number, he subtracts the square of any one digit that appears in its decimal representation. That yields a new number (which could be negative? Possibly but then maybe not allowed). The problem constraints allow N up to 1e6, T up to 1e5. So we need to precompute something.
But we must be careful: The operation can be applied any number of times, possibly zero. So if N itself is pandatic (A^A), output Yes.
Otherwise, we can apply operations to transform N into some other numbers. The operation: For a number X, consider its decimal digits (presumably each digit is 0-9). For each digit d, we can form X' = X - d^2. (But is it subtract d^2 exactly? "subtracts it with squares of any one particular digit" might mean he subtracts the square of a digit from the current number. Or could be "subtracts the squares of any one particular digit" i.e., subtracts multiple? But "squares" plural? Actually "squares" suggests the square of that digit. I think it's subtract d^2.)
We need to check if there exists a finite sequence of such operations leading to a number that is pandatic. Note that the operation may produce negative numbers; but if we get negative, can we continue? Possibly yes, but negative numbers have digits? The digits of a negative number? Usually the representation of a negative number would have a minus sign, but digits are ambiguous. Typically such problems consider only non-negative numbers, or consider the absolute value? Let's check sample: For 3, they considered 3 - 3^3 = -6. Wait! They wrote: "3 - 3^3 = -6". That's a cube, not square! They used 3^3 = 27? Actually 3^3 = 27, so 3 - 27 = -24, not -6. Something's off. Let's read sample explanation: "Case 2: 3 is not a pandatic number, the answer for this is "No". 3 - 3^3 = -6 is also not a "pandatic number". Hence the final answer is "No"." That's contradictory: 3^3 = 27, but they got -6. So maybe they meant "3 - (3^2) = -6"? 3 - 9 = -6. That would be -6. So they wrote 3^3 but likely meant 3^2? Possibly a typo? But then they wrote "3 - 3^3 = -6" which is incorrect mathematically. In case 3, they wrote: "13 - 1^2 = 12 is not a pandatic number. 13 - 3^2 = 4 is a pandatic number." So it is indeed square: 1^2=1, 3^2=9, 13-9=4. So they used squares. So sample case 2 they probably meant 3 - 3^2 = -6. So we are correct: operation: subtract square of a digit.
But in case 2 they considered the result -6 and said it's not pandatic. So they allowed negative numbers, but pandatic numbers are defined as A^A with A positive integer, so pandatic numbers are positive: 1^1=1, 2^2=4, 3^3=27, 4^4=256, 5^5=3125, 6^6=46656, 7^7=823543, 8^8=16777216, 9^9=387420489, etc. So negative numbers cannot be pandatic. But they considered the operation and said it's not pandatic. So they allow negative numbers as intermediate states? Possibly, but if we get a negative, we could continue? But negative numbers have digits? Usually we consider decimal representation ignoring sign. In many programming problems, digits of a negative number are considered as digits of its absolute value? Or perhaps we cannot apply operation on negative numbers because digits are ambiguous. In sample, they didn't continue from -6. Could they have? Possibly not because the operation is defined only for numbers? Usually they consider the digits of the number as in its decimal representation (including sign? not). I'd assume we only consider non-negative integers for operation, because digits are defined for non-negative numbers. But the sample seems to allow checking -6 (maybe just for demonstration) but then they stop. Since -6 is negative, they might not apply further operations, as they only consider subtracting squares from current number if it has digits? But -6 has digits '6'? If we treat its absolute value, digits are 6, but the number is negative. The problem statement: "Given a number, he subtracts it with squares of any one particular digit of that number". It doesn't specify non-negative. But most people assume the number is positive. However, as we are to decide if we can eventually get a pandatic number, and pandatic numbers are positive, it might be possible that we go through negative numbers and then later become positive? But subtraction always reduces (or remains same if digit 0?), so X' = X - d^2 ≤ X, because d^2 ≥ 0. So the process is non-increasing. So if we start with a positive number, after any number of operations, the numbers we get will be ≤ the starting number (could become negative). So we cannot increase the number again. So we only go downwards. So if we don't hit a pandatic number on the way down (including possibly negative numbers), we can't later get positive pandatic number. So we can just simulate from N downwards? But we have choices: which digit to subtract.
So the problem reduces to: Starting from N, we can subtract the square of any digit present in the current number's decimal representation (possibly repeated). We want to know if there exists a sequence of such subtractions that results in a number of the form A^A (where A is positive integer). Since the process is deterministic in a sense but with branching, we need to check reachability.
Given constraints: N up to 1e6, T up to 1e5. So we could precompute for all numbers from 0 up to some max (maybe up to 10^6) whether we can reach a pandatic number. But note that operation can go below 0, but we might only need to consider numbers up to maybe a little above 10^6 because subtractions are at most 9^2 = 81 per step, and N up to 10^6, so worst-case depth could be large, but numbers could become negative many times? Actually if we keep subtracting, we go negative eventually. But if we only care about reaching pandatic numbers, and pandatic numbers are positive (except maybe 0? 0 is not of form A^A because A positive integer yields >=1). So the only pandatic numbers in range up to 10^6? Let's list them: 1^1=1, 2^2=4, 3^3=27, 4^4=256, 5^5=3125, 6^6=46656, 7^7=823543 ( > 1e6? Actually 7^7 = 823543, which is less than 1e6? 823543 < 1,000,000, so yes it's within 1e6. 8^8 = 16,777,216 > 1e6. So pandatic numbers up to 1e6 are: 1, 4, 27, 256, 3125, 46656, 823543. Also possibly 0? No, because A positive. So we have 7 pandatic numbers.
Now the operation is subtracting a square (0 to 81). So we can think of a directed graph where nodes are integers (maybe from -something to max). But we can bound the search: Starting from N (up to 1e6), we can only subtract up to 81 each step, so numbers can go down to negative values arbitrarily far if we keep applying operations, but is that useful? Since pandatic numbers are positive, we need to exactly land on one of those positive numbers. If we go below the smallest pandatic number (1), we cannot come back up (since we only subtract). So if we ever go below 1, we can never reach a pandatic number (since pandatic numbers are >=1). The only chance to reach pandatic number is to hit exactly one of those positive numbers >=1. So any path that goes below 1 without hitting pandatic is a dead end. So we can restrict our state space to numbers >=0? Actually 0 might be reachable but is not pandatic; from 0, digits? 0 has digit 0, subtracting 0^2 = 0 would keep at 0, but that's a loop, but you cannot reach pandatic from 0 because you cannot increase. So 0 is a sink (or loop) but not pandatic. So from 0 you cannot win. So we can ignore states < 1.
Thus we only need to consider numbers from 1 up to max(N, max pandatic). Since N up to 1e6, we can precompute for all numbers up to, say, 1e6, whether it's possible to reach a pandatic number via these operations. However, note that from a number, you can subtract squares of digits, which could be up to 81, so you can jump to numbers less than it. So we can compute backwards: we want to know all numbers that can reach a pandatic number. Starting from pandatic numbers as winning states, we can do reverse BFS: For each winning state x, for each possible previous number y such that from y you can go to x by subtracting a square of some digit of y. That is, y - d^2 = x, where d is a digit of y. So y = x + d^2, and d must be a digit of y. So we need to find y such that y > x and y's decimal representation contains a digit d where y - d^2 = x. This is a kind of reverse edge.
Alternatively, we can compute forward with DP/memoization: For each number n (1..MAX), compute if any move leads to a winning state. Since numbers only decrease, we can process numbers in increasing order: For n from 1 to MAX, we check its moves: For each digit d in n, compute next = n - d^2. If next >= 1 and DP[next] is winning, then n is winning. But careful: If next < 1, we consider that losing because cannot lead to pandatic (since only positive pandatic). Also if next == 0, no. So DP[n] = OR_{d in digits(n)} (next >= 1 and DP[next] == True). However, this requires DP[next] already computed if next < n. Since next = n - d^2 < n (unless d=0, then next=n, but d=0 gives same number? Actually digit 0 yields d^2=0, so n-0 = n, which is a self-loop. That operation is allowed? If the digit 0 is present, then you can subtract 0 and stay at same number. That could cause infinite loops. But is it considered a valid move? "subtracts it with squares of any one particular digit of that number" - if the digit is 0, subtracting 0 yields the same number. So you could stay forever. But does that help? If you are at a number that is not pandatic, you could stay, but that doesn't lead to pandatic. However, if you have a self-loop, then from that state, you could also choose other digits. So the existence of a self-loop doesn't affect reachability: if you can stay, you might never progress, but we ask if there exists a sequence that leads to pandatic. So we can ignore the self-loop because it doesn't help; we need to consider whether there exists a path that eventually leads to pandatic. So if we have a self-loop only, we are stuck. But if we have other moves that lead to pandatic, we can take them. In DP, we consider OR over all moves; for d=0, the move leads to same state n, which would create a cycle: DP[n] would depend on itself. If we include that move, the recurrence becomes DP[n] = DP[n] OR other moves, which leads to an equation: if there is any other winning move, DP[n]=True; else, if only d=0 moves, then DP[n] = DP[n], which is ambiguous. So we should either exclude d=0 moves, or treat them as not helpful. So we should not consider the move that stays at same state because it doesn't help to reach pandatic unless DP[n] is already true, which is what we're computing. So including it would make the recurrence cyclic and we need to solve fixed points. Typically for such game/reachability with non-strictly decreasing moves, we can do recursion with memo and handle cycles by marking visited and assuming losing if only self loops? Since we only care about existence of a path to pandatic, and the operation can be applied any number of times, but we can skip the self-loop. So we can just ignore digit 0, because subtracting 0 doesn't change the number, so it's irrelevant; we can consider we are allowed to not use that digit. So in DP, we can consider only digits d where d>0, because subtracting 0 doesn't change anything. However, if the number contains only digit 0 (i.e., number 0) then it's a sink; but N >=1 so digits contain non-zero digits typically. So ignoring d=0 is safe.
Thus DP approach: For each n from 1 to MAX (max N among test cases), we compute win[n] = OR_{d in digits(n), d>0} (n - d^2 >= 1 ? win[n - d^2] : False). Since n - d^2 < n (when d>0), DP values for smaller numbers are already computed if we process in increasing order. So it's straightforward. But careful: n - d^2 could be larger than n? No, because d>0 => d^2 >=1, so n-d^2 < n. So monotonic. So we can compute bottom-up from 1 to MAX. This is O(MAX * number_of_digits) where MAX up to 1e6, digits at most 7 (since 1e6 is 7 digits? Actually up to 1,000,000 is 7 digits, but 1e6 is 1,000,000, which is 7 digits? 1,000,000 has 7 digits (1 and six zeros). So at most 7 digits, fine.
We need to also consider if the number itself is pandatic, then win[n] should be true even without any moves (zero operations). So we can precompute pandatic numbers set, and set win[n] = True for those numbers initially. Then for other n, compute as above.
But wait: If n itself is pandatic, we can stop, so win[n] true.
Now the DP will compute if there exists a path from n to any pandatic number by applying operations that strictly decrease the number (since d>0). However, is it possible that we need to use a move with d=0? That wouldn't change the number, so it doesn't help. So ignoring is fine.
Now one nuance: The operation says "subtracts it with squares of any one particular digit of that number". It could be interpreted as "subtracts the squares of any one particular digit" meaning that digit's square is subtracted. So we are correct.
Thus for each test case N, if win[N] is true, output "Yes", else "No".
But we need to confirm with sample: N=1: pandatic => Yes. N=3: digits: 3 => d=3 => next = 3-9 = -6 (<1) => false. So win[3] = false. But also is 3 pandatic? No. So output No. N=13: digits: 1 and 3. For d=1: next=12. For d=3: next=4. win[4]? 4 is pandatic => true. So win[13] = true. So matches sample.
But what about numbers where you could go to negative but then from negative? They didn't consider. So our DP only considers positive next states. That seems consistent: if you go negative, you cannot reach a positive pandatic number because further moves would make it more negative? Actually from negative number, if we consider its digits (maybe ignoring sign), we could subtract a square and become less negative? Wait, if number is negative, e.g., -6. Digits of -6? Usually we consider the decimal representation of the absolute value? But it's ambiguous. Usually in such problems, numbers are considered as natural numbers (non-negative). So I think it's safe to assume that the operation is defined only for non-negative integers? But the sample considered -6 as a result but didn't continue. They might have considered it as a terminal because it's negative and maybe they cannot apply further operations. In the problem statement: "Given a number, he subtracts it with squares of any one particular digit of that number to get new numbers." It doesn't explicitly restrict to positive, but typically contest problems assume non-negative integers when talking about digits. So I'd guess we only consider non-negative numbers. But the sample shows they considered -6 as a possible result but not further. They likely would not allow further moves from negative numbers because digits are not defined. So in DP, we can consider that if a move leads to a number < 1, it's a dead end (cannot lead to pandatic). That's what we did.
Now need to check if there is any tricky scenario where using a digit 0 could be useful? Suppose you have a number with digit 0, and if you subtract 0, you stay same. That doesn't help progress. But if you have other digits, you will subtract something. So ignoring 0 is fine.
Now, DP approach works for N up to 1e6. But T up to 1e5, so we precompute win array up to max N among all test cases, and answer each query in O(1).
Potential pitfall: The operation can be applied any number of times, possibly zero. So if N itself is pandatic, win[N] true. Good.
But we need to confirm if we should consider numbers > MAX? For some N up to 1e6, moves might go to numbers slightly less than N, but always within range <=N. So DP from 1 to MAX where MAX = maximum N among test cases. But there is also pandatic number 823543 > 1e6? Actually 823543 < 1e6? Let's compute: 7^7 = 823543. Yes, that's less than 1e6 (1,000,000). 8^8 = 16,777,216 > 1e6. So max pandatic within range is 823543. So all pandatic numbers up to 1e6 are within our DP range. However, what about numbers that might lead to pandatic numbers > 1e6? For N up to 1e6, could we ever reach a pandatic number > 1e6? Since we are subtracting, we only go down, so we cannot reach numbers larger than N. So any pandatic number we could reach must be <= N. Since N <= 1e6, pandatic numbers > 1e6 are > N, so unreachable. So we only need to consider pandatic numbers <= 1e6.
Thus DP for 0..maxN inclusive is sufficient. But careful: Some numbers might be slightly larger than maxN? Not needed.
Now, DP: For each n from 1 to maxN:
- if n is pandatic: win[n] = true.
- else: For each digit d in n (excluding 0 if we choose), let m = n - d*d. If m >= 1 and win[m] true, then win[n] = true.
We need to ensure we process n in increasing order so that when we compute win[n], win[m] for m < n is already known. This works because m = n - d^2 < n. But what about d=0? m = n, which is same, we ignore.
Edge case: n - d^2 could be 0. Then m=0, which is not >=1, so we treat as false. 0 is not pandatic.
Also note that if n contains digit 0 only? Not possible for n>=1 because at least one non-zero digit.
Now about the size: maxN could be up to 1e6. That is fine. We need to compute digits for each n. Could precompute or compute on the fly.
But we need to be careful about the definition of pandatic: A^A where A is positive integer. So 1^1 = 1, 2^2=4, 3^3=27, 4^4=256, 5^5=3125, 6^6=46656, 7^7=823543, 8^8=16777216, etc. So we can generate all such numbers up to maxN.
Now, let's test some small numbers manually to see if there are any cases where a number may reach pandatic via multiple steps. Example: 10: digits 1 and 0. d=1 => 10-1=9. Is 9 pandatic? No. Check win[9]? digits 9 => 9-81 = -72 => false. So win[9] false. So win[10] false. But maybe 10 -> 1? Not possible: 10-1^2=9; 10-0^2=10 (loop). So false.
11: digits 1 and 1 (two 1s). d=1 => 10. 10 false. So false.
12: d=1 => 11; d=2 => 12-4=8. 8? 8 not pandatic; digits of 8: 8-> -64 negative. So false.
13 -> true as we saw.
14: d=1 =>13 (true) => so true. Also d=4 =>14-16=-2 false. So 14 true.
15: d=1 =>14 true => true.
16: d=1 =>15 true => true? But 15 is true, so 16 true. Also d=6 =>16-36=-20. So true.
17: d=1=>16 true; so true.
18: d=1=>17 true; so true.
19: d=1=>18 true => true.
20: digits 2 and 0. d=2 => 20-4=16 true => true. So many numbers are true.
We can see that many numbers may be winning.
But we must confirm if any cycles could cause issues: Since all moves go to strictly smaller numbers when d>0, the graph is acyclic (strictly decreasing). So DP bottom-up works.
Now consider digit 0: if we ignore d=0, fine. But what about numbers like 100? digits: 1,0,0. Only non-zero digit is 1. So move to 99. 99? digits 9 and 9. d=9 =>99-81=18 (true), so 99 true? Let's compute: 18 we know true? 18->17 true, so yes. So 100 should be true. DP would compute 99 first, then 100 sees 99 is true. Good.
Now, what about numbers that are exactly pandatic? Done.
Now confirm with constraints: T up to 1e5, N up to 1e6. So DP O(1e6 * digits) ~ 1e6*7=7e6, fine.
Now one more nuance: The problem might have a subtlety: The operation says "subtracts it with squares of any one particular digit". Could it be interpreted as "subtracts the square of any digit from the number"? We assumed d^2. But the sample uses 3^2. So that's fine.
Now let's think about negative numbers and zero. The problem statement: "he subtracts it with squares of any one particular digit of that number to get new numbers." Usually "subtracts it with squares" might mean he subtracts the number by the square of a digit. So we are correct.
Thus solution: Precompute win array for all numbers up to max_N (maximum input). Since inputs can be up to 1e6, but maybe some inputs are bigger? Constraint says 1 ≤ N ≤ 10^6 for subtask 2 (90 points). So max N=1e6.
But we must also consider that during DP, some n - d^2 may be less than 1. So we treat those as losing. Also note that if n itself is pandatic, win[n] true.
Now we also need to consider if the operation can be applied zero times, so initial number itself could be pandatic, we already covered.
Now we need to output "Yes" or "No".
One possible edge: N=0? But constraints N>=1.
Now precomputation: First generate pandatic set: For A from 1 upwards while A^A <= maxN, compute p = A^A, add to set. We need to know maxN. Since we don't know test cases ahead, we can precompute up to 1e6+ maybe some margin. Actually we can precompute for all numbers up to 1,000,000. Since max N = 1e6, we can set MAX = 1000000.
Now implement: array win[MAX+1] initialized false. For A=1; compute p=1; while p <= MAX: win[p]=true; A++; p = A^A; But careful: A^A grows fast; we can precompute for A up to maybe 10 because 10^10 is huge. But we can compute until p > MAX.
Compute p=1^1=1, 2^2=4, 3^3=27, 4^4=256, 5^5=3125, 6^6=46656, 7^7=823543, 8^8=16777216 > 1e6. So only A=1..7.
Now for n from 1 to MAX:
If win[n] is already true, skip (but it's fine to still compute OR, but we can set true initially). Else, compute digits of n. For each digit d (1..9 if present) compute next = n - d*d. If next >=1 and win[next] is true, set win[n]=true and break.
We can loop n from 1 to MAX. Since for n < some pandatic, win[n] might become true by reaching pandatic later? But note that win[next] will be computed already because next < n. So we need to process in increasing order. That's fine.
Now complexity: For each n we need to extract digits. We can either convert to string, or use a loop. Since MAX=1e6, it's fine.
One more thought: The DP as described yields "Yes" if there exists a sequence of moves that strictly decreases and eventually lands on pandatic. However, is it possible that the optimal path includes a step where you subtract a digit's square and the resulting number is larger than the original? No, subtraction never increases.
Thus solution seems correct.
But wait: Could there be a scenario where the best path includes a move that uses digit 0? As argued, digit 0 yields same number, so it's not useful. However, consider if we have a number that contains only digit 0? Only number 0 itself, but N>=1. So no.
Now, what about numbers like 1000? digits: 1 and zeros. So only move to 999. That's fine.
Now, any possibility of infinite loops due to digit 0? Not a problem.
Now confirm with some borderline: N=2: digits: 2 => next = 2-4=-2 (<1) => false; not pandatic => false. So output No. Seems plausible.
N=4: pandatic => Yes.
N=27: pandatic => Yes.
N=256: pandatic => Yes.
Now let's test N=5: digits:5 =>5-25=-20 false => No.
N=6: digits:6 =>6-36=-30 false => No.
N=7: 7-49=-42 false => No.
N=8: 8-64=-56 false => No.
N=9: 9-81=-72 false => No.
N=10: we said false.
N=11: false.
N=12: false.
N=13: true.
N=14: true.
N=15: true.
N=16: true.
N=17: true.
N=18: true.
N=19: true.
N=20: true.
N=21: digits 2 and 1: d=2 => 21-4=17 true => true; d=1 =>20 true => true.
So many numbers become true.
We can check N=23: digits 2,3. d=2 =>21 true; d=3 =>23-9=14 true. So true.
Seems many numbers will be true.
Now, is there any scenario where DP fails because we need to consider digits that are not present in the original number but appear after subtraction? Wait, the operation says we subtract the square of a digit of the current number. So at each step, we look at digits of the current number. In our DP, for a number n, we consider all digits present in n. That's correct.
Now, for the reverse perspective: When we compute win[n], we assume that from n we can go to any n - d^2 where d is a digit in n. So DP is correct.
Thus solution: Precompute win up to maxN.
But we need to be careful about large T up to 1e5, so fast I/O.
Now, potential issue: maxN might be up to 1e6, but test cases may have N up to 1e6, but we also need to consider numbers up to maybe a bit higher? Not really, since moves only go down.
Now, are there any numbers for which win[n] could be true even if all moves go to numbers <1? No.
Thus we can implement.
But let's double-check the problem statement for any twist: "Panda is fond of numbers. Given a number, he subtracts it with squares of any one particular digit of that number to get new numbers. This operation can be applied any number of times (possibly zero) till he obtains a pandatic number. If he is able to reach to a pandatic number then he wins. A pandatic number is a number which can be expressed in the form A^A, where A is a positive integer."
The phrase "subtracts it with squares of any one particular digit" might be interpreted as "subtracts the square of any digit from the number" as we did.
Now, we need to think about the possibility of subtracting the square of a digit that appears multiple times? It says "any one particular digit", meaning choose a digit that appears in the number and subtract its square (once). So it's exactly what we did.
Now, we might need to consider if the digit can be selected from the set of distinct digits? Or could we subtract square of a digit that appears multiple times, but still only subtract once. So it's fine.
Now, what about large N up to 10^6? Precomputation is fine.
But there is subtask 2: N up to 10^6, T up to 10^5. DP is fine.
But is there any scenario where from a number you can reach a pandatic number by a sequence that includes numbers > 10^6? Since we start at most 10^6, all numbers in the sequence are ≤ starting number, so they are ≤ 10^6. So we only need to compute up to maxN.
Now let's implement:
Pseudo:
def precompute(maxN):
win = [False] * (maxN+1)
# pandatic
A = 1
while True:
p = A ** A
if p > maxN:
break
win[p] = True
A += 1
# DP
for n in range(1, maxN+1):
if win[n]:
continue
# get digits of n
temp = n
while temp > 0:
d = temp % 10
if d != 0:
next_n = n - d*d
if next_n >= 1 and win[next_n]:
win[n] = True
break
temp //= 10
return win
But careful: For numbers where win[n] is initially false, we check all digits. However, we need to consider that if a number has multiple digits, we might have a digit repeated; but we only need to consider distinct digits because subtracting same digit yields same next_n. So we can just loop through digits; duplicates fine.
We must also consider that digit 0 yields no change, so we skip.
Now, we process n in increasing order. For n, win[next_n] is already known because next_n < n. So it's fine.
Now test: n=10. digits: 1 and 0. d=1: next=9. win[9]? At that point, we have processed n=9 earlier, so win[9] false. So win[10] false.
n=13: digits 1 and 3: d=3: next=4, win[4] true, so set win[13] true.
Works.
Now about n that are pandatic: we set win true initially, so skip.
Now check n=1: win[1] true, skip.
n=2: win[2] false; digits: 2; next= -2; condition next>=1 false, so remains false.
n=3: false.
n=4: true (pandatic).
Now n=5: false.
Now n=6: false.
n=7: false.
n=8: false.
n=9: false.
n=10: false.
n=11: false.
n=12: false.
n=13: true.
n=14: digits: 1 and 4; next 13 true, so true.
n=15: digits 1 and 5; next 14 true, so true.
... Seems correct.
But let's check n=22: digits: 2 and 2; next 18 for d=2: 22-4=18; win[18]? 18 we computed true? 18 digits: 1 and 8; d=1 => 17 true; so yes true. So win[22] true.
Now check n=23: true.
Now n=24: digits 2,4; 24-4=20 true; 24-16=8 false; so true.
Now n=25: digits 2,5; 25-4=21 true; so true.
Now n=26: digits 2,6; 26-4=22 true; so true.
Now n=27: pandatic.
So many numbers become true.
But we must verify if there exists any number for which win is false. Let's try to find some that are not reachable from pandatic numbers. Since pandatic numbers are few, and from them we can go up via reverse edges? But we are doing forward DP, so we compute all numbers that can reach pandatic. The ones that cannot are those that cannot reach any pandatic via decreasing moves.
Given that each move subtracts at most 81, and pandatic numbers are scattered, there might be numbers that never land exactly on a pandatic. For example, N=2,3,5,6,7,8,9,10,11,12 all false. What about 28? digits 2,8; 28-4=24 true; so true. 29: 29-4=25 true. 30: digits 3,0; d=3 =>27 true; so true. 31: d=3=>28 true; so true. 32: d=3=>29 true; so true. 33: d=3=>30 true; so true. 34: d=3=>31 true; so true. 35: d=3=>32 true; so true. 36: d=3=>33 true; so true. 37: d=3=>34 true; so true. 38: d=3=>35 true; so true. 39: d=3=>36 true; so true. So after 13, many numbers become true. However, what about numbers like 100? Already true. So the only false numbers might be those less than 13 except 1,4? Actually 1 and 4 are true (pandatic). 2,3,5,6,7,8,9,10,11,12 are false. Also 0? Not considered. So is 13 the first true after 4? 13 true. So after 12, all numbers >=13 might be true? Let's test a few: 13 true, 14 true, ... up to maybe some number might be false? Let's check 24 we did true. 25 true, 26 true, 27 true, 28 true, etc. But could there be a number greater than 12 that cannot reach any pandatic? Let's try to find a counterexample: Suppose a number n such that for every digit d, n - d^2 is either <1 or a number that is false. We need to see if any n>12 can have that property. Since false numbers we have so far are {2,3,5,6,7,8,9,10,11,12}. Also maybe 0? Not considered. Also maybe some numbers >12 that are false? Let's try 22? we saw true. 23 true. 24 true. 30 true. 31 true. 32 true. 33 true. 34 true. 35 true. 36 true. 37 true. 38 true. 39 true. 40? digits 4,0; d=4 => 40-16=24 true; so true. 41: d=4=>25 true. 42: d=4=>26 true. 43: d=4=>27 true. 44: d=4=>28 true. 45: d=4=>29 true; also d=5=>20 true. 46: d=4=>30 true. 47: d=4=>31 true. 48: d=4=>32 true. 49: d=4=>33 true; d=9=>49-81=-32 false; so true. 50: d=5=>25 true; etc. So seems all numbers >=13 might be true. Let's test 100? 100 -> 99 -> 18 -> ... -> true. So maybe the only false numbers are those less than 13 except pandatic ones. But is there any number >=13 that cannot reach pandatic? Let's attempt to reason: The pandatic numbers we have are: 1,4,27,256,3125,46656,823543. 1 and 4 are small. 27 is 27. So if we can reach 27 from any number above 27? From numbers above 27, we subtract squares up to 81, so we can potentially land on 27 exactly if n - d^2 = 27 => n = 27 + d^2. So numbers of the form 27+d^2 where d is a digit of n. That's not necessarily covering all numbers. But we can also reach 27 via multiple steps: e.g., from n, we can go to some intermediate number that eventually leads to 27. So the reachable set might be large.
We should test some numbers near the next pandatic: 256. For numbers between 256 and maybe 256+81=337, they could directly go to 256 if they contain a digit d such that n - d^2 = 256, i.e., n = 256 + d^2. For d from 1 to 9, d^2 = 1,4,9,16,25,36,49,64,81. So numbers: 257,260,265,272,281,292,305,320,337. So these numbers have direct move to 256 if they contain the corresponding digit. But many numbers may not contain that digit; but they might go via intermediate steps. The question is: Is there any number >=13 that cannot eventually reduce to a pandatic? Possibly some numbers might get stuck in a "dead end" region where all moves go to numbers that are also dead. But from our DP, we can compute up to 1e6 to see if any false numbers exist beyond 12.
Let's do a quick mental check: Starting from 13 true. 14 true. 15 true. 16 true. 17 true. 18 true. 19 true. 20 true. 21 true. 22 true. 23 true. 24 true. 25 true. 26 true. 27 true. 28 true. 29 true. 30 true. So all numbers from 13 to 30 are true. For 31-39 we already saw true. For 40-49 true. So it seems all numbers up to 49 are true. What about 50-59: All have digit 5? 50 has digit 5, so 50-25=25 true. 51: digit 5? 51 has 5? Yes, 5 and 1. So 51-25=26 true. 52: 5 -> 27 true. 53: 5->28 true. 54: 5->29 true. 55: 5->30 true. 56:5->31 true. 57:5->32 true. 58:5->33 true. 59:5->34 true. So all true.
What about numbers like 60: digit 6? 60-36=24 true. So true. 61:6->25 true. 62:6->26 true. 63:6->27 true. etc. So any number >=13 that has at least one digit that is not 0? Actually all numbers have at least one non-zero digit. The only concern is if n - d^2 is still >=1 and leads to false. So we need to examine numbers that are exactly one step away from the false set. But the false set is only numbers below 13 and not pandatic: {2,3,5,6,7,8,9,10,11,12}. Also maybe 0, but not considered. So if n - d^2 is in that set, then n might be false if all moves lead to those false numbers. For n >=13, can n - d^2 be in {2,3,5,6,7,8,9,10,11,12}? Let's check: n = d^2 + m, where m in false set. For d from 1 to 9, d^2 values: 1,4,9,16,25,36,49,64,81. So possible n values: For each d, n = d^2 + m. For m in false set and d such that n >=13? Let's compute all such n and see if they could be false if no other digit leads to a true state. But also need to check if those n contain digit d? Actually the move that gives m uses digit d; for that move to be available, n must contain digit d. So n must contain the digit d that was used. So for n to have a move to a false m, it must have a digit d such that n - d^2 = m, and m false. If all possible moves (for all digits in n) lead to false, and n itself not pandatic, then n is false. So we need to check if any n >=13 can be constructed such that for each digit d of n, n - d^2 is either <1 or a false number. But note that if n has a digit d such that n - d^2 is >=1 and is true, then n is true.
Thus to find false n >=13, we need to find n such that:
- n is not pandatic.
- For every digit d in n, either (n - d^2) < 1, or (n - d^2) is false.
But given that false numbers are small (<13 except pandatic), the only possible false m are 2,3,5,6,7,8,9,10,11,12. So n = d^2 + m for some m false. So n would be at least 1+2=3? Actually smallest d=1 gives m=2 => n=3; but 3 is already false and <13. For d=1, m up to 12 => n up to 13. So d=1 gives n from 1+2=3 up to 1+12=13. But n=13 with d=1 gives m=12 false; but n=13 also has digit 3, which gives m=4 true, so 13 true. So to be false, n must have only digits that lead to false. So we need to examine n that are >12 and possibly built from digits with d^2+m.
Let's list all possible (d,m) pairs with m false, d from 1..9:
d=1: m ∈ {2,3,5,6,7,8,9,10,11,12} => n ∈ {3,4,6,7,8,9,10,11,12,13}
But n=4 is pandatic, so not false. n=6? 6 is false? Actually 6 is false. n=7 false, 8 false, 9 false, 10 false, 11 false, 12 false, 13 true because it has digit 3 leading to true. But note: n=13 also appears from d=1,m=12; but 13 also has digit 3 which gives m=4 true, so it's not false. So for n=13, even though one move leads to false (12), another leads to true, so overall true.
For n=14, possible from d=1,m=13? But 13 is true, so that would make 14 true. Also from d=4,m=? etc.
Now d=2: d^2=4. m false: m ∈ {2,3,5,6,7,8,9,10,11,12} => n ∈ {6,7,9,10,11,12,13,14,15,16}. Among these, n=6 false, 7 false, 9 false, 10 false, 11 false, 12 false, 13 true, 14 true, 15 true, 16 true. So n=13,14,15,16 are >12 and appear, but they have other digits leading to true? Let's check each:
n=13: contains digit 1? yes, but we are considering d=2 path: 13-4=9 false. However, 13 has digit 1 => 12 false? Actually 13-1=12 false, digit 3 => 4 true. So 13 true. So if n=13, even though d=2 leads to false, other digit leads to true. But suppose there exists n that only has digits among those that lead to false, and n itself is not pandatic. For n to be >12, it must be at least 13. Could there be n with digits only from set such that each digit's resulting m is false? Let's attempt to find such n.
Consider n=13: digits are 1 and 3. 1 leads to 12 false, 3 leads to 4 true -> not all false. So 13 is true.
n=14: digits 1 and 4. 1->13 true? Actually 14-1=13 true, 4-> -2? 14-16=-2 false? Actually 4^2=16, 14-16=-2 (<1) false. So one true => true.
n=15: digits 1,5. 1->14 true; 5-> -10 false. true.
n=16: digits 1,6. 1->15 true; 6-> -20 false. true.
n=17: digits 1,7. 1->16 true; 7-> -32 false. true.
n=18: 1->17 true; 8-> -46 false. true.
n=19: 1->18 true; 9-> -62 false. true.
n=20: digits 2,0. 2->16 true; 0 ignored. true.
n=21: 2->17 true; 1->20 true. true.
n=22: digits 2,2 -> both 22-4=18 true. true.
n=23: 2->19 true; 3->14 true. true.
n=24: 2->20 true; 4->8 false? Actually 24-16=8, 8 is false. But 2 leads to true. So true.
n=25: 2->21 true; 5->0 false. true.
n=26: 2->22 true; 6-> -10 false. true.
n=27: pandatic.
n=28: 2->24 true; 8-> -36 false. true.
n=29: 2->25 true; 9-> -52 false. true.
n=30: 3->21 true; 0 ignored. true.
n=31: 3->22 true; 1->30 true. true.
n=32: 3->23 true; 2->28 true. true.
n=33: 3->24 true; etc.
So seems all numbers from 13 upward become true because they often have digit 1 leading to previous number that is true? But we need to check if there is any number that does not have digit 1 and whose other digits lead only to false or negative. For n without digit 1, maybe? Let's examine numbers that do not contain digit 1. For instance, n=30 has digit 3 -> 21 true (21 has digit 1? But that's okay because we only care about direct move). So n=30 works because 30-9=21, and 21 is true (since we already determined 21 true). So we need to check whether numbers that lack digit 1 might still have a digit that leads to a true number. The true numbers seem to propagate upward.
Now consider n=40: digit 4 -> 24 true (24 true). So true.
n=50: digit 5 -> 25 true.
n=60: digit 6 -> 24 true.
n=70: digit 7 -> 21 true? 70-49=21 true.
n=80: digit 8 -> 64? 80-64=16 true.
n=90: digit 9 -> 81? 90-81=9 false, but digit 9 leads to false. However, 90 also has digit 0 (ignored). So only digit 9 leads to 9 (false). So n=90 would be false if 9 is false and there is no other digit. But 90 has digits 9 and 0. So only digit 9 gives move to 9. 9 is false. So is 90 false? But earlier we considered 90 might be false. Let's compute: n=90, digits: 9. d=9 gives next = 90 - 81 = 9. 9 is false (since we determined 9 is false). Also digit 0 gives no change (ignore). So win[90] would be false if 9 is false. But is 9 false? Let's check DP: 9 is false because 9-81 = -72 <1. So indeed 9 false. So 90 would be false according to our DP, unless there is another digit? 90 has only digits 9 and 0. So 90 should be false. But earlier we assumed many numbers become true; we need to verify 90.
But wait: 90 also could be considered: Are there any other digits? Could we consider both digits? No, only digits 9 and 0. So only move is to 9. 9 is false. So win[90] = false.
But is it possible to reach a pandatic from 90 via multiple steps? If we go to 9, from 9 we can't go anywhere else (9-81 negative). So 90 is a dead end. However, we must check if 90 itself is pandatic? 90 is not A^A. So 90 would be false. So there exist numbers >=13 that are false: 90 is a candidate. Let's check if any other moves? What about using digit 0? That yields same number 90, which doesn't help. So 90 false.
But is there any other path? Could we apply operation on 90 with digit 9 to get 9, then from 9 there is no digit other than 9? 9's digits: only 9, so again negative. So indeed no.
Now, is 90 >=13, yes. So not all numbers >=13 are true. So our earlier assumption was wrong.
Thus we need to compute DP fully up to maxN to capture such cases.
Now, is 90 the only such number? Possibly there are many. Let's think about numbers that consist only of digits from {0,2,3,4,5,6,7,8,9} but need to check if any digit leads to a true number. For a number to be false, all its non-zero digits must map to false numbers (or to numbers <1). So we need to compute the set of false numbers.
Thus we need to run DP for all up to 1e6.
Now, let's also test 100: digits 1 and 0, so has digit 1. 100-1=99; is 99 true? 99 digits 9,9 => 99-81=18 true? Actually 18 is true, so 99 true. So 100 true.
Now, what about 200? digits 2 and 0. 200-4=196. Need to compute win[196]. If 196 true then 200 true. Likely true.
Now, 900? digits 9 and 0. 900-81=819. 819? digits 8,1,9. Could be true. We need to compute.
Thus we need to compute DP for all numbers.
Now, the DP as described will correctly compute win for each n.
But we need to be careful about numbers that have digit 0. We ignore them, but what about numbers that only have digit 0? Only 0 itself. Not in range.
Now, we need to handle n up to 1e6. We can precompute.
Now, one more nuance: When we compute digits of n, we need to consider each distinct digit only once? But duplicates don't matter; we can loop through all digits (including duplicates) but it's fine; same next value will be checked multiple times but that's okay.
Now, time: For each n up to 1e6, we loop over its digits (max 7). So about 7 million operations, trivial.
Now, we must also consider that the DP for n might depend on win[n - d^2] that is already computed because n - d^2 < n. However, there is a corner case: if n - d^2 = n (d=0) we ignore; if d>0, it's strictly less. So order is fine.
Now, we also need to consider that if n - d^2 = 0, we treat as false because 0 is not pandatic and no moves from 0? Actually 0 might be considered as number; but we can check if we should consider 0. 0 has digit 0; the only move from 0 is subtract 0^2 = 0, staying at 0. So from 0, we cannot reach pandatic. So win[0] = false. But we only care about n>=1. But for n where n - d^2 = 0, we need to know win[0] which we set false. So we need win[0] defined. So we should set win[0] = False.
Now, we need to precompute pandatic numbers up to maxN, set win[p] = True.
Now, DP: for n from 1 to maxN: if not win[n], then for each digit d>0 in n: next = n - d*d; if next >=0 and win[next] is True then set win[n]=True and break.
But careful: If next = 0, win[0] false, so no.
Now, what about numbers that are larger than maxN? Our test cases only up to 1e6, so fine.
Now, we need to ensure that we process in increasing order so that win[next] is already computed. Since next < n, it's fine.
But what about numbers that are pandatic but also have moves that lead to other pandatic? Not needed.
Now, we should also consider if there is any possibility that a number can reach pandatic via a path that goes through numbers greater than n? No, because moves always subtract, so you only go down.
Thus solution seems straightforward.
But before finalizing, we should test some numbers manually to verify DP's correctness on some edge cases.
Let's compute win for numbers up to maybe 30 to confirm manually our DP logic:
We'll do small numbers:
1: pandatic -> true.
2: digits: 2 -> next -2 (<1) -> false.
3: digits: 3 -> next -6 (<1) -> false.
4: pandatic -> true.
5: digits:5 -> next -20 -> false.
6: digits:6 -> next -30 -> false.
7: digits:7 -> next -42 -> false.
8: digits:8 -> next -56 -> false.
9: digits:9 -> next -72 -> false.
10: digits:1 -> next 9 (false) -> false.
11: digits:1 -> next 10 (false) -> false.
12: digits:1 -> next 11 (false); digit 2 -> next 8 (false) -> false.
13: digit 1->12 false; digit 3->4 true -> true.
14: digit 1->13 true -> true.
15: 1->14 true -> true.
16: 1->15 true -> true.
17: 1->16 true -> true.
18: 1->17 true -> true.
19: 1->18 true -> true.
20: digit 2->16 true -> true.
21: 2->17 true; 1->20 true -> true.
22: 2->18 true -> true.
23: 2->19 true; 3->14 true -> true.
24: 2->20 true; 4->8 false -> true because 2 gives true.
25: 2->21 true; 5->0 false -> true.
26: 2->22 true; 6->-10 false -> true.
27: pandatic true.
28: 2->24 true; 8->-36 false -> true.
29: 2->25 true; 9->-52 false -> true.
30: 3->21 true -> true.
31: 3->22 true; 1->30 true -> true.
32: 3->23 true; 2->28 true -> true.
33: 3->24 true; 3->24 also -> true.
34: 3->25 true; 4->18? 34-16=18 true -> true.
35: 3->26 true; 5->10 false? 35-25=10 false but 3 gives true -> true.
36: 3->27 true -> true.
37: 3->28 true; 7->-12 false -> true.
38: 3->29 true; 8->-26 false -> true.
39: 3->30 true; 9->-42 false -> true.
40: 4->24 true; 0 ignored -> true.
41: 4->25 true -> true.
42: 4->26 true -> true.
43: 4->27 true -> true.
44: 4->28 true -> true.
45: 4->29 true; 5->20 true -> true.
46: 4->30 true; 6->10 false -> true.
47: 4->31 true; 7->-2 false -> true.
48: 4->32 true; 8->-16 false -> true.
49: 4->33 true; 9->-32 false -> true.
50: 5->25 true -> true.
51: 5->26 true; 1->50 true -> true.
52: 5->27 true; 2->48? 52-4=48 true -> true.
53: 5->28 true; 3->44? 53-9=44 true -> true.
54: 5->29 true; 4->38? 54-16=38 true -> true.
55: 5->30 true; 5->30 also -> true.
56: 5->31 true; 6->20 true -> true.
57: 5->32 true; 7->8? 57-49=8 false, but 5 gives true -> true.
58: 5->33 true; 8->-6 false -> true.
59: 5->34 true; 9->-22 false -> true.
60: 6->24 true -> true.
61: 6->25 true; 1->60 true -> true.
62: 6->26 true; 2->58 true -> true.
63: 6->27 true; 3->54 true -> true.
64: 6->28 true; 4->48 true -> true.
65: 6->29 true; 5->40 true -> true.
66: 6->30 true; 6->30 also -> true.
67: 6->31 true; 7->18? 67-49=18 true -> true.
68: 6->32 true; 8->4? 68-64=4 true -> true.
69: 6->33 true; 9->-12 false -> true.
70: 7->21 true -> true.
71: 7->22 true; 1->70 true -> true.
72: 7->23 true; 2->68 true -> true.
73: 7->24 true; 3->64 true -> true.
74: 7->25 true; 4->58 true -> true.
75: 7->26 true; 5->50 true -> true.
76: 7->27 true; 6->40 true -> true.
77: 7->28 true; 7->28 also -> true.
78: 7->29 true; 8->14? 78-64=14 true -> true.
79: 7->30 true; 9->-2 false -> true.
80: 8->16 true -> true.
81: 8->17 true; 1->80 true -> true.
82: 8->18 true; 2->78 true -> true.
83: 8->19 true; 3->74? 83-9=74 true -> true.
84: 8->20 true; 4->68 true -> true.
85: 8->21 true; 5->60 true -> true.
86: 8->22 true; 6->50? 86-36=50 true -> true.
87: 8->23 true; 7->38? 87-49=38 true -> true.
88: 8->24 true; 8->24 also -> true.
89: 8->25 true; 9->8? 89-81=8 false; but 8 gives true -> true.
90: digits 9 only (and 0). 9->9 false. So win[90] = false.
91: digits 9,1. 9->10? 91-81=10 false; 1->90? 91-1=90, and 90 is false (we just determined). So both lead to false? Check: 91-81=10, 10 false. 91-1=90, 90 false. So win[91] should be false if both false. Also 91 itself is not pandatic. So win[91] false. But we need to verify if 10 is false (yes). So 91 false.
92: digits 9,2. 9->11? 92-81=11 false; 2->88? 92-4=88 true? 88 we saw true (since 8->24 true etc). So 2 leads to 88 true => win[92]