Question Link
Given a decimal N, you can convert it into a base number. If this number contains only numbers 3, 4, 5, and 6, this base number becomes the lucky number of N, the number of lucky numbers in the output. For example, if the number is 19,5, the number is 34, so the number 5 is a lucky number of 19.
Train of Thought: The following ideas are provided here
In special cases, there will be infinite? Only when n is 3, 4, 5, and 6, because these numbers are themselves in hexadecimal notation greater than N .. Note that special cases do not include 33,343 (I died here at the beginning, WA 3 times ). Because 33 is not 33 in the 34-digit System (like 10 is a in the hexadecimal system ).
We know that n = A0 + A1 * x + A2 * x ^ 2 +... where X is in hexadecimal notation. Since n reaches 1e12, we will discuss the situation in detail.
1) In A0 format, we have pointed out in special cases that the conditions will be met only when there are infinite
2) A0 + A1 * x form, enumeration A0, A1, we judge (n-a0) can be A1 divisible, and X is greater than Max (A0, A1) can be.
3) A0 + A1 * x + A2 * x ^ 2. Let's enumerate A0, A1, A2, which is equivalent to solving the quadratic equation of one dollar. Determine whether integer solutions exist and whether integer solutions x> MAX (A0, a1, a2.
4) if it is not in the above three forms, then the maximum X will not be x ^ 3> N, otherwise it will become the above three forms. We can enumerate and then determine if it is lucky. Because x ^ 3 <= N, the complexity is only 1e4.
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #define LL __int64 6 7 using namespace std; 8 9 int main()10 {11 int T, casee = 1;12 scanf("%d",&T);13 while(T--)14 {15 LL n;16 LL ans = 0,i,j,k;17 scanf("%I64d",&n);18 printf("Case #%d: ", casee++);19 if(n >= 3 && n <= 6)20 {21 printf("-1\n");22 continue;23 }24 for( i = 3; i <= 6; i++)25 for(j = 3; j <= 6; j++)26 if((n - i) % j == 0 && (n - i) / j > max(i, j))27 ans++;28 LL s ,a,b,c;29 for( i = 3; i <= 6; i++)30 {31 for(j = 3; j <= 6; j++)32 {33 for(k = 3; k <= 6; k++)34 {35 a = i,b = j,c = k - n;36 s = (LL)sqrt(b * b - 4 * a * c + 0.5);37 if(s * s != (b * b - 4 * a * c)) continue;38 if((s - b) % (2 * a)) continue;39 if((s - b) / (2 * a) > max(i, max(j, k)))40 ans++;41 }42 }43 }44 LL t;45 for(i = 4; i * i * i <= n; i++)46 {47 t = n;48 while(t)49 {50 if(t % i < 3 || t % i > 6) break;51 t = t / i;52 }53 if(!t) ans++;54 }55 printf("%I64d\n", ans);56 }57 return 0;58 }
View code