Codechef November Challenge 2013 Square Digit Squares
Square Digit SquaresProblem code: SDSQUARE |
Read problems statements in Mandarin Chinese and Russian.
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
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <map> 3 #include <queue> 4 #include <vector> 5 #include <string> 6 #include <cmath> 7 #include <cstdio> 8 #include <cstring> 9 #include <cstdlib> 10 #include <iostream> 11 #include <algorithm> 12 using namespace std; 13 #define maxn 135 14 #define ll long long 15 #define mod 1000000007 16 #define INF 0x7fffffff 17 #define eps 1e-8 18 ll n,m; 19 ll a[maxn]; 20 int main(){ 21 int t,k=0; 22 for (ll i = 1; i <= 100000; i++){ 23 ll s = i*i; 24 while (s){ 25 ll x = s % 10; 26 if (x != 0 && x != 4 && x != 1 && x != 9)break; 27 s /= 10; 28 } 29 if (s==0)a[k++] = i*i; 30 } 31 scanf("%d", &t); 32 while (t--){ 33 scanf("%lld%lld", &n,&m); 34 int l = lower_bound(a, a + k, n) - a; 35 int r = lower_bound(a, a + k, m) - a; 36 if (a[r] != m)r--; 37 printf("%d\n", r - l+1); 38 } 39 return 0; 40 }
浙公网安备 33010602011771号