天天看點

枚舉 + 進制轉換 --- hdu 4937 Lucky NumberLucky 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

<a href="http://www.cnblogs.com/search.php?field=problem&amp;key=2014%20Multi-University%20Training%20Contest%207&amp;source=1&amp;searchmode=source">2014 Multi-University Training Contest 7</a>

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&gt;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&lt;n的範圍内枚舉進制就可以了、枚舉x進制的就可以了。

Time complexity:不會超過O(n)開3次方次

Source code: