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 47, 744, 4 are lucky and 5, 17, 467 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) = 2, cnt(7) = 4, cnt(47) = 2, cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.
The single line contains four integers a1, a2, a3 and a4 (1 ≤ a1, a2, a3, a4 ≤ 106).
On the single line print without leading zeroes the answer to the problem — the minimum lucky number d such, that cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).
2 2 1 1
4774
4 7 3 1
-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;
}