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.
![](https://i-blog.csdnimg.cn/blog_migrate/4a61150d68d78cf5c363d8843e0373aa.png)
The first and only line contains an integer k (1 ≤ k ≤ 106) — the desired number of loops.
Output an integer — if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positivedecimal integer not exceeding 1018.
2
462
6
8080
题意:0~9这十个数字里,有几个数字的形状里有环,就是首尾连接了。现在给你一个k,代表构造一个有k个圈的正整数,小于1e18,如果不存在输出-1。
思路:这题和A题有同样的特点:有神坑,有且仅有5个数字有圈:0,4,6,8,9,其中8里面有两个环。所以直接构造8,80,88,880,888,8880,1e18十九位,所以最长可以构造16位个8,即最大的k仅有36,当k>36时,直接特判-1。最大的坑是,k等于1的答案一定不是0,题目要求正整数,所以输出4,6,9均可AC。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll ans[50],k,tmp;
int main()
{
ans[1]=6;tmp=8;
for(ll i=2;i<=36;i++)
{
ans[i]=tmp;
if(i&1)tmp+=8;
else tmp*=10;
}
while(~scanf("%lld",&k))
{
if(k>36)printf("-1\n");
else printf("%lld\n",ans[k]);
}
return 0;
}