题目描述 Description
从m开始,我们只需要6次运算就可以计算出m31:
m2=m×m,m4=m2×m2,m8=m4×m4,m16=m8×m8,m32=m16×m16,m31=m32÷m。
请你找出从m开始,计算mn的最少运算次数。在运算的每一步,都应该是m的正整数次方,换句话说,类似m-3是不允许出现的。
输入描述 Input Description
输入为一个正整数n
输出描述 Output Description
输出为一个整数,为从m开始,计算mn的最少运算次数。
样例输入 Sample Input
样例1
1
样例2
31
样例3
70
样例输出 Sample Output
样例1
0
样例2
6
样例3
8
数据范围及提示 Data Size & Hint
n(1<=n<=1000)
数据没有问题,已经出现过的n次方可以直接调用
史上最有潜力的打表,快来围观!
#include<iostream> using namespace std; int a[1001]={0,0,1,2,2,3,3,4,3,4,4,5,4,5,5,5,4,5,5,6,5, 6,6,6,5,6,6,6,6,7,6,6,5,6,6,7,6,7,7,7,6, 7,7,7,7,7,7,7,6,7,7,7,7,8,7,8,7,8,8,8,7, 8,7,7,6,7,7,8,7,8,8,8,7,8,8,8,8,8,8,8,7, 8,8,8,8,8,8,9,8,9,8,9,8,8,8,8,7,8,8,8,8, 9,8,9,8,9,9,9,8,9,9,9,8,9,9,9,9,9,9,9,8, 9,9,9,8,9,8,8,7,8,8,9,8,9,9,9,8,9,9,9,9, 9,9,9,8,9,9,9,9,9,9,10,9,9,9,9,9,9,9,9,8, 9,9,9,9,9,9,10,9,10,9,10,9,10,10,10,9,10,10,10,9, 10,10,10,9,10,9,10,9,9,9,9,8,9,9,9,9,10,9,10,9, 10,10,10,9,10,10,10,9,10,10,10,10,10,10,10,9,10,10,10,10, 10,10,10,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,9, 10,10,10,10,10,10,10,9,10,10,10,9,10,9,9,8,9,9,10,9, 10,10,10,9,10,10,11,10,11,10,10,9,10,10,11,10,11,10,10,10, 10,10,10,10,10,10,10,9,10,10,10,10,10,10,11,10,10,10,11,10, 11,11,11,10,11,10,11,10,11,10,11,10,11,10,10,10,10,10,10,9, 10,10,10,10,10,10,11,10,11,10,11,10,11,11,11,10,11,11,11,10, 11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,10, 11,11,11,11,11,11,11,10,11,11,11,10,11,11,11,10,11,10,11,10, 10,10,10,9,10,10,10,10,11,10,11,10,11,11,11,10,11,11,11,10, 11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11, 11,11,11,11,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,11, 11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,11,11,11,11,11, 12,11,12,11,11,11,12,11,12,11,11,11,11,11,11,11,11,11,11,10, 11,11,11,11,11,11,11,11,11,11,12,11,12,11,11,10,11,11,12,11, 12,11,11,10,11,11,11,10,11,10,10,9,10,10,11,10,11,11,11,10, 11,11,12,11,12,11,11,10,11,11,12,11,12,12,11,11,12,12,12,11, 12,11,11,10,11,11,12,11,12,12,12,11,11,12,12,11,12,11,12,11, 11,11,12,11,12,11,11,11,12,11,11,11,11,11,11,10,11,11,11,11, 11,11,12,11,11,11,12,11,12,12,12,11,12,11,12,11,12,12,12,11, 12,12,12,12,12,12,12,11,12,12,12,11,12,12,12,11,12,12,12,11, 12,12,12,11,12,12,12,11,12,11,12,11,12,11,11,11,11,11,11,10, 11,11,11,11,11,11,12,11,12,11,12,11,12,12,12,11,12,12,12,11, 12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,11, 12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,11, 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12, 12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,11,12,12,12,11, 12,11,12,11,11,11,11,10,11,11,11,11,12,11,12,11,12,12,12,11, 12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,11, 12,12,12,12,12,12,12,12,12,12,13,12,12,12,12,11,12,12,12,12, 13,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,13,12,12,12,13,12,13,12,12,12,13,12,12,12, 12,12,12,11,12,12,12,12,12,12,13,12,12,12,13,12,13,12,12,12, 13,12,13,12,13,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12, 12,12,12,12,12,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12, 13,13,13,12,13,13,13,12,13,12,13,12,13,13,13,12,13,13,13,12, 13,12,13,12,12,12,13,12,13,12,12,12,12,12,12,12,12,12,12,11, 12,12,12,12,12,12,12,12,12,12,13,12,13,12,12,12,12,12,13,12, 13,13,13,12,13,13,13,12,13,12,12,11,12,12,13,12,13,13,13,12 }; int main(){ int n; cin>>n; cout<<a[n]<<endl; return 0; }
正解:仿照 快速幂(上面也AC了)
本代码做不到 0ms
#include<iostream> using namespace std; #include<cstdio> #define MAXdeep 20 int a[MAXdeep]; bool dfs(int k,int maxdepth,int n) { if(a[k]==n) return true; if(k==maxdepth) return false; int maxx=a[0]; for(int i=1;i<=k;++i) maxx=max(maxx,a[i]); if((maxx<<(maxdepth-k))<n) return false; for(int i=k;i>=0;--i)// { a[k+1]=a[i]+a[k]; if(dfs(k+1,maxdepth,n)) return true; a[k+1]=a[k]-a[i]; if(dfs(k+1,maxdepth,n)) return true; } return false; } int solve(int n) { if(n==1) return 0; a[0]=1; for(int i=1;i<MAXdeep;++i) if(dfs(0,i,n)) return i; return MAXdeep; } int main() { int n; scanf("%d",&n); printf("%d ",solve(n)); return 0; }