题解:P11229 [CSP-J 2024] 小木棍
ZMQ_Ink6556 · · 题解
题解:P11229 [CSP-J 2024] 小木棍
小木棍是爆搜剪枝,所以小木棍也是爆搜剪枝。
爆搜剪枝款。
解题思路
显而易见,位数少的数一定比位数多的数小。所以我们可以先求出最小位数
暴力也很简单,从小到大枚举最小位数每一位分别放
此时,加入一个极其简单的剪枝,如果剩余的
if(ceil(1.0 * 剩余的 $n$ / 7.0) + 已经拼过的位数 - 1 > 最小位数)
{
return 0;
}
复杂度分析:玄学
代码
#include<bits/stdc++.h>
using namespace std;
int t , n , maxws , ans[100005];
const int cost[10] = {6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6};
bool dfs(int deep , int nless)
{
if(deep > maxws)
{
if(nless == 0)
{
return 1;
}
return 0;
}
if(ceil(1.0 * nless / 7.0) + deep - 1 > maxws)
{
return 0;
}
if(deep == 1)
{
for(int i = 1 ; i <= 9 ; i++)
{
if(dfs(deep + 1 , nless - cost[i]))
{
ans[deep] = i;
return 1;
}
}
return 0;
}
for(int i = 0 ; i <= 9 ; i++)
{
if(dfs(deep + 1 , nless - cost[i]))
{
ans[deep] = i;
return 1;
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while(t--)
{
cin >> n;
if(n == 1)
{
cout << "-1\n";
continue;
}
maxws = ceil(1.0 * n / 7.0);
dfs(1 , n);
for(int i = 1 ; i <= maxws ; i++)
{
cout << ans[i];
}
cout << '\n';
}
return 0;
}