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:

//Memory   Time
// 1347K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL __int64
using namespace std; int main()
{
#ifndef ONLINE_JUDGE
freopen("cin.txt","r",stdin);
#endif
int T,kase=0;
cin>>T;
while(T--){
LL n,t,ans=0;
LL i,j,k;
LL a,b,c,d,x;
scanf("%I64d",&n);
if(n>=3&&n<=6){
printf("Case #%d: -1\n",++kase);
continue;
}
for(i=3;i<=6;i++){
for(j=3;j<=6;j++){
if((n-i)%j==0&&(n-i)/j>max(i,j))
ans++;
}
}
for(i=3;i<=6;i++){
for(j=3;j<=6;j++){
for(k=3;k<=6;k++){
a=i;b=j;c=k-n;
d=(LL)sqrt(b*b-a*c*4+0.5);
if(d*d!=b*b-a*c*4)continue;
if((d-b)%(a*2))continue;
x=(d-b)/(a*2);
if(x>max(max(i,j),k)){
ans++;
}
}
}
}
for(i=2;i*i*i<=n;i++){
t=n;
while(t){
if(t%i<3||t%i>6)
break;
t=t/i;
}
if(!t){
ans++;
}
}
printf("Case #%d: %I64d\n",++kase,ans);
}
return 0;
}

  

枚举 + 进制转换 --- hdu 4937 Lucky Number的更多相关文章

  1. HDU 4937 Lucky Number (数学,进制转换)

    题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator fi ...

  2. 2014多校第七场1003 || HDU 4937 Lucky Number

    题目链接 题意 : 给定一个十进制n,让你转化成某个进制的数,让这个数只包含3 4 5 6这些数字,这个进制就成为n的幸运数字,输出有多少幸运数字,例如19,5进制表示是34,所以5是19的一个幸运数 ...

  3. hdu 4937 Lucky Number

    虽然算法清晰的不能再清晰,但是实现总是边角料错这错那. 题目大意: 给出n,找出一些进制,使得n在该进制下仅为3,4,5,6表示 解题思路: 首先,4-10000进制直接枚举计算出每一位 此外,最多只 ...

  4. HDU 4937 Lucky Number 搜索

    题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大, ...

  5. HDU 4937 Lucky Number 规律题_(:зゝ∠)_

    把全部合法的进制打出来会发现合法的进制都是在 n/3 n/4 n/5的边上 然后暴力边上的进制数.. #include <cstdio> #include <set> type ...

  6. HDU 4937 Lucky Number(2014 Multi-University Training Contest 7)

    思路:先枚举  a*bas +b = n  求出 bas 在sqrt(n)到n的  (bas>a&&bas>b) 再枚举  a*bas*bas+b*bas+c =n  求出 ...

  7. HDU 2097 Sky数 进制转换

    解题报告:这题就用一个进制转换的函数就可以了,不需要转换成相应的进制数,只要求出相应进制的数的各位的和就可以了. #include<cstdio> #include<string&g ...

  8. HDU 2031 进制转换(10进制转R进制)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2031 进制转换 Time Limit: 2000/1000 MS (Java/Others)    M ...

  9. 11 JavaScript Number原始值&对象&科学记数法&范围&进制转换&溢出Infinity&NaN

    JavaScript Number对象 是经过封装的能处理数字值的对象 由Number()构造器创建 只有一种数字类型 可以使用也可以不使用小数点书写数字 JavaScript原始值与对象: 在Jav ...

随机推荐

  1. Backbone源码解析(三):Collection模块

    Collection模块式是对分散在项目中model的收集,他可以存储所有的model,构成一个集合,并且通过自身的方法统一操作model.Collection模块包装着若干对象,对象本身不具有一些方 ...

  2. Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境

    我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...

  3. 在MVVM模式中,按钮Click事件的绑定方法

    在MVVM模式中,我们将Button的方法写到ViewModel中,然后绑定到前端界面.通常的做法是写一个类,继承ICommand接口,然而如果按钮比较多的话,就需要写很多的类,对于后期维护造成很大的 ...

  4. js函数实现转换css中常用的颜色编码

    //转换css中常用颜色编码 var toRGB = function (val){ var reg1 = /^#([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i; ...

  5. tomcat项目无法发布异常,Could not copy all resources to .........(转)

    [plain] <span style="font-size:18px;">Deployment failure on Tomcat  6.x. Could not c ...

  6. Post方式的Http流请求调用

    HttpRequest公共类: public static class HttpRequestAction { /// <summary> /// 发送http请求并返回响应 /// &l ...

  7. vs2013中的“任务列表”菜单

    以前在java项目中经常用到todo. 现在vs2013也完美支持了. 首先,对于目前不确定而尚需完善的代码,在前面加 //TODO:by who --注释文字,比如: //TODO:lhl--类目I ...

  8. WebApi系列~QQ互联的引入(QConnectSDK)

    回到目录 感谢与改进 首先要感谢张善友老兄为大家封装的这个DLL,它将QQ官方的相关API都集成到了这个里面,这对于开发人员来说,是个福音,有人会说,为什么QQ官方没有提供.net版的SDK呢,在这里 ...

  9. javascript_core_07之错误处理、函数作用域

    1.错误处理:保证程序发生错误时,不会被强制退出: ①处理方式:try{可能出错的正常语句:}catch(err){只有出现错误时才执行的错误处理代码:}finally{无论是否出错都必须执行的代码: ...

  10. linux安装locust

    linux安装locust 1. 安装epel扩展源(目的是为了在安装Pip时不出现一堆乱七八糟的错误信息) EPEL(http://fedoraproject.org/wiki/EPEL) 是由 F ...