题目:
代码如下:
#include<bits/stdc++.h>
using namespace std;
int a[540001],b[2431];
void dfs(int x,int step){
a[x] = step;
if((x % 2 == 0) && (a[x / 2] == -1)) dfs(x / 2,step + 1);
if((x % 3 == 0) && (a[x / 3] == -1)) dfs(x / 3,step + 1);
if((x % 5 == 0) && (a[x / 5] == -1)) dfs(x / 5,step + 1);
}
void dfs2(int x,int step){
b[x] = step;
if((x % 2 == 0) && (b[x / 2] == -1)) dfs2(x / 2,step + 1);
if((x % 3 == 0) && (b[x / 3] == -1)) dfs2(x / 3,step + 1);
if((x % 5 == 0) && (b[x / 5] == -1)) dfs2(x / 5,step + 1);
}
int gcd(int a,int b){return b == 0 ? a : gcd(b,a % b);};
int main()
{
memset(a,-1,sizeof(a));
memset(b,-1,sizeof(b));
dfs(540000,0);
dfs2(2430,0);
for(int i = 2430;i >= 0;i--){
cout << b[i] << " " << i << " " << a[i] << endl;
if((b[i] != -1) && (a[i] != -1)){
cout << a[i] + b[i] << endl;
break;
}
}
return 0;
}
填空题,麻烦一点也没事,简单来说就是dfs记录到达每一质量需要的步数,初始化都为-1,如果同样的质量2块蛋糕都可通过切割达到,那就把二者步数相加就可以了。