B - Lucky Mask
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 calls a mask of a positive integer n the number that is obtained after successive writing of all lucky digits of number n from the left to the right. For example, the mask of number 72174994 is number 7744, the mask of 7 is 7, the mask of 9999047 is 47. Obviously, mask of any number is always a lucky number.
Petya has two numbers — an arbitrary integer a and a lucky number b. Help him find the minimum number c (c > a) such that the mask of number c equals b.
Input
The only line contains two integers a and b (1 ≤ a, b ≤ 105). It is guaranteed that number b is lucky.
Output
In the only line print a single number — the number c that is sought by Petya.
Examples
1 7
7
100 47
147
解题思路:从a开始遍历直到找到c,使c从左读到右的幸运数的组合数等于b,利用string的begin和end函数和reverse函数反转string.
ac代码:
#include<iostream> #include<string> #include<algorithm> using namespace std; string mask(int a){ string ret; while(a){ if(a%10==4||a%10==7) { ret+=((a%10)+'0'); } a/=10; } reverse(ret.begin(),ret.end()); return ret; } int main() { int a; string b; cin>>a>> b; int res= a + 1; while(mask(res)!= b){ res++; } cout<<res<<endl; return 0; }
D - Lucky Number 2
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.
Input
The single line contains four integers a1, a2, a3 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) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).
Examples
2 2 1 1
4774
4 7 3 1
-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
ac代码:
#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; #define pf printf #define sf scanf #define for1(i,a) for(int (i)=1;(i)<=(a);(i)++) int a,b,c,d; int main() { cin>>a>>b>>c>>d; if(abs(c-d)>1||a+b<=c+d||a<c||b<d||a<d||b<c) { puts("-1"); } 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; }