B. 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 digits4 and7.
For example, numbers47, 744,4 are lucky and5,
17,467 are not.
Petya loves long lucky numbers very much. He is interested in theminimum lucky numberd that meets some condition. Let
Cnt (x) be the number of occurrences of numberx in numberd as a substring. for example, ifd records = listen 747747, thencnt (4) records = listen 2, cnt (7) records = listen 4, cnt (47) records = listen 2, cnt (74) limit = Limit 2.
Petya wants the following condition to fulfil simultaneously: cnt (4) random = a1, cnt (7) random = random a2, cnt (47) random = random a3, cnt (74) vertex = maid. Petya is not interested
In the occurrences of other numbers. Help him with this task.
Input
The single line contains four integersa1, a2,
A3 anda4 (1 Gbit/s ≤ a1, a2, a3, and a4 Gbit/s ≤ 106 ).
Output
On the single line print without leading zeroes the answer to the problem-the minimum lucky numberd such, thatcnt (4) blocks = random a1, cnt (7) blocks = random a2, cnt (47) mask = 108a3, cnt (74) mask = 109a4.
If such number does not exist, print the single number "-1" (without the quotes ).
Sample test (s) Input
2 2 1 1
Output
4774
Input
4 7 3 1
Output
-1
I haven't looked at acm for a long time. Today I have done it. After a long time, I feel like I am giving birth to the question: a sequence consists of 4 and 7, with a given number of 4, 7: The number of 47, the number of 74, and the minimum sequence that meets these conditions. Ideas:
Assume that the numbers of 4, 7, 47, and 74 are abcd.
Situation analysis:
When c> = d
Construct a sequence of 474747... 47 (c 47), which contains the C-1 74
When d <C-1, no matter how 4 or 7 is added in this sequence, the number of 74 cannot be reduced, so no solution is available at this time.
When d = C-1, this sequence exactly meets the 3, 4 conditions, so as long as all the remaining 4 are placed on the left of the sequence, and the remaining 7 on the right is the final sequence.
When d = c, because the constructed sequence already contains C-1 74, you only need to construct another 74, and put one at the end of the final sequence in the remaining 4.
When d> c
Construction Sequence 747474... 74 (d 74), which contains D-1 47
When c <D-1, no number of 47 numbers can be reduced no matter how 4 or 7 is added to the constructed sequence.
When c = D-1 is, the construction sequence has met the 3, 4 conditions, but at this time, the remaining 4 cannot be simply added to the front of the construction sequence, you cannot add the remaining 7 to the end of the construction sequence, because if this will make the sequence no longer meet the 3, 4 conditions,
When d> 1, the final sequence should be like 7 (the remaining 4) 47474 .... 7 (the remaining 7) 4. When d = 1, the sequence should be like this (the remaining 7) 74 (the remaining 4)
The above is the idea of solving the problem. Another worry is that it is difficult to judge the problem without a solution. Three errors have been made here.
The code is improved 1.1 points after Wa, and the logic is somewhat redundant.
#include<iostream>using namespace std;int main(){int i ,a ,b ,c ,d ;while(cin>>a>>b>>c>>d && a|b|c|d){if(a-c <0 || b -c<0 || a - d <0 || b - d < 0){cout<<"-1"<<endl;continue;}if(c >= d){if(d < c-1){cout<<"-1";}else if( d == c-1){if(a-c>=0 && b-c>=0){for(i = 0 ; i < a - c ;i ++)cout<<'4';for(i = 0 ; i < c ;i ++)cout<<"47";for(i = 0 ; i < b - c ; i++)cout<<'7';}elsecout<<'-1';}else if( c==d && c!=0 ){if(a-c>0 && b-c>=0){for(i = 0 ; i < a - c - 1 ;i ++)cout<<'4';for(i = 0 ; i < c ;i ++)cout<<"47";for(i = 0 ; i < b - c ; i++)cout<<'7';cout<<'4';}else if(a-c==0 && b-c>0){cout<<'7';for(i = 0 ; i < a - c ;i ++)cout<<'4';for(i = 0 ; i < c ;i ++)cout<<"47";for(i = 0 ; i < b - c -1;i++)cout<<'7';}elsecout<<"-1";}elsecout<<"-1";}else{if(c < d-1){cout<<"-1";}else{if(d>1 && a-d>=0 && b-d>=0){cout<<'7';for(i = 0 ; i < a - d ;i ++)cout<<'4';cout<<'4';for(i = 0 ; i < d -2 ;i ++)cout<<"74";if(d >1)cout<<'7';for(i = 0 ; i < b - d ; i++)cout<<'7';if(d>1)cout<<'4';}else if(a - d >=0 && b - d>=0){for(i = 0 ; i < b ; i++)cout<<'7';for(i = 0 ; i < a - d ;i ++)cout<<'4';cout<<'4';}elsecout<<"-1";}}cout<<endl;}return 0;}