枚举 + 进制转换 --- hdu 4937 Lucky Number

Lucky Number#

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 294    Accepted Submission(s): 49


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.
 

 

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.
 

 

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
 

 

Mean:

 

给你一个10进制数n,现在要你找一个x,使得这个十进制数在x进制表示下的数字中只包含3,4,5,6这四个数字,问你这样的x有多少个。

 

 

analyse:

 

我们将n这个数在x进制下的表示记为:n=a0+a1*x+a2*x^2+a3*x^3+.....
我们采取枚举a0、a1、a2...,然后判断这个式子是否等于n的做法。
讨论一下几种情况:
1)a0:即a0==n,只有当a0等于3,4,5,6中其中一个的时候,才可能满足要求,而这种情况下,x取任何值都满足要求(当然最基本的条件x>a0要满足),所以该种情况下就输出-1;
2)a0+a1*x:此时要枚举的量有a0和a1,我们枚举在3,4,5,6中枚举a0和a1,那么如果方程:(n-a0)%a1==0成立(上面的基本条件不再重复),此时也是成立的;
3)a0+a1*x+a2*x^2:此时相当于求解方程a0+a1*x+a2*x^2=n这样的2次方程,但是要怎么解呢?利用求根公式:x=(-b±根号(b^2-4ac))/2a,然后判断这个值是否为整数就可以了。
这样一来三位以内的x就被我们用枚举a0,a1,a2,a3的方式来枚举完了。
我们可以证明a0+a1*x+a2*x^2+a3*x^3是可以将1e12以内的数表示出来的,以上三个步骤枚举完后,剩下的就是a*x^3这种情况,然后在x^3<n的范围内枚举进制就可以了、枚举x进制的就可以了。

 

 

Time complexity:不会超过O(n)开3次方次

 

Source code:

 

  

posted @   北岛知寒  阅读(327)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩