In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are n extensions, the i-th of them multiplies the width or the length (by Arkady's choice) by ai. Each extension can't be used more than once, the extensions can be used in any order.
Now Arkady's field has size h × w. He wants to enlarge it so that it is possible to place a rectangle of size a × b on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal.
The first line contains five integers a, b, h, w and n (1 ≤ a, b, h, w, n ≤ 100 000) — the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions.
The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 100 000), where ai equals the integer a side multiplies by when the i-th extension is applied.
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
3 3 2 4 4
2 5 4 10
1
3 3 3 3 5
2 3 5 4 2
0
5 5 1 2 3
2 2 3
-1
3 4 1 1 3
2 3 2
3
In the first example it is enough to use any of the extensions available. For example, we can enlarge h in 5 times using the second extension. Then h becomes equal 10 and it is now possible to place the rectangle on the field.
没时间看的D居然是道暴力可以过的题唉,想着后面很难就没做,确实CF这种两小时赛制的比较机灵,有人觉得A难读,分数不高就放弃A,后面的题倒是容易看出他让你做什么。这个题的意思是你有一块h*w的
地经过放大ai倍使其不小于a*b,这不是直接dfs都可以过么,有些人以为这个扩大是相加,可能是读错题啦,这种写法类似于记忆化搜索
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n; int tar[N]; int ans = n + 1; bool cmp(int a, int b) { return a > b; } void dfs(int cur_a, int cur_b, int step) { if (!cur_a && !cur_b) { ans = min(ans, step); return; } if (step == n) return; if (tar[step] == 2) { while (cur_a) cur_a /= 2, step++; while (cur_b) cur_b /= 2, step++; ans = min(ans, step); return; } if (cur_a) dfs(cur_a / tar[step], cur_b, step + 1); if (cur_b) dfs(cur_a, cur_b / tar[step], step + 1); } int main() { int a, b, h, w; while (~scanf("%d%d%d%d%d", &a, &b, &h, &w, &n)) { ans = n + 1; for (int i = 0; i < n; i++) scanf("%d", tar + i); sort(tar, tar + n, cmp); dfs((a - 1) / h, (b - 1) / w, 0); dfs((a - 1) / w, (b - 1) / h, 0); ans == n + 1 ? printf("-1 ") : printf("%d ", ans); } return 0; }