枚举 + 进制转换 --- 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
“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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | //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; } |
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3908369.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?