Codeforces Round #104 (Div.2)補題報告
B - Lucky Mask
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits4and7. For example, numbers47,744,4are lucky and5,17,467are not.
Petya calls amaskof a positive integernthe number that is obtained after successive writing of all lucky digits of numbern
Petya has two numbers — an arbitrary integeraand a lucky numberb. Help him find the minimum numberc(c > a)such that the mask of numberc
Input
The only line contains two integersaandb(1 ≤ a, b ≤ 105). It is guaranteed that numberbis lucky.
Output
In the only line print a single number — the numbercthat is sought by Petya.
Examples
Input1 7Output
7Input
100 47Output
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; }View Code
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 digits4and7. For example, numbers47,744,4are lucky and5,17,467are not.
Petya loves long lucky numbers very much. He is interested in theminimumlucky numberdthat meets some condition. Letcnt(x)be the number of occurrences of numberxin numberdas a substring. For example, ifd = 747747, thencnt(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 integersa1,a2,a3anda4(1 ≤ a1, a2, a3, a4 ≤ 106).
Output
On the single line print without leading zeroes the answer to the problem — the minimum lucky numberdsuch, thatcnt(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
Input2 2 1 1Output
4774Input
4 7 3 1Output
-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; }View Code