CodeForces - 1249C1 Good Numbers (easy version)

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值