From 3bb90b57d033b238bbe141016786a0b6e906dd17 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 6 Aug 2022 15:59:08 +0800 Subject: [PATCH] F - Modulo https://codeforces.com/gym/103860/submission/167192783 --- Gym/103860/F/F.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Gym/103860/F/F.cpp diff --git a/Gym/103860/F/F.cpp b/Gym/103860/F/F.cpp new file mode 100644 index 00000000..e4c84a01 --- /dev/null +++ b/Gym/103860/F/F.cpp @@ -0,0 +1,53 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 22; + +int n; +long long a[N], x, ans; +bool vis[N]; + +void dfs(int idx, long long now, int cnt) { + if (now < ans) return; + if (cnt == n || now < a[1]) { + ans = std::max(ans, now); + + return; + } + + vis[idx] = true; + + for (int i = 1; i <= n; i++) { + if (now < a[i]) break; + if (vis[i]) continue; + + dfs(i, now % a[i], cnt + 1); + } + + vis[idx] = false; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + cin >> x; + + std::sort(a + 1, a + 1 + n); + + dfs(0, x, 0); + + cout << ans << endl; + + return 0; +}