Recently Chef become very much interested in perfect squares. We all know Chef and his weird interests. Anyways Chef will be soon writing his masters thesis on perfect squares revealing what-not-known properties of perfect squares.
While doing his research, he happened to be confronted with some interesting perfect squares. These prefect squares consists only of digits which are themselves perfect squares. 0, 1, 4 and 9 are such digits. These are called perfect digits.
As we all know Chef also has habit of asking too many questions, he is asking- given two numbers a and b, how many perfect squares exists between these two numbers inclusive, that contains only perfect digits.
Input:
First line of input will contains T, number of test cases. Then T lines follows, each containing two positive integers a and b.
Constraints:
T <= 500
1<= a <= b <= 10000000000
Output:
For each input, output number of perfect digit squares between given numbers.
Sample
Input:
2
1 10
100 10000
Output:
3
9
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to count numbers between a and b inclusive that are perfect squares and consist only of digits {0,1,4,9} (digits that are perfect squares). Constraints: a,b up to 10^10, so numbers up to 10^10. That means squares up to 10^10, so the square roots up to sqrt(10^10) = 10^5 = 100000. So we can precompute all perfect squares from 1 to 10^10, but only those that have digits in {0,1,4,9}. But there are only 100k squares, we could generate all squares from 1 to 100000, check if the square's digits are all in {0,1,4,9}, and store the squares in a sorted list. Then for each query, binary search to count how many squares in [a,b] are in that list.
But note: a and b up to 10^10, and T up to 500, so this is feasible.
But we need to be careful: the squares can be large, up to 10^10, which fits in 64-bit (since 10^10 ~ 1e10, 2^63 ~ 9.22e18, so fine). So we can precompute squares as unsigned long long.
Implementation steps:
Precompute max_n = floor(sqrt(1e10)) = 100000 (since 100000^2 = 10^10 exactly). But careful: 1e10 = 10000000000, sqrt = 100000. So indeed, n from 1 to 100000 inclusive, compute n*n. For n=0? Since a>=1, we don't consider 0.
But note: perfect squares between a and b inclusive, where a >=1. So squares are n^2 with n>=1.
For each n from 1 to 100000, compute square = n*n. Check if all digits of square are in {'0','1','4','9'}. We can convert to string or iterate digits using division. Since performance is okay (100k numbers), using string conversion is simpler.
Store the squares that pass in a vector perfect_digit_squares.
Then for each test case, read a,b (both long long). Use lower_bound and upper_bound to count number of elements in the vector that are between a and b inclusive.
Complexities: Precomputation O(100k * digit length) ~ O(1e6) fine. Queries O(T log size) with size at most maybe few thousand? Actually how many squares satisfy? Let's approximate: Among numbers up to 10^10, the squares are 100k numbers. Among them, those with digits only 0,1,4,9: count? Could be around maybe a few hundred? Actually let's consider: For each digit position, allowed digits 4 options, but squares have varying lengths. The number of such numbers up to 10^10 is sum_{d=1 to 10} 4^d (since numbers can have leading zero? No, numbers cannot have leading zero, but if we consider numbers with exactly d digits, first digit cannot be 0. Allowed digits first digit: {1,4,9} (since 0 not allowed as leading). So count for exactly d digits: 3 * 4^{d-1}. Sum from d=1 to 10: 3 * (4^0 + 4^1 + ... + 4^9) = 3 * (4^10 -1)/3 = 4^10 -1 = 1,048,575 - 1? Actually 4^10 = 1,048,576, so minus 1 = 1,048,575. That's the total numbers with up to 10 digits consisting only of 0,1,4,9 and not starting with 0. But we need perfect squares among these numbers. So the filtered list could be up to maybe around ~1000? Let's compute quickly: For d=1, numbers: 1,4,9 (all squares) => 3; d=2: first digit 1,4,9 (3) * second digit 4 choices = 12 numbers: 10? Actually 10 (digits 1 and 0) -> 10 not square; 11->121? Wait 11^2=121, but we are listing numbers themselves, not squares. So we don't know. But number of squares that satisfy condition is not huge. For n up to 100k, squares up to 10^10, we can just compute and store. Indeed, after computation, we can check size. But it's fine.
Edge Cases: a and b inclusive. Input a <= b. Also a and b can be up to 10^10, which fits in 64-bit. Use long long (signed) but 10^10 = 10,000,000,000 > 2^31-1, need 64-bit. In C++, use long long or unsigned long long. We'll use long long, but ensure it can hold up to 10^10 (since 2^63-1 ~9.22e18, fine). Use stoll for conversion if reading as string? But we can directly read as long long via cin.
But careful: square of 100000 = 10000000000 = 10,000,000,000, which fits in long long (10 billion). So fine.
Now implement:
bool is_perfect_digit_square(long long x) {
while(x) {
int d = x % 10;
if (d != 0 && d != 1 && d != 4 && d != 9) return false;
x /= 10;
}
return true;
}
But what about x=0? We don't need. So above works for x>0. However, if x has digit 0, that's allowed. So condition: d in {0,1,4,9}. In code: if(!(d==0||d==1||d==4||d==9)) return false.
Potential pitfalls: x may contain digit like 2,3,5,6,7,8. Reject.
Now precompute:
vector squares;
for (long long i=1; i<=100000; ++i) {
long long sq = i*i;
if (is_perfect_digit_square(sq)) squares.push_back(sq);
}
Then for each query:
int ans = upper_bound(squares.begin(), squares.end(), b) - lower_bound(squares.begin(), squares.end(), a);
print ans.
Test with sample:
First query: 1 10 -> squares between 1 and 10 that are perfect digit squares: 1,4,9? 1^2=1,2^2=4,3^2=9 all consist of digits {1,4,9}. So answer 3. Second: 100 10000. Perfect digit squares: we need to count squares in that range. Let's check manually? The answer given is 9. So our method should produce that.
But careful: Are there squares like 100? 10^2=100, digits 1,0,0 -> allowed digits? 0,1,4,9: 0 allowed, 1 allowed => yes. So 100 qualifies. So we need to count all squares with root between ceil(sqrt(100)) =10 and floor(sqrt(10000))=100 inclusive that produce perfect-digit squares. So we need to count for n=10..100 where n^2 has digits only 0,1,4,9. There are 9 such squares. So okay.
Implementation details: Precomputation can be done once at program start.
We should also consider that the input constraints T up to 500, a and b up to 1e10, but a can be as low as 1. So fine.
Now, I'd like to consider if there are any numbers like 0? Since a>=1, ignore.
Thus solution is straightforward.
But we must be careful about integer overflow: i up to 100000, i*i up to 10^10 fits in long long (signed). But some compilers might treat int multiplication overflow if i is int and not long long. So we'll use long long i.
Now implement solve():
int main(){
// precompute
vector list;
for(long long i=1; i<=100000; i++){
long long sq = i*i;
if(isPerfect(sq)) list.push_back(sq);
}
int T; cin>>T;
while(T--){
long long a,b; cin>>a>>b;
auto l = lower_bound(list.begin(), list.end(), a);
auto r = upper_bound(list.begin(), list.end(), b);
cout << (r - l) << endl;
}
return 0;
}
Edge Cases: If a > b? Not possible per constraints.
But wait: The problem statement says "between these two numbers inclusive". So inclusive.
Now, I'll test with a small example: a=4,b=4 -> only 4 -> answer 1. Our code: lower_bound returns iterator to first >=4, which will be at 4 if present; upper_bound returns first >4, which is after 4; difference=1 -> correct.
Now verify if we need to consider squares like 0? Not.
Now, are there any pitfalls with the digit check? For example, number 0 is a perfect square but not considered since a>=1. So fine.
But what about numbers like 100? digits: 1,0,0 -> all allowed, yes. 400? digits 4,0,0 -> allowed, 4^2? Actually 20^2=400, digits 4,0,0 -> allowed. So that qualifies. 900? digits 9,0,0 -> allowed. So many.
Now, is there any case where the square is like 144? digits 1,4,4 -> allowed. So 12^2=144 qualifies. So our code includes it.
Now, is there any need to handle large T and precomputation? No.
Now, what about performance of is_perfect_digit_square for each square? Up to 100k checks, each checking up to 11 digits, so fine.
Now, final answer.
But wait, we need to read input possibly large; use scanf/printf for speed? Not necessary for 500 queries.
Now, ensure using long long for square and for reading.
Potential issue: If we use long long and check digits, while(x) loop will skip trailing zeros? Actually if x ends with zeros, e.g., 100, loop: x=100, d=0 ok, x=10, d=0 ok, x=1, d=1 ok, then x=0 loop ends, returns true. That's correct because it checks all digits including zeros, but it does not check if the number is zero, but it's not zero.
But what if the number is 0? It would not enter loop and return true (since while(x) condition false). But we never consider 0. If we did, 0 digits? Actually number 0 consists of digit 0, which is allowed. But since a>=1, ignoring is fine.
But suppose a=0? Not per constraints. So fine.
Now, test sample: Input: 2\n1 10\n100 10000\n Output: 3 and 9. Let's test mentally with our list: squares that satisfy up to 10000: We'll generate a few: 1,4,9, 100? Actually 100 is >10, so between 1 and 10 we have 1,4,9 => 3. Between 100 and 10000: squares from 100 to 10000 inclusive: candidates: 100 (10^2), 144 (12^2), 400 (20^2), 441 (21^2? Actually 21^2=441, digits 4,4,1 allowed), 900 (30^2), 1444? 38^2=1444? Actually 38^2=1444, digits 1,4,4,4 allowed. Also 1600? 40^2=1600 includes digit 6, not allowed. 4900? 70^2=4900 digits 4,9,0,0 allowed? 4,9,0,0 all allowed, yes. 9409? 97^2=9409 digits 9,4,0,9 allowed. And many others. So there might be 9. So okay.
Now, confirm the count: Actually the problem expects 9 for range 100-10000. So it's plausible.
Now, one more nuance: The problem says "perfect digits" are digits which are themselves perfect squares: 0,1,4,9. So we need squares that consist only of these digits. So correct.
Now, we might also consider that numbers like 0 are perfect squares but we ignore.
Thus solution.
But before finalizing, we might consider whether any squares beyond 10^10 exist between a and b? Since b <= 10^10, no.
Thus answer.
Now, implementation in code.
But wait: The constraint: T <= 500, a <= b <= 10000000000. So we are safe with sqrt(10000000000)=100000. However, note that 100000^2 = 10,000,000,000 exactly. That's within b limit. So we should include n=100000. But also maybe squares like 0? Not needed.
Now, consider if b=10000000000 exactly, and a=1. Then we need to consider 100000^2 = 10000000000, which we compute and check if it qualifies. Good.
Now, precomputation loop: for(long long i=1; i<=100000; i++) includes 100000.
Now, is the condition correct for numbers with many zeros? For example 10000000000 has digits: 1 followed by 10 zeros? Actually 10000000000 = 10^10, digits: 1 and ten zeros? Actually 10^10 = 10000000000, that's 11 digits? Let's count: 10,000,000,000 has 11 digits: 1 followed by 10 zeros. So it's within 11 digits. Our digit check will handle. While loop for x=10000000000: d=0 OK, x=1000000000, d=0, ... eventually x=1, d=1, then x=0. So returns true because all digits are 0 or 1. So 10000000000 qualifies. So answer for a=1,b=10000000000 would include that.
Now, is there any square like 0? Not.
Now, consider performance: 100k iterations, each with while loop up to 11 digits, so trivial.
Now, implement.
But there's a twist: The problem says "perfect digits" are digits that are perfect squares, i.e., 0,1,4,9. That's correct. However, some might argue that 0 is a perfect square? 0^2=0, so yes. So 0 is allowed.
Now, I'll produce final answer.
But maybe we can do better: precompute once and answer queries quickly.
Now, code.
I'll use C++.
Now, double-check that we don't miss squares like 49? 7^2=49, digits 4 and 9, both allowed. So 49 qualifies. So our list includes 49.
Now, test with a small range: a=49,b=49 => answer 1.
Now, all good.
Now, finalize.
But wait: There's potential issue: The problem says "between these two numbers inclusive", which we handle.
Now, final answer.
Implementation details:
Include iostream, vector, algorithm.
Use long long.
Define function:
bool isPerfectDigitSquare(long long x) {
if (x == 0) return true; // not needed but for completeness
while (x > 0) {
int digit = x % 10;
if (digit != 0 && digit != 1 && digit != 4 && digit != 9) return false;
x /= 10;
}
return true;
}
But careful: The condition is digit must be 0,1,4,9. So if digit is any of these, it's fine. So we can check if (digit != 0 && digit != 1 && digit != 4 && digit != 9) return false.
Now, we might also consider using a bitset for speed.
Now, precompute:
vector perfect;
for (long long i = 1; i <= 100000; ++i) {
long long sq = i * i;
if (isPerfectDigitSquare(sq)) perfect.push_back(sq);
}
Now, answer queries.
Now, time complexity: O(100k) precompute, O(T log P) query, where P = size of perfect (maybe few hundred). So very fast.
Now, ensure we print newline after each answer.
Now, confirm sample:
Compute perfect list up to 10: squares: 1,4,9 -> all qualify, so list includes them.
For query 1 10: lower_bound(1) returns iterator to 1, upper_bound(10) returns iterator after 9 (since 9 <=10, next maybe 49?), so difference = 3.
For query 100 10000: We need to count perfect squares between 100 and 10000 inclusive. Let's manually compute to ensure we get 9. Might be correct.
Now, I'll also think about potential edge case: a and b are given as positive integers, but can be as large as 10^10. Ensure we read them into long long. Use cin.
Now, code.
But wait: Could there be squares beyond 10^10? No, because b <= 10^10, so max square considered is at most b.
Now, finalize.
But let's double-check the sqrt(10000000000) exactly 100000? Since 100000^2 = 10^10 exactly. So yes.
But is the upper bound inclusive? We consider n up to 100000 inclusive. Good.
Now, produce final solution.
Now, potential alternative: Could we generate numbers that consist only of digits 0,1,4,9 and then check if they are perfect squares? Since there are about 1 million such numbers up to 10^10, we could also precompute squares and check membership using set. But our method is simpler.
But one subtlety: The input a and b up to 10^10, but squares only up to 10^10. That's fine.
Now, I'll answer.
But the problem may have been intended for larger constraints? But given constraints, our approach works.
Now, I'll craft final answer.
Now, let's write the code with appropriate includes and using namespace std.
Potential improvements: Use fast IO if needed. But T=500, fine.
Now, deliver final solution.
One more thing: Check if there are any squares like 0? Not needed.
Now, final answer.
I'll output the code. ```cpp
#include
#include
#include
using namespace std;
// Check if all decimal digits of x are in {0,1,4,9}
bool isValid(long long x) {
// x is positive in our context
while (x > 0) {
int d = x % 10;
if (d != 0 && d != 1 && d != 4 && d != 9)
return false;
x /= 10;
}
return true;
}
int main() {
// Precompute all perfect squares up to 10^10 that consist only of perfect digits
const long long MAX_ROOT = 100000; // because 100000^2 = 10^10
vector good;
for (long long i = 1; i <= MAX_ROOT; ++i) {
long long sq = i * i;
if (isValid(sq))
good.push_back(sq);
}
code
int T;
cin >> T;
while (T--) {
long long a, b;
cin >> a >> b;
// count good squares in [a, b]
auto lo = lower_bound(good.begin(), good.end(), a);
auto hi = upper_bound(good.begin(), good.end(), b);
cout << (hi - lo) << endl;
}
return 0;