codeforces 146D Lucky Number 2

D. Lucky Number 2
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Petya 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 = 747747, then cnt(4) = 2cnt(7) = 4cnt(47) = 2cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1cnt(7) = a2cnt(47) = a3cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.

Input

The single line contains four integers a1a2a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).

Output

On the single line print without leading zeroes the answer to the problem — the minimum lucky number d such, that cnt(4) = a1cnt(7) = a2cnt(47) = a3cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).

Examples
input
2 2 1 1
output
4774
input
4 7 3 1
output
-1

题意:给a个4,b个7,能否构成最小的x使得x含c个47,d个74,如果不能输出-1

随便写一个有4 7构成的数可以发现首尾都是4或7时,47和74的个数相等,第一个是4最后一个是7时47的个数比74多一个,第一个是7最后一个是4时74的个数比47多一个,所以47和74的个数差的绝对值应小于等于1

如果c>d,第一个和最后一个分别是4和7,中间输出d个74,要使构成的数最小,4一定放在前面输出,7放在后面输出

如果c<d,第一个和最后一个分别是7和4,中间输出c个47,4一定在前面输出,7在最后一个4之前输出

如果c=d,如果a>c,则以4为开头和结尾输出,输出a-c-1个4,再输出c个47,b-c个7,再输出一个4

如果a=c,以7为开头和结尾输出,输出c个47,b-c-1个7


#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,mi
#define rson rt<<1|1,mi+1,r
#define it rt,l,r
#define root 1,1,n
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int a,b,c,d;

int main()
{
    while(~sf("%d%d%d%d",&a,&b,&c,&d))
    {
	//当a,b任意一个小于c,d时输出-1,a+b=c+d时也不能满足要求,比如2 2 2 2 
        if(abs(c-d)>1||a+b<=c+d||a<c||b<d||a<d||b<c)
        {
            puts("-1");
            continue;
        }
        else if(c>d)
        {
            a=a-d,b=b-d;
            for1(i,a)pf("4");
            for1(i,d)pf("74");
            for1(i,b)pf("7");
            puts("");
        }
        else if(c==d)
        {
            if(a>c)
            {
                a-=1+c;
                b-=c;
                for1(i,a)pf("4");
                for1(i,c)pf("47");
                for1(i,b)pf("7");
                pf("4");
                puts("");
            }
            else
            {
                b-=1+c;
                pf("7");
                for1(i,c)pf("47");
                for1(i,b)pf("7");
                puts("");
            }
        }
        else
        {
            a-=c+1,b-=c+1;
            pf("7");
            for1(i,a)pf("4");
            for1(i,c)pf("47");
            for1(i,b)pf("7");
            pf("4");
            puts("");
        }
    }
    return 0;
}








  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值