A Prosperous Lot CodeForces - 934B

本文探讨了一道有趣的算法题目,涉及驱邪红包的传统习俗与数学逻辑。故事中提到的Nian与Sui恶魔,以及父母为了保护孩子免受侵害而给予的红包,成为了算法灵感的来源。题目要求寻找一个特定的正整数,其十进制表示中的环数等于给定的数值k。通过分析,我们发现8是最优选择,但在达到一定数量级后将无法满足条件。文章提供了一个简洁的C++代码实现,展示了如何根据k的不同情况输出相应的数字。

Apart from Nian, there is a daemon named Sui, which terrifies children and causes them to become sick. Parents give their children money wrapped in red packets and put them under the pillow, so that when Sui tries to approach them, it will be driven away by the fairies inside.

Big Banban is hesitating over the amount of money to give out. He considers loops to be lucky since it symbolizes unity and harmony.

He would like to find a positive integer n not greater than 1018, such that there are exactly k loops in the decimal representation of n, or determine that such n does not exist.

A loop is a planar area enclosed by lines in the digits’ decimal representation written in Arabic numerals. For example, there is one loop in digit 4, two loops in 8 and no loops in 5. Refer to the figure below for all exact forms.

Input
The first and only line contains an integer k (1 ≤ k ≤ 106) — the desired number of loops.

Output
Output an integer — if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positive decimal integer not exceeding 1018.

Examples
Input
2
Output
462
Input
6
Output
8080

题意: 0 4 6 9有一个环,8有两个环,其他没有环。求出一个数字,上面数位的环和答案为给定的x。如果这个数字超过1e18,输出-1.
思路: 很明显,贪8就可以了。到了37,就超过1e18了。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n;scanf("%d",&n);
    if(n > 36)printf("-1\n");
    else if(n % 2 == 0)
    {
        for(int i = 1;i <= n / 2;i++)
            printf("%d",8);
    }
    else
    {
        printf("4");
        for(int i = 1;i <= n / 2;i++)
            printf("%d",8);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值