Lucky Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 664 Accepted Submission(s): 194
Problem Description
“Ladies and Gentlemen, It’s show time! ”
“A thief is a creative artist who takes his prey in style... But a detective is nothing more than a critic, who follows our footsteps...”
Love_Kid is crazy about Kaito Kid , he think 3(because 3 is the sum of 1 and 2), 4, 5, 6 are his lucky numbers and all others are not.
Now he finds out a way that he can represent a number through decimal representation in another numeral system to get a number only contain 3, 4, 5, 6.
For example, given a number 19, you can represent it as 34 with base 5, so we can call 5 is a lucky base for number 19.
Now he will give you a long number n(1<=n<=1e12), please help him to find out how many lucky bases for that number.
If there are infinite such base, just print out -1.
“A thief is a creative artist who takes his prey in style... But a detective is nothing more than a critic, who follows our footsteps...”
Love_Kid is crazy about Kaito Kid , he think 3(because 3 is the sum of 1 and 2), 4, 5, 6 are his lucky numbers and all others are not.
Now he finds out a way that he can represent a number through decimal representation in another numeral system to get a number only contain 3, 4, 5, 6.
For example, given a number 19, you can represent it as 34 with base 5, so we can call 5 is a lucky base for number 19.
Now he will give you a long number n(1<=n<=1e12), please help him to find out how many lucky bases for that number.
If there are infinite such base, just print out -1.
Input
There are multiply test cases.
The first line contains an integer T(T<=200), indicates the number of cases.
For every test case, there is a number n indicates the number.
The first line contains an integer T(T<=200), indicates the number of cases.
For every test case, there is a number n indicates the number.
Output
For each test case, output “Case #k: ”first, k is the case number, from 1 to T , then, output a line with one integer, the answer to the query.
Sample Input
2
10
19
Sample Output
Case #1: 0
Case #2: 1
Hint
10 shown in hexadecimal number system is another letter different from ‘0’-‘9’, we can represent it as ‘A’, and you can extend to other cases.Author
UESTC
Source
Recommend
题意、题解,转自:
题意:
我们将3,4,5,6认为是幸运数字。给定一个十进制数n。现在可以讲起任意转换成其他进制,但转换后的数必须是由3,4,5,6构成的,而这个进制称为幸运进制。问有多少个幸运进制。若有无数个,则输出-1。例如19在5进制下是34,所以5是幸运进制。
题解:
先考虑特殊情况,所情况下会有无穷个?只有n=3,4,5,6的时候,因为这几个数在大于n的进制下都是他本身。。注意特殊情况不包括33,343这些(我一开始就死在这里了,wa了三次)。因为33在34进制下就不是33了(类似于10在16进制下就是A了)。
我们知道n=a0+a1*x+a2*x^2+...,其中x为进制。由于n达到1e12,所以我们分情况讨论。
1)a0形式,我们已经在特殊情况中指出,只有无穷个的时候才会符合条件
2)a0+a1*x形式,枚举a0,a1,我们判断(n-a0)是否能被a1整除,以及x是否大于max(a0,a1)即可。
3)a0+a1*x+a2*x^2,我们枚举a0,a1,a2,那么就相当于解一元二次方程。判断是否有整数解,是否整数解x>max(a0,a1,a2)即可。
4)不在上述三种形式内的,那么进制x最大也不会x^3>n,不然就会变成上述三种的形式。我们就可以枚举进制然后判断是否为幸运进制了。由于x^3<=n,所以复杂度只有1e4。
注意:就是上述的特殊情况,死的惨惨的。。
代码:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 9 #define N 100005 10 #define M 10005 11 #define mod 1000000007 12 #define mod2 100000000 13 #define ll long long 14 #define maxi(a,b) (a)>(b)? (a) : (b) 15 #define mini(a,b) (a)<(b)? (a) : (b) 16 17 using namespace std; 18 19 int T; 20 int f[N]; 21 ll n; 22 23 void ini() 24 { 25 memset(f,0,sizeof(f)); 26 int i,j; 27 int te,yu; 28 for(i=1;i<=M;i++){ 29 for(j=4;j<=10000;j++){ 30 te=i; 31 int flag=1; 32 while(te){ 33 yu=te%j; 34 if(yu!=3 && yu!=4 && yu!=5 && yu!=6){ 35 flag=0;break; 36 } 37 te/=j; 38 } 39 if(flag==1) f[i]++; 40 } 41 } 42 f[3]=f[4]=f[5]=f[6]=-1; 43 44 // for(i=1;i<=M;i++){ 45 // printf(" i=%d f=%d ",i,f[i]); 46 //} 47 } 48 49 int main() 50 { 51 ll ans; 52 ll j,i,k; 53 ll a,b,c,d; 54 ll base; 55 //freopen("data.in","r",stdin); 56 // ini(); 57 scanf("%d",&T); 58 for(int cnt=1;cnt<=T;cnt++) 59 { 60 ans=0; 61 printf("Case #%d: ",cnt); 62 scanf("%I64d",&n); 63 if(n>=3 && n<=6){ 64 printf("-1 ");continue; 65 } 66 67 for(i=3;i<=6;i++){ 68 for(j=3;j<=6;j++){ 69 if( (n-i)%j==0 && (n-i)/j >max(i,j) ) ans++; 70 } 71 } 72 // printf(" %I64d ",ans); 73 74 for(i=3;i<=6;i++){ 75 for(j=3;j<=6;j++){ 76 for(k=3;k<=6;k++){ 77 a=i;b=j;c=k-n; 78 ll te=sqrt(b*b-4*a*c+0.5); 79 if(te*te!=b*b-4*a*c) continue; 80 if(te<b) continue; 81 d=(te-b); 82 if(d%(2*a)==0){ 83 base=d/2/a; 84 if(base>max(max(i,j),k))ans++; 85 } 86 } 87 } 88 } 89 90 // printf(" %I64d ",ans); 91 92 //printf("%I64d ",ans); 93 94 for(j=4;j*j*j<=n;j++){ 95 ll te=n; 96 int flag=1; 97 while(te){ 98 ll yu=te%j; 99 if(yu!=3 && yu!=4 && yu!=5 && yu!=6){ 100 flag=0;break; 101 } 102 te/=j; 103 } 104 if(flag==1) ans++; 105 } 106 printf("%I64d ",ans); 107 // } 108 109 110 } 111 return 0; 112 }