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. 冲刺阶段 day13

    ---恢复内容开始--- 项目进展 今天星期三,我们将专业管理部分又继续做了完善,之前漏掉的几项功也一一补全,没能实现的数据库部分也已经进行了完善,并且对已经完成的部分进行了检测,数据库的部分还有待进 ...

  2. Unity3D热更新全书FAQ

    只要有程序员朋友们问过两次的问题 就会收录在此FAQ中 1.C#Light对比LUA有什么好处 C#Light是静态类型脚本语言,语法同C#,Lua是动态类型脚本语言,这两种都有人喜欢. 我更喜欢静态 ...

  3. 如何为编程爱好者设计一款好玩的智能硬件(三)——该选什么样的MCU呢?

    一.我的构想:如何为编程爱好者设计一款好玩的智能硬件(一)——即插即用.积木化.功能重组的智能硬件模块构想 二.别人家的孩子:如何为编程爱好者设计一款好玩的智能硬件(二)——别人是如何设计硬件积木的! ...

  4. knh

    市场调研,分析—— 决定是否创业 不要再极度的沉默无言.宅.无存在感,无趣,难熬..

  5. 分割超大Redis数据库例子

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/124.html?1455853509 薄荷 App 上的伙伴功能大量使用了 ...

  6. [Java面试十一]数据库总结.

    问题及描述: --1.学生表 Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course ...

  7. Backbone中 View之间传值的解决办法

    Backbone中的View就是用来展示由Model层传出的数据,或者在View里产生的一些数据,包括输入框中输入等产生的数据,由当前View传递到另外一个View层里,应该怎么办呢,我之前读到一位博 ...

  8. 动手搭个wordpress

    看到很多人都是自己搭建博客服务器,然后一切都在自己的掌控之下,这样就不存在什么迁移,数据安全之类的问题,当然需要自己搞个空间了,不过现在都便宜的不行,$15/year,也是醉了.我不怎么写博客,但是个 ...

  9. JsBridge实现Javascript和Java的互相调用

    前端网页Javascript和Native互相调用在手机应用中越来越常见,JsBridge是最常用的解决方案. 在Android开发中,能实现Javascript与Native代码通信的,有4种途径: ...

  10. Enterprise Solution 界面设计规范

    Enteprise Solution有一套自己的界面设计规范,也是很多年(10年以上)管理软件界面精华的积累.没有一个软件从一开始就很善于界面设计,许多个小小的改善,比如控件位置的移动,控件摆放顺序的 ...