codeforces 146D Lucky Number 2 (找规律)

£神魔★判官ぃ 2022-06-04 06:45 330阅读 0赞

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).

Example

Input

  1. 2 2 1 1

Output

  1. 4774

Input

  1. 4 7 3 1

Output

  1. -1

题意:有一长串数字只由4和7组成,现在告诉你这串数字有a1个4,a2个7,a3个47,a4个74,(比如4774有2个4,2个7,1个47,1个74。)要求让你写出一个满足条件的最小的数字。

思路:我们首先在纸上随便写出几串由4和7组成的数字,很容易发现一个特点,以4开头以7结尾,那么47一定比74多1,以7开头以4结尾,那么74一定比47多1,如果开头结尾都是4或都是7,那么47一定等于74。所以说我们一开始就可以判断出当abs(47-74)>1时,一定是输出-1的。

现在我们再分类讨论:

①当47>74时:

输出的格式一定是444…4747474747…777。这样可以保证数字最小。那么我们就要找出多余的4和多余的7的个数,它们分别是a1-a3和a2-a3,接下来循环输出即可。

②当74>47时:

输出的格式一定是7444…47474747…777。这样可以保证数字最小。那么同理,依然要找出多余的4和多余的7的个数,它们分别是a1-a4和a2-a4,接下来循环输出即可。

③当47=74时:

这个需要再分类讨论一下,首先如果a1<2&&a2<2的话肯定是输出-1。如果a1 - 1 >= a3&&a2 >= a3,那么就可以满足4放在两边的情况,那么输出格式一定是444…444747…474777…7774。多余的4和多余的7的个数分别是a1-a3-1和a2-a3。如果不满足这个条件的话,那么至少要满足a2 - 1 >= a3&&a1 >= a3,这是把7放在两边所需满足的条件,这时的输出格式是74747….474777….777。这时是不会有多余的4的,不然就可以满足4放两边的情况。所以多余的7的个数为a2-a3-1。如果这两个条件都不满足,那么就输出-1。

AC代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <cmath>
  7. #include <vector>
  8. #include <queue>
  9. #include <map>
  10. #include <algorithm>
  11. #include <set>
  12. #include <functional>
  13. using namespace std;
  14. typedef long long LL;
  15. typedef unsigned long long ULL;
  16. const int INF = 1e9 + 5;
  17. const int MAXN = 1005;
  18. const int MOD = 1e9 + 7;
  19. const double eps = 1e-8;
  20. const double PI = acos(-1.0);
  21. LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a%b); }
  22. LL ppow(LL a, LL b) { LL res = 1; for (int i = 1; i <= b; i++) res *= a; return res; }
  23. LL quick_mod(LL a, LL b, LL c) { LL ans = 1; while (b) { if (b % 2 == 1)ans = (ans*a) % c; b /= 2; a = (a*a) % c; }return ans; }
  24. int main()
  25. {
  26. int a1, a2, a3, a4;
  27. while (scanf("%d%d%d%d", &a1, &a2, &a3, &a4) != EOF)
  28. {
  29. if (abs(a3 - a4) >= 2){printf("-1\n"); continue;}
  30. else if (a3 == a4)
  31. {
  32. if (a1 < 2 && a2 < 2){printf("-1\n");continue;}
  33. if (a1 - 1 >= a3&&a2 >= a3)
  34. {
  35. a1 = a1 - a3 - 1;
  36. a2 = a2 - a3;
  37. for (int i = 1; i <= a1; i++) printf("4");
  38. for (int i = 1; i <= a3; i++) printf("47");
  39. for (int i = 1; i <= a2; i++) printf("7");
  40. printf("4\n");
  41. continue;
  42. }
  43. else if (a2 - 1 >= a3&&a1 >= a3)
  44. {
  45. a2 = a2 - 1 - a3;
  46. printf("7");
  47. for (int i = 1; i <= a3; i++) printf("47");
  48. for (int i = 1; i <= a2; i++) printf("7");
  49. printf("\n");
  50. continue;
  51. }
  52. else{printf("-1\n");continue;}
  53. }
  54. else if (a3 > a4)
  55. {
  56. a1 = a1 - a3;
  57. a2 = a2 - a3;
  58. if (a1 < 0 || a2 < 0){printf("-1\n");continue;}
  59. for (int i = 1; i <= a1; i++) printf("4");
  60. for (int i = 1; i <= a3; i++) printf("47");
  61. for (int i = 1; i <= a2; i++) printf("7");
  62. printf("\n");
  63. }
  64. else
  65. {
  66. a1 = a1 - a4;
  67. a2 = a2 - a4;
  68. if (a1 < 0 || a2 < 0){printf("-1\n");continue;}
  69. printf("7");
  70. for (int i = 1; i <= a1; i++) printf("4");
  71. for (int i = 1; i <= a3; i++) printf("47");
  72. for (int i = 1; i <= a2; i++) printf("7");
  73. printf("4\n");
  74. }
  75. }
  76. }

发表评论

表情:
评论列表 (有 0 条评论,330人围观)

还没有评论,来说两句吧...

相关阅读