HDOJ 4937 Lucky Number
当进制转换后所剩下的为数较少时(2位。3位),相应的base都比較大。能够用数学的方法计算出来。
预处理掉转换后位数为3位后,base就小于n的3次方了,能够暴力计算。
。
。。
Lucky Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 521 Accepted Submission(s): 150
“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.
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.
2
10
19
Case #1: 0
Case #2: 1Hint10 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath> using namespace std; typedef long long int LL; LL n;
set<LL> ans;
bool change(LL x,LL base)
{
bool flag=true;
while(x)
{
LL temp=x%base;
x/=base;
if(temp>=3LL&&temp<=6LL) continue;
else
{
flag=false;
break;
}
}
return flag;
} int main()
{
int T_T,cas=1;
scanf("%d",&T_T);
while(T_T--)
{
ans.clear();
cin>>n;
if(n>=3LL&&n<=6LL)
{
cout<<"Case #"<<cas++<<": -1"<<endl; continue;
} for(LL a=3;a<=6;a++)
{
for(LL b=3;b<=6;b++)
{
LL limit=max(a,b);
if( (n-b)%a == 0 )
{
if( (n-b)/a > limit)
{
ans.insert((n-b)/a);
}
}
}
} for(LL a=3;a<=6;a++)
{
for(LL b=3;b<=6;b++)
{
for(LL c=3;c<=6;c++)
{
LL C=c-n;
if(b*b >= 4LL*a*C )
{
LL deta=sqrt(b*b-4LL*a*C);
LL base1=(-b+deta)/(2*a);
LL base2=(-b-deta)/(2*a);
LL limit=max(a,max(b,c));
if(a*base1*base1+b*base1+c==n && base1>limit)
{
ans.insert(base1);
}
if(a*base2*base2+b*base2+c==n && base2>limit)
{
ans.insert(base2);
}
}
}
}
} for(LL i=4;i*i*i<=n;i++)
{
if(change(n,i))
{
ans.insert(i);
}
}
cout<<"Case #"<<cas++<<": "<<ans.size()<<endl;
}
return 0;
}
HDOJ 4937 Lucky Number的更多相关文章
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- 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 求出 ...
- HDU 4937 Lucky Number (数学,进制转换)
题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator fi ...
- 2014多校第七场1003 || HDU 4937 Lucky Number
题目链接 题意 : 给定一个十进制n,让你转化成某个进制的数,让这个数只包含3 4 5 6这些数字,这个进制就成为n的幸运数字,输出有多少幸运数字,例如19,5进制表示是34,所以5是19的一个幸运数 ...
- hdu 4937 Lucky Number
虽然算法清晰的不能再清晰,但是实现总是边角料错这错那. 题目大意: 给出n,找出一些进制,使得n在该进制下仅为3,4,5,6表示 解题思路: 首先,4-10000进制直接枚举计算出每一位 此外,最多只 ...
- HDU 4937 Lucky Number 规律题_(:зゝ∠)_
把全部合法的进制打出来会发现合法的进制都是在 n/3 n/4 n/5的边上 然后暴力边上的进制数.. #include <cstdio> #include <set> type ...
- HDU 4937 Lucky Number 搜索
题意: 给你一个数,求在多少种不同的进制下这个数每一位都是3.4.5.6中的一个. 思路: 搜索.枚举这个数在任意进制下的表示,判断是否合法.当数字只有3.4.5.6时,必定有无穷种. 因为数字太大, ...
- 水题 HDOJ 4727 The Number Off of FFF
题目传送门 /* 水题:判断前后的差值是否为1,b[i]记录差值,若没有找到,则是第一个出错 */ #include <cstdio> #include <iostream> ...
- SCU3502 The Almost Lucky Number
Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...
随机推荐
- speex的基本编码和解码流程
最近在研究speex的编码和解码流程 之前在IM上用到的都是发语音片段,这个很简单,只需要找到googlecode上gauss的代码,然后套一下就可以用了. 不过googlecode要关闭,有人将他导 ...
- Java基础知识强化44:StringBuffer类之把数组拼接成指定格式的字符串的案例
1. 先看案例代码如下: package cn.itcast_07; /* * 把数组拼接成一个字符串 */ public class StringBufferTest2 { public stati ...
- 设置开机启动时指定非ROOT用户执行相应的脚本
[root@MSJTVL-MJSP-A01 sm01]# vim /etc/rc.d/rc.local #!/bin/sh # # This script will be executed *afte ...
- html5.边框属性相关知识点
border-left 定义左边框 border-top 定义上边框 border-right 定义有边框 border-bottom 定义下边框 边框样式: dotted 边框线为点状虚线 dash ...
- C#socket通讯两个最经典错误解决方案
1.经典错误之 无法访问已释放的对象. 对象名:“System.Net.Sockets.Socket” (1).问题现场 (2).问题叙述 程序中的某个地方调用到了socket.close ...
- MS SQL到Oracle的数据迁移笔记
MS SQL到Oracle的数据迁移笔记 一.任务背景 旧系统使用MS SQL Server数据库,新系统使用Oracle数据库,现在需要将旧系统中的数据迁移到新系统中,旧数据按照约定的规则转换后,能 ...
- VMware 虚拟机(linux)增加根目录磁盘空间 转自
转自 http://wenku.baidu.com/link?url=WZDgESO0oXqYfhPYOWFalZsMglS0HKtLw7t6ICRs_sJ_sfPc85RpxsqKMwqSniis0 ...
- Java开发环境安装
一.安装JDK(java development kit) 下载地址:www.oracle.com/technetwork/java/javase/downloads 二.配置Java环境变量 1.J ...
- android DatePickerDialog配合edittext实现按日期查询
我们从网上一搜DatePickerDialog相关实现,大多都是默认的形式,也就是不带取消按钮.下边上我的代码:我将代码简单的封装到一个工具类里边 public static DatePickerDi ...
- 在预装win8的电脑上换win7系统讲解
现在买电脑,如果电脑预装的系统是win8系统,那么这个电脑的默认启动模式应该就是UEFI模式,现在UEFI模式正在逐渐取代传统模式.UEFI启动需要一个独立的分区,它将系统启动文件和操作系统本身隔离, ...