aboutsummaryrefslogtreecommitdiff
path: root/PolyCarpAndDividend/main.cpp
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2022-11-01 11:51:53 +0200
committeromagdy7 <omar.professional8777@gmail.com>2022-11-01 11:51:53 +0200
commit91ca20b8f78eeeb74fc4eea6eeecdca9fefa5656 (patch)
tree0b8fa3a57f5e442c8134f089e64019aba89c5b19 /PolyCarpAndDividend/main.cpp
parentf2c878488b659cd19ccecc9880e042251585fdbb (diff)
downloadcompetitive-programming-91ca20b8f78eeeb74fc4eea6eeecdca9fefa5656.tar.xz
competitive-programming-91ca20b8f78eeeb74fc4eea6eeecdca9fefa5656.zip
Added new problems
Diffstat (limited to 'PolyCarpAndDividend/main.cpp')
-rw-r--r--PolyCarpAndDividend/main.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/PolyCarpAndDividend/main.cpp b/PolyCarpAndDividend/main.cpp
new file mode 100644
index 0000000..81e6a92
--- /dev/null
+++ b/PolyCarpAndDividend/main.cpp
@@ -0,0 +1,49 @@
+#include <bits/stdc++.h>
+
+using namespace std;
+
+const int N = 1234567;
+
+int a[12], p[N], e[N];
+bool vis[N];
+
+void print(int x) {
+ if (x == -1) return;
+ print(p[x]);
+ printf("%d", e[x]);
+}
+
+int main() {
+ int n, m;
+ scanf("%d", &n);
+ for (int i = 0; i < n; i++) {
+ scanf("%d", a + i);
+ }
+ sort(a, a + n);
+ scanf("%d", &m);
+ int cnt = 0;
+ queue < pair <int, int> > q;
+ q.push(make_pair(-1, 0));
+ while (!q.empty()) {
+ int u = q.front().first;
+ int x = q.front().second;
+ q.pop();
+ for (int i = 0; i < n; i++) {
+ int v = (x * 10 + a[i]) % m;
+ if (u == -1 && a[i] == 0) continue;
+ if (!vis[v]) {
+ vis[v] = true;
+ q.push(make_pair(cnt, v));
+ e[cnt] = a[i];
+ p[cnt++] = u;
+ }
+ if (v == 0) {
+ print(cnt - 1);
+ return 0;
+ }
+ }
+ }
+ puts("-1");
+ return 0;
+}
+