题目链接:https://vjudge.net/problem/CodeForces-1249C1
题目描述:
因为万圣节数字都是用3的不同次幂组成的,所以对于每个3的幂来说, 只有可以用来组成万圣节数字和不能用来组成万圣节数字两种情况,这题因为是easy version, n给的比较小,所以我们给定一个区间直接暴☆搜出所有在区间内大于等于n的数然后取最小值即可
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll ll_INF = 233333333333333;
const int maxn = 1e3 + 10;
int sign, kase;
ll list[1000], res[1000], num;
bool dfs(int i, ll sum)
{
if (i == sign+1)
{
if (sum >= num)
res[kase++] = sum;
return sum == num;
}
return dfs(i+1, sum) || dfs(i+1, sum + list[i]);
}
int main(void)
{
ll fac = 1;
list[0] = 1;
for (int i = 1; fac < 10000; i++) //先打一个3的最高次幂的表, 打到比10000大就行了
{
fac *= 3;
list[i] = fac;
}
int t;
cin >> t;
while(t--)
{
fill(res, res+1000, ll_INF); //用一个足够大的数初始化数组
kase = 0;
scanf("%lld", &num);
sign = -1;
while(num - list[++sign] > 0); //找出一个比num大的3的最小次幂
dfs(0, 0); //把比num大且最高次幂不大于num的最小次幂的数全部搜出来
printf("%lld\n", *min_element(res, res+1000)); //输出结果中的最小值
}
return 0;
}