您的位置:首页 > 理论基础 > 计算机网络

哈尔滨理工大学软件学院ACM程序设计全国邀请赛(网络同步赛)L Lucky Number By Assassin 杀人的模拟题

2016-12-04 14:02 453 查看
Description Alex loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 6 and 8. For example, numbers 68, 688, 6 are lucky and 5, 18, 467 are not.

Alex loves long lucky numbers very much. He is interested in the minimum lucky number d that meets some condition. Let cnt(x) be the number of occurrences of number x in number d as a substring. For example, if d = 868868, then cnt(6) = 2, cnt(8) = 4, cnt(68) = 2, cnt(86) = 2. Petya wants the following condition to fulfil simultaneously: cnt(6) = a1, cnt(8) = a2, cnt(68) = a3, cnt(86) = a4. Alex is not interested in the occurrences of other numbers. Help him cope with this task.

Input Input contains multiple test cases. For each test case , the single line contains four integers a1, a2, a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 1000000).

Output For each test case, output a single line print without leading zeroes the answer to the problem — the minimum lucky number d such, that cnt(6) = a1, cnt(8) = a2, cnt(68) = a3, cnt(86) = a4. If such number does not exist, print the single number “-1” (without the quotes).

Sample Input

2 2 1 1

Sample Output

6886

为什么说他杀人,因为我太水了。。。这个题目看似难其实水。。。

首先我们判断一下“68”和“86”的个数必须是有关系的,他们的绝对值相差不超过1!我们推一下,如果我们想68比86多2个,那么68 68,中间加上谁呢?加上8或者6都会影响到最后68和86的值,那么我们发现只有:

cnt(68)==cnt(86)

cnt(68)==cnt(86)+1

cnt(68)==cnt(86) -1

三种情况。

情况一:cnt(68)==cnt(86)

这个时候就是68686或者86868的组合,首尾一定是封闭的。那么为了最小我们一定要先优先第一种情况,第一种情况至少要满足:

cnt(6)>=cnt(68) +1 and cnt(8)>=cnt(68)

这种情况下多余6加到开头,多余8加到68686最后一个8的后面。

给个例子:

4 3 2 2

6686886

如果第一种挂了,那么我么还可以选择第二种情况,但是必须满足

cnt(6)>=cnt(68) and cnt(8)>=cnt(68)+1

这个时候6一定只有cnt(68)个了(否则一定是第一种情况最好),8补到末尾即可。

栗子:

2 6 2 2

86868888

如果都不满足肯定就不可能了输出-1

情况二:cnt(68)==cnt(86)+1

这个时候是类似6868686868的组合,注意这个必须满足:

cnt(6)>=cnt(68) and cnt(8)>=cnt(68) 这个要注意了

之后有6补到开头,有8补到末尾。

粒子:

5 5 3 2

6668686888

情况三:cnt(68)==cnt(86)-1

这个时候是类似86868686的组合,注意这个必须满足:

cnt(6)>=cnt(86) and cnt(8)>=cnt(86) 这个要注意了

之后有6补到868686第一个6后面,有8补到868686 最后一个8后面。

李子:

5 5 2 3

8666868886

思路:猛一下不好做出来,细心也是还好,主要还是我太菜了

。。。

#include<bits/stdc++.h>
using namespace std;
long long n,k;
int main(){
long long  i,j;
long long a,b,c,d;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&d)!=EOF){
string s="";
if(c==d){
if(a>=c+1&&b>=d){
for(i=1;i<=a-(c+1);i++) s+="6";
for(i=1;i<=c;i++){
if(i!=c) s+="68";
else {
s+="6";
for(j=1;j<=b-(c-1);j++) s+="8";
s+="6";
}
}
cout<<s<<endl;
}
else if(a>=c&&b>=d+1){
for(i=1;i<=c;i++)
s+="86";
for(i=1;i<=b-c;i++) s+="8";
cout<<s<<endl;
}
else {
cout<<-1<<endl;
}
}
else if(c==d+1){
if(a>=c&&b>=c){                 //注意判断条件
for(i=1;i<=a-c;i++) s+="6";
for(i=1;i<=c;i++) s+="68";
for(i=1;i<=b-c;i++) s+="8";
cout<<s<<endl;
}
else {
cout<<-1<<endl;
}
}
else if(c==d-1){
if(a>=d&&b>=d){                 //注意判断条件
s+="8";
for(i=1;i<=a-(d-1);i++) s+="6";
for(i=1;i<=d-2;i++) s+="86";
for(i=1;i<=b-(d-1);i++) s+="8";
s+="6";
cout<<s<<endl;
}
else {
cout<<-1<<endl;
}
}
else {
cout<<-1<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐