output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; int MAX_INT = numeric_limits<int>::max(); int MIN_INT = numeric_limits<int>::min(); long long MAX_LL = numeric_limits<long long>::max(); long long MIN_LL = numeric_limits<long long>::min(); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; int n; int x; for (int h = 0; h < t; h++) { cin >> n >> x; cout << 2 * x << endl; } }
### Prompt Generate a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int MAX_INT = numeric_limits<int>::max(); int MIN_INT = numeric_limits<int>::min(); long long MAX_LL = numeric_limits<long long>::max(); long long MIN_LL = numeric_limits<long long>::min(); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; int n; int x; for (int h = 0; h < t; h++) { cin >> n >> x; cout << 2 * x << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, n, x; cin >> t; while (t--) { cin >> n >> x; cout << 2 * x << "\n"; } }
### Prompt Generate a CPP solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, n, x; cin >> t; while (t--) { cin >> n >> x; cout << 2 * x << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; long long result; long long n, x; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> x; result = x * 2; cout << result << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; long long result; long long n, x; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> x; result = x * 2; cout << result << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int arr[1000000]; int main() { long long t, a, b, n; cin >> t; while (t > 0) { cin >> a >> b; cout << 2 * b << endl; t--; } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int arr[1000000]; int main() { long long t, a, b, n; cin >> t; while (t > 0) { cin >> a >> b; cout << 2 * b << endl; t--; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int a, b; cin >> a >> b; cout << 2 * b << endl; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int a, b; cin >> a >> b; cout << 2 * b << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("unroll-loops") #pragma GCC optimize("O2") constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; constexpr long long INF = 1999999999999999997; constexpr int inf = INT_MAX; constexpr int MAXSIZE = int(1e6) + 5; constexpr auto PI = 3.14159265358979323846L; constexpr auto oo = numeric_limits<int>::max() / 2 - 2; constexpr auto eps = 1e-6; constexpr auto mod = 1000000007; constexpr auto MOD = 1000000007; constexpr auto MOD9 = 1000000009; template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template <typename T> int SIZE(const T &t) { return t.size(); } string to_string(string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(char c) { return string({c}); } template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(A(&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += '\n'; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += '\n'; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += '\n'; return res; } void dbgs() { ; } template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgs(T...); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << "\n"; } }
### Prompt Generate a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("unroll-loops") #pragma GCC optimize("O2") constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; constexpr long long INF = 1999999999999999997; constexpr int inf = INT_MAX; constexpr int MAXSIZE = int(1e6) + 5; constexpr auto PI = 3.14159265358979323846L; constexpr auto oo = numeric_limits<int>::max() / 2 - 2; constexpr auto eps = 1e-6; constexpr auto mod = 1000000007; constexpr auto MOD = 1000000007; constexpr auto MOD9 = 1000000009; template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template <typename T> int SIZE(const T &t) { return t.size(); } string to_string(string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(char c) { return string({c}); } template <size_t N> string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(A(&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += '\n'; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += '\n'; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += '\n'; return res; } void dbgs() { ; } template <typename Heads, typename... Tails> void dbgs(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgs(T...); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n, x; cin >> n >> x; cout << 2 * x << endl; } return 0; }
### Prompt Please create a solution in cpp to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n, x; cin >> n >> x; cout << 2 * x << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e9 + 7; const int N = 1e4 + 500; long long t, n, x; int main() { cin >> t; while (t--) { cin >> n >> x; cout << x * 2 << "\n"; } }
### Prompt Your task is to create a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e9 + 7; const int N = 1e4 + 500; long long t, n, x; int main() { cin >> t; while (t--) { cin >> n >> x; cout << x * 2 << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; long long x, n; int main() { int q; cin >> q; for (int i = 0; i < q; i++) { cin >> n >> x; cout << (2 * x) << endl; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long x, n; int main() { int q; cin >> q; for (int i = 0; i < q; i++) { cin >> n >> x; cout << (2 * x) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n, x; int T; while (cin >> T) while (T--) { cin >> n >> x; cout << 2 * x << endl; } return 0; }
### Prompt Generate a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n, x; int T; while (cin >> T) while (T--) { cin >> n >> x; cout << 2 * x << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void c_p_c() {} int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n, x; cin >> n >> x; cout << 2 * x << endl; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void c_p_c() {} int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n, x; cin >> n >> x; cout << 2 * x << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, i, n, x; cin >> t; for (i = 1; i <= t; i++) { cin >> n >> x; cout << 2 * x << endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, i, n, x; cin >> t; for (i = 1; i <= t; i++) { cin >> n >> x; cout << 2 * x << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T; int n, x; int main() { scanf("%d", &T); for (int ii = 0; ii < T; ii++) { scanf("%d%d", &n, &x); int ans = x * 2; printf("%d\n", ans); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int T; int n, x; int main() { scanf("%d", &T); for (int ii = 0; ii < T; ii++) { scanf("%d%d", &n, &x); int ans = x * 2; printf("%d\n", ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long T; cin >> T; while (T--) { long long n, x; cin >> n >> x; cout << 2 * x << endl; } }
### Prompt Your task is to create a CPP solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long T; cin >> T; while (T--) { long long n, x; cin >> n >> x; cout << 2 * x << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int i, j, t; cin >> t; vector<int> N; vector<int> X; for (i = 0; i < t; i++) { int n, x; cin >> n; cin >> x; N.push_back(n); X.push_back(x); } for (j = 0; j < t; j++) { cout << (2 * X[j]) << endl; } }
### Prompt Develop a solution in CPP to the problem described below: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int i, j, t; cin >> t; vector<int> N; vector<int> X; for (i = 0; i < t; i++) { int n, x; cin >> n; cin >> x; N.push_back(n); X.push_back(x); } for (j = 0; j < t; j++) { cout << (2 * X[j]) << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t, x, n; cin >> t; while (t--) { cin >> n >> x; cout << 2 * x << "\n"; } }
### Prompt Your task is to create a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t, x, n; cin >> t; while (t--) { cin >> n >> x; cout << 2 * x << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int q[10000001]; vector<int> v; int main() { long long n, a, b, i, cnt = 0, c = 0; cin >> n; while (n--) { cin >> a >> b; cout << b * 2 << "\n"; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int q[10000001]; vector<int> v; int main() { long long n, a, b, i, cnt = 0, c = 0; cin >> n; while (n--) { cin >> a >> b; cout << b * 2 << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int x, y; cin >> x >> y; cout << y * 2 << "\n"; } }
### Prompt Your task is to create a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int x, y; cin >> x >> y; cout << y * 2 << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; int x; cin >> n >> x; cout << 2 * x << endl; } }
### Prompt Create a solution in cpp for the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; int x; cin >> n >> x; cout << 2 * x << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long MOD2 = 998244353; const long long N = 2e5 + 5; const long double pi = 3.14159265359; void SRAND() { auto duration = std::chrono::system_clock::now().time_since_epoch(); auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); srand(millis); } long long t, n, x; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> x; cout << x * 2 << "\n"; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long MOD2 = 998244353; const long long N = 2e5 + 5; const long double pi = 3.14159265359; void SRAND() { auto duration = std::chrono::system_clock::now().time_since_epoch(); auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); srand(millis); } long long t, n, x; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> x; cout << x * 2 << "\n"; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; using ll = long long; using ld = long double; using itn = int; using dd = double; mt19937 gen(41); const dd eps = 1e-7; const ll MAXN = 2097152; const ll INF = 1e9; const dd pi = acos(dd(-1)); ll gcd(ll a, ll b) { a = abs(a); b = abs(b); while (b) { a %= b; swap(a, b); } return a; } ll binpow(ll a, ll n) { ll binpow_mod = 1e9 + 7; ll res = 1; while (n) { if (n & 1) res *= a; a *= a; n >>= 1; a %= binpow_mod; res %= binpow_mod; } return res; } struct Point { ll x, y; bool z; void scan() { cin >> x >> y; } Point(ll X = 0, ll Y = 0, bool Z = 0) { x = X; y = Y; z = Z; } ll operator*(Point &a) { return x * a.y - y * a.x; } bool operator==(Point &a) { return x == a.x && y == a.y; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int q; cin >> q; while (q--) { int n, x; cin >> n >> x; cout << x * 2 << '\n'; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; using ll = long long; using ld = long double; using itn = int; using dd = double; mt19937 gen(41); const dd eps = 1e-7; const ll MAXN = 2097152; const ll INF = 1e9; const dd pi = acos(dd(-1)); ll gcd(ll a, ll b) { a = abs(a); b = abs(b); while (b) { a %= b; swap(a, b); } return a; } ll binpow(ll a, ll n) { ll binpow_mod = 1e9 + 7; ll res = 1; while (n) { if (n & 1) res *= a; a *= a; n >>= 1; a %= binpow_mod; res %= binpow_mod; } return res; } struct Point { ll x, y; bool z; void scan() { cin >> x >> y; } Point(ll X = 0, ll Y = 0, bool Z = 0) { x = X; y = Y; z = Z; } ll operator*(Point &a) { return x * a.y - y * a.x; } bool operator==(Point &a) { return x == a.x && y == a.y; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int q; cin >> q; while (q--) { int n, x; cin >> n >> x; cout << x * 2 << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T; cin >> T; while (T--) { int x, n; cin >> n >> x; cout << x * 2 << endl; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T; cin >> T; while (T--) { int x, n; cin >> n >> x; cout << x * 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, x; cin >> n >> x; cout << 2 * x << endl; } }
### Prompt Please create a solution in CPP to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, x; cin >> n >> x; cout << 2 * x << endl; } } ```
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); int i; int a, b; for (i = 0; i < t; i++) { scanf("%d %d", &a, &b); printf("%d\n", b * 2); } }
### Prompt Generate a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> int main() { int t; scanf("%d", &t); int i; int a, b; for (i = 0; i < t; i++) { scanf("%d %d", &a, &b); printf("%d\n", b * 2); } } ```
#include <bits/stdc++.h> using namespace std; int n, x, t; int main() { cin >> t; for (int ck = 1; ck <= t; ck++) { cin >> n >> x; cout << x * 2 << "\n"; } }
### Prompt Please provide a CPP coded solution to the problem described below: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, x, t; int main() { cin >> t; for (int ck = 1; ck <= t; ck++) { cin >> n >> x; cout << x * 2 << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int* results = new int[N]; for (int i = 0; i < N; i++) { int num, pos; cin >> num >> pos; results[i] = pos * 2; } for (int i = 0; i < N; i++) cout << results[i] << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int* results = new int[N]; for (int i = 0; i < N; i++) { int num, pos; cin >> num >> pos; results[i] = pos * 2; } for (int i = 0; i < N; i++) cout << results[i] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << '\n'; } };
### Prompt Create a solution in CPP for the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << '\n'; } }; ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; vector<long long> flash(long long n) { vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; return a; } void Shazam() { long long a, b; cin >> a >> b; cout << 2 * b << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; while (t--) { Shazam(); } return 0; }
### Prompt Generate a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; vector<long long> flash(long long n) { vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; return a; } void Shazam() { long long a, b; cin >> a >> b; cout << 2 * b << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; while (t--) { Shazam(); } return 0; } ```
#include <bits/stdc++.h> int main() { int x, n, k; scanf("%d", &x); while (x--) { scanf("%d %d", &n, &k); printf("%d\n", k * 2); } }
### Prompt In CPP, your task is to solve the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> int main() { int x, n, k; scanf("%d", &x); while (x--) { scanf("%d %d", &n, &k); printf("%d\n", k * 2); } } ```
#include <bits/stdc++.h> int main() { int T; long long int n, x, c, f; scanf("%d\n", &T); for (f = 0; f < T; f++) { scanf("%lld %lld", &n, &x); c = 2 * x; printf("%lld\n", c); } }
### Prompt Generate a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> int main() { int T; long long int n, x, c, f; scanf("%d\n", &T); for (f = 0; f < T; f++) { scanf("%lld %lld", &n, &x); c = 2 * x; printf("%lld\n", c); } } ```
#include <bits/stdc++.h> using namespace std; void solve() { long long n, x; cin >> n >> x; cout << 2 * x << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { long long n, x; cin >> n >> x; cout << 2 * x << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int t, m, n; int main() { cin >> t; while (t--) { cin >> n >> m; cout << m * 2 << endl; ; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int t, m, n; int main() { cin >> t; while (t--) { cin >> n >> m; cout << m * 2 << endl; ; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int s, q; cin >> s >> q; cout << 2 * q << endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int s, q; cin >> s >> q; cout << 2 * q << endl; } } ```
#include <bits/stdc++.h> using namespace std; int n, x; void solve() { cin >> n >> x; cout << x * 2 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) { solve(); } }
### Prompt Construct a CPP code solution to the problem outlined: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, x; void solve() { cin >> n >> x; cout << x * 2 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << x * 2 << "\n"; } }
### Prompt Please create a solution in cpp to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << x * 2 << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1e5 + 5; int main() { int t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << endl; } }
### Prompt Please formulate a Cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1e5 + 5; int main() { int t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int N = 1e6 + 5; const int inf = 1 << 30; const long long llf = 9e18; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } struct node { int x, y; }; bool cmp(int x, int y) { return x < y; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; cout << 2 * m << endl; } }
### Prompt Construct a Cpp code solution to the problem outlined: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int N = 1e6 + 5; const int inf = 1 << 30; const long long llf = 9e18; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } struct node { int x, y; }; bool cmp(int x, int y) { return x < y; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; cout << 2 * m << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long int INF = 1000000000000000000; int main() { int t = 1; cin >> t; while (t--) { long long int i, y, h, j, k, r = 0, l = 0, n, m, x = 0, o = 0, cnt1 = INT_MAX, cnt2 = 0, cnt3 = INT_MIN, cnt4 = 0, min1 = INT_MIN; cin >> n >> m; cout << 2 * m << endl; } }
### Prompt Your challenge is to write a CPP solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long int INF = 1000000000000000000; int main() { int t = 1; cin >> t; while (t--) { long long int i, y, h, j, k, r = 0, l = 0, n, m, x = 0, o = 0, cnt1 = INT_MAX, cnt2 = 0, cnt3 = INT_MIN, cnt4 = 0, min1 = INT_MIN; cin >> n >> m; cout << 2 * m << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, i, n, x; cin >> t; int a[t]; for (i = 0; i < t; i++) { cin >> n >> x; cout << 2 * x << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, i, n, x; cin >> t; int a[t]; for (i = 0; i < t; i++) { cin >> n >> x; cout << 2 * x << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n, x; cin >> n >> x; cout << x * 2 << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n, x; cin >> n >> x; cout << x * 2 << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; for (int i = 0; i < t; i++) { int n, x; cin >> n >> x; cout << 2 * x << '\n'; } }
### Prompt Create a solution in CPP for the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; for (int i = 0; i < t; i++) { int n, x; cin >> n >> x; cout << 2 * x << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << endl; } }
### Prompt Construct a cpp code solution to the problem outlined: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, x; cin >> n >> x; cout << 2 * x << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6; const long long mod = 1e9 + 7; const int oo = 0x7fffffff; const int sup = 0x80000000; template <typename it> void db(it *begin, it *end) { while (begin != end) cout << (*begin++) << " "; puts(""); } template <typename it> string to_str(it n) { string s = ""; while (n) s += n % 10 + '0', n /= 10; reverse(s.begin(), s.end()); return s; } template <typename it> int o(it a) { cout << a << endl; return 0; } long long mul(long long a, long long b, long long c) { long long ans = 0; for (; b; b >>= 1, a = (a + a) % c) if (b & 1) ans = (ans + a) % c; return ans; } long long ksm(long long a, long long b, long long c) { long long ans = 1; for (; b; b >>= 1, a = mul(a, a, c)) if (b & 1) ans = mul(ans, a, c); return ans; } int main() { int n, x, t; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> x; cout << x * 2 << endl; } }
### Prompt Your challenge is to write a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6; const long long mod = 1e9 + 7; const int oo = 0x7fffffff; const int sup = 0x80000000; template <typename it> void db(it *begin, it *end) { while (begin != end) cout << (*begin++) << " "; puts(""); } template <typename it> string to_str(it n) { string s = ""; while (n) s += n % 10 + '0', n /= 10; reverse(s.begin(), s.end()); return s; } template <typename it> int o(it a) { cout << a << endl; return 0; } long long mul(long long a, long long b, long long c) { long long ans = 0; for (; b; b >>= 1, a = (a + a) % c) if (b & 1) ans = (ans + a) % c; return ans; } long long ksm(long long a, long long b, long long c) { long long ans = 1; for (; b; b >>= 1, a = mul(a, a, c)) if (b & 1) ans = mul(ans, a, c); return ans; } int main() { int n, x, t; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> x; cout << x * 2 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0); long long t; cin >> t; for (int i = 1; i <= t; i++) { long long n, x; cin >> n >> x; cout << x * 2 << endl; } }
### Prompt Create a solution in Cpp for the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0); long long t; cin >> t; for (int i = 1; i <= t; i++) { long long n, x; cin >> n >> x; cout << x * 2 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; long long int N, x; while (T--) { cin >> N >> x; cout << (2 * x) << "\n"; } return 0; }
### Prompt Generate a cpp solution to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; long long int N, x; while (T--) { cin >> N >> x; cout << (2 * x) << "\n"; } return 0; } ```
#include <bits/stdc++.h> int main() { int T; int n, x; scanf("%d", &T); while (T--) { scanf("%d %d", &n, &x); printf("%d\n", std::min(x * 2, (n / 2) * 2)); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> int main() { int T; int n, x; scanf("%d", &T); while (T--) { scanf("%d %d", &n, &x); printf("%d\n", std::min(x * 2, (n / 2) * 2)); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t, n, x; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> x; cout << x * 2 << endl; } }
### Prompt In cpp, your task is to solve the following problem: You have a list of numbers from 1 to n written from left to right on the blackboard. You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit). <image> When there are less than i numbers remaining, you stop your algorithm. Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped? Input The first line contains one integer T (1 ≀ T ≀ 100) β€” the number of queries. The next T lines contain queries β€” one per line. All queries are independent. Each line contains two space-separated integers n and x (1 ≀ x < n ≀ 10^{9}) β€” the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers. Output Print T integers (one per query) β€” the values of the x-th number after performing the algorithm for the corresponding queries. Example Input 3 3 1 4 2 69 6 Output 2 4 12 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t, n, x; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> x; cout << x * 2 << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int Mxxx = 1e5; inline char gc() { static char buf[Mxxx], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Mxxx, stdin), p1 == p2) ? EOF : *p1++; } inline char pc(char ch, bool fl) { static char buf[Mxxx], *p1 = buf, *p2 = buf + Mxxx; return (fl || ((*p1++ = ch) && p1 == p2)) && (fwrite(buf, 1, p1 - buf, stdout), p1 = buf), 0; } inline int read() { char ch = gc(); int gflag = 0, gans = 0; while (ch < '0' || ch > '9') { gflag |= (ch == '-'); ch = gc(); } while ('0' <= ch && ch <= '9') { gans = (gans << 1) + (gans << 3) + (ch ^ 48); ch = gc(); } return gflag ? -gans : gans; } template <typename T> inline char read(T &gans) { char ch = gc(); int gflag = 0; gans = 0; while (ch < '0' || ch > '9') { gflag |= (ch == '-'); ch = gc(); } while ('0' <= ch && ch <= '9') { gans = (gans << 1) + (gans << 3) + (ch ^ 48); ch = gc(); } gans = gflag ? -gans : gans; return ch; } template <typename T> inline void write(T x) { if (x > 9) { write(x / 10); } pc(x % 10 ^ 48, false); } template <typename T> inline void writenum(T x, char ch) { if (x < 0) { pc('-', false); x = -x; } write(x); pc(ch, false); } inline void writechar(char x, char ch) { pc(x, false); pc(ch, false); } template <typename T> inline T Min(T x, T y) { return x < y ? x : y; } template <typename T> inline T Max(T x, T y) { return x > y ? x : y; } template <typename T> inline T Abs(T x) { return x > 0 ? x : -x; } template <typename T> inline void ckmx(T &x, T y) { x = Max(x, y); } template <typename T> inline void ckmn(T &x, T y) { x = Min(x, y); } const int M = 128; const int Mod = 1e9 + 7; inline int Ad(int x, int y = 0) { return (x += y) >= Mod ? x - Mod : x; } inline void Add(int &x, int y) { x = Ad(x, y); } inline void Mul(int &x, int y) { x = (((long long)(x) * (y)) % Mod); } inline int Ksm(int x, int y) { int nm = 1; for (; y; y >>= 1) { if (y & 1) { Mul(nm, x); } Mul(x, x); } return nm; } inline int Inv(int x) { return Ksm(x, Mod - 2); } const int Inv100 = Inv(100); const int Mx = 7; const int MX = 1 << 16; int n, tot, p[Mx + 5][Mx + 5], prb[Mx + 5][M + 5], trs[MX + 5][M + 5], F[Mx + 5][MX + 5]; bitset<M> al; unordered_map<bitset<M>, int> mp; inline int Pre_DFS(const bitset<M> a) { if (mp.find(a) != mp.end()) { return mp[a]; } int i, j, t = mp[a] = ++tot; bitset<M> b, pre[Mx + 5]; for (i = 1; i <= n; i++) { pre[i].reset(); for (j = (1 << n) - 1; ~j; j--) { if ((j >> (i - 1)) & 1 && a[j ^ (1 << (i - 1))]) { pre[i][j] = 1; } } } for (i = (1 << n) - 1; ~i; i--) { b = a; for (j = 1; j <= n; j++) { if ((i >> (j - 1)) & 1) { b |= pre[j]; } } trs[t][i] = Pre_DFS(b); } return t; } signed main() { int i, j, k; n = read(); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { p[i][j] = (((long long)(Inv100) * (read())) % Mod); } } for (i = (1 << n) - 1; ~i; i--) { al[i] = 1; } F[0][Pre_DFS(1)] = 1; for (i = 1; i <= n; i++) { for (j = (1 << n) - 1; ~j; j--) { for (k = prb[i][j] = 1; k <= n; k++) { Mul(prb[i][j], (j >> (k - 1)) & 1 ? p[i][k] : Ad(1 - p[i][k], Mod)); } } } for (i = 1; i <= n; i++) { for (k = 1; k <= tot; k++) { if (!F[i - 1][k]) { continue; } for (j = (1 << n) - 1; ~j; j--) { Add(F[i][trs[k][j]], (((long long)(F[i - 1][k]) * (prb[i][j])) % Mod)); } } } writenum(F[n][mp[al]], 10); return pc('!', true); }
### Prompt Please create a solution in Cpp to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int Mxxx = 1e5; inline char gc() { static char buf[Mxxx], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Mxxx, stdin), p1 == p2) ? EOF : *p1++; } inline char pc(char ch, bool fl) { static char buf[Mxxx], *p1 = buf, *p2 = buf + Mxxx; return (fl || ((*p1++ = ch) && p1 == p2)) && (fwrite(buf, 1, p1 - buf, stdout), p1 = buf), 0; } inline int read() { char ch = gc(); int gflag = 0, gans = 0; while (ch < '0' || ch > '9') { gflag |= (ch == '-'); ch = gc(); } while ('0' <= ch && ch <= '9') { gans = (gans << 1) + (gans << 3) + (ch ^ 48); ch = gc(); } return gflag ? -gans : gans; } template <typename T> inline char read(T &gans) { char ch = gc(); int gflag = 0; gans = 0; while (ch < '0' || ch > '9') { gflag |= (ch == '-'); ch = gc(); } while ('0' <= ch && ch <= '9') { gans = (gans << 1) + (gans << 3) + (ch ^ 48); ch = gc(); } gans = gflag ? -gans : gans; return ch; } template <typename T> inline void write(T x) { if (x > 9) { write(x / 10); } pc(x % 10 ^ 48, false); } template <typename T> inline void writenum(T x, char ch) { if (x < 0) { pc('-', false); x = -x; } write(x); pc(ch, false); } inline void writechar(char x, char ch) { pc(x, false); pc(ch, false); } template <typename T> inline T Min(T x, T y) { return x < y ? x : y; } template <typename T> inline T Max(T x, T y) { return x > y ? x : y; } template <typename T> inline T Abs(T x) { return x > 0 ? x : -x; } template <typename T> inline void ckmx(T &x, T y) { x = Max(x, y); } template <typename T> inline void ckmn(T &x, T y) { x = Min(x, y); } const int M = 128; const int Mod = 1e9 + 7; inline int Ad(int x, int y = 0) { return (x += y) >= Mod ? x - Mod : x; } inline void Add(int &x, int y) { x = Ad(x, y); } inline void Mul(int &x, int y) { x = (((long long)(x) * (y)) % Mod); } inline int Ksm(int x, int y) { int nm = 1; for (; y; y >>= 1) { if (y & 1) { Mul(nm, x); } Mul(x, x); } return nm; } inline int Inv(int x) { return Ksm(x, Mod - 2); } const int Inv100 = Inv(100); const int Mx = 7; const int MX = 1 << 16; int n, tot, p[Mx + 5][Mx + 5], prb[Mx + 5][M + 5], trs[MX + 5][M + 5], F[Mx + 5][MX + 5]; bitset<M> al; unordered_map<bitset<M>, int> mp; inline int Pre_DFS(const bitset<M> a) { if (mp.find(a) != mp.end()) { return mp[a]; } int i, j, t = mp[a] = ++tot; bitset<M> b, pre[Mx + 5]; for (i = 1; i <= n; i++) { pre[i].reset(); for (j = (1 << n) - 1; ~j; j--) { if ((j >> (i - 1)) & 1 && a[j ^ (1 << (i - 1))]) { pre[i][j] = 1; } } } for (i = (1 << n) - 1; ~i; i--) { b = a; for (j = 1; j <= n; j++) { if ((i >> (j - 1)) & 1) { b |= pre[j]; } } trs[t][i] = Pre_DFS(b); } return t; } signed main() { int i, j, k; n = read(); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { p[i][j] = (((long long)(Inv100) * (read())) % Mod); } } for (i = (1 << n) - 1; ~i; i--) { al[i] = 1; } F[0][Pre_DFS(1)] = 1; for (i = 1; i <= n; i++) { for (j = (1 << n) - 1; ~j; j--) { for (k = prb[i][j] = 1; k <= n; k++) { Mul(prb[i][j], (j >> (k - 1)) & 1 ? p[i][k] : Ad(1 - p[i][k], Mod)); } } } for (i = 1; i <= n; i++) { for (k = 1; k <= tot; k++) { if (!F[i - 1][k]) { continue; } for (j = (1 << n) - 1; ~j; j--) { Add(F[i][trs[k][j]], (((long long)(F[i - 1][k]) * (prb[i][j])) % Mod)); } } } writenum(F[n][mp[al]], 10); return pc('!', true); } ```
#include <bits/stdc++.h> using namespace std; const int N = 8, M = 20, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { return ((a + b) % MOD + MOD) % MOD; } void _sum(int &a, int b) { a = sum(a, b); } int mul(int a, int b) { return 1LL * a * b % MOD; } void _mul(int &a, int b) { a = mul(a, b); } int power(int a, int b) { int res = 1; while (b) { if (b & 1) _mul(res, a); _mul(a, a); b >>= 1; } return res; } int inv(int x) { return power(x, MOD - 2); } int n, f[1 << N], p[N][N], pro[N][1 << N]; unordered_map<long long, int> dp[N]; vector<int> vec[N]; void input() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; _mul(p[i][j], inv(100)); } for (int mask = 0; mask < (1 << n); mask++) { pro[i][mask] = 1; for (int j = 0; j < n; j++) if (mask & (1 << j)) _mul(pro[i][mask], p[i][j]); else _mul(pro[i][mask], sum(1, -p[i][j])); } } } void pre_pro() { for (int i = 0; i < (1 << n); i++) { f[i] = vec[__builtin_popcount(i)].size(); vec[__builtin_popcount(i)].push_back(i); } } void act(int i, long long mask, int nei) { long long nxt = 0; vector<int> a, b; for (int j = 0; j < vec[i].size(); j++) if (mask & (1LL << j)) a.push_back(vec[i][j]); for (int j = 0; j < n; j++) if (nei & (1 << j)) b.push_back(j); for (int x : a) for (int y : b) if ((x & (1 << y)) == false) nxt |= (1LL << f[x | (1 << y)]); _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei])); } void solve() { dp[0][1] = 1; for (int i = 0; i < n; i++) for (auto p : dp[i]) for (int nei = 0; nei < (1 << n); nei++) if (pro[i + 1][nei]) act(i, p.first, nei); } int main() { ios::sync_with_stdio(false), cin.tie(0); input(); pre_pro(); solve(); cout << dp[n][1]; return 0; }
### Prompt In CPP, your task is to solve the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 8, M = 20, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { return ((a + b) % MOD + MOD) % MOD; } void _sum(int &a, int b) { a = sum(a, b); } int mul(int a, int b) { return 1LL * a * b % MOD; } void _mul(int &a, int b) { a = mul(a, b); } int power(int a, int b) { int res = 1; while (b) { if (b & 1) _mul(res, a); _mul(a, a); b >>= 1; } return res; } int inv(int x) { return power(x, MOD - 2); } int n, f[1 << N], p[N][N], pro[N][1 << N]; unordered_map<long long, int> dp[N]; vector<int> vec[N]; void input() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; _mul(p[i][j], inv(100)); } for (int mask = 0; mask < (1 << n); mask++) { pro[i][mask] = 1; for (int j = 0; j < n; j++) if (mask & (1 << j)) _mul(pro[i][mask], p[i][j]); else _mul(pro[i][mask], sum(1, -p[i][j])); } } } void pre_pro() { for (int i = 0; i < (1 << n); i++) { f[i] = vec[__builtin_popcount(i)].size(); vec[__builtin_popcount(i)].push_back(i); } } void act(int i, long long mask, int nei) { long long nxt = 0; vector<int> a, b; for (int j = 0; j < vec[i].size(); j++) if (mask & (1LL << j)) a.push_back(vec[i][j]); for (int j = 0; j < n; j++) if (nei & (1 << j)) b.push_back(j); for (int x : a) for (int y : b) if ((x & (1 << y)) == false) nxt |= (1LL << f[x | (1 << y)]); _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei])); } void solve() { dp[0][1] = 1; for (int i = 0; i < n; i++) for (auto p : dp[i]) for (int nei = 0; nei < (1 << n); nei++) if (pro[i + 1][nei]) act(i, p.first, nei); } int main() { ios::sync_with_stdio(false), cin.tie(0); input(); pre_pro(); solve(); cout << dp[n][1]; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = (int)1.01e9; const long long infll = (long long)1.01e18; const long double eps = 1e-9; const long double pi = acos((long double)-1); mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x) { return mrand() % x; } void precalc() {} const int mod = (int)1e9 + 7; int mul(int a, int b) { return (long long)a * b % mod; } void add(int &a, int b) { a += b; if (a >= mod) { a -= mod; } } pair<int, int> gcd(int a, int b) { if (!b) { return make_pair(1, 0); } pair<int, int> p = gcd(b, a % b); return make_pair(p.second, p.first - (a / b) * p.second); } int inv(int x) { int res = gcd(x, mod).first % mod; if (res < 0) { res += mod; } return res; } const int maxn = 6; int n; int ps[maxn][maxn]; int qs[maxn][maxn]; bool read() { if (scanf("%d", &n) < 1) { return false; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &ps[i][j]); } } return true; } const int pown = (1 << maxn); int ids[pown]; vector<int> msks; vector<pair<int, int>> es; vector<int> a, b; int can[maxn][maxn]; void genMsk(int i, int k, int cur, bool inv, int &msk) { if (i >= k) { if (inv) { cur = (((1 << n) - 1) ^ cur); } assert(ids[cur] != -1); msk |= (1 << ids[cur]); return; } for (int j = 0; j < n; j++) { if (!can[i][j] || (cur & (1 << j))) { continue; } genMsk(i + 1, k, (cur | (1 << j)), inv, msk); } } void solve() { { int tomul = inv(100); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ps[i][j] = mul(ps[i][j], tomul); } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { qs[i][j] = 1; add(qs[i][j], mod - ps[i][j]); } } } int k = n / 2; { msks.clear(); for (int i = 0; i < (1 << n); i++) { ids[i] = -1; if (__builtin_popcount(i) == k) { ids[i] = ((int)(msks).size()); msks.push_back(i); } } } es.clear(); for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { es.push_back(make_pair(i, j)); } } a.clear(); a.resize(1 << ((int)(msks).size())); for (int i = 0; i < (1 << ((int)(es).size())); i++) { int p = 1, msk = 0; for (int j = 0; j < ((int)(es).size()); j++) { int v = es[j].first, u = es[j].second; can[v][u] = false; if (i & (1 << j)) { can[v][u] = true; p = mul(p, ps[v][u]); } else { p = mul(p, qs[v][u]); } } genMsk(0, k, 0, false, msk); add(a[msk], p); } es.clear(); for (int i = k; i < n; i++) { for (int j = 0; j < n; j++) { es.push_back(make_pair(i, j)); } } b.clear(); b.resize(1 << ((int)(msks).size())); for (int i = 0; i < (1 << ((int)(es).size())); i++) { int p = 1, msk = 0; for (int j = 0; j < ((int)(es).size()); j++) { int v = es[j].first, u = es[j].second; can[v][u] = false; if (i & (1 << j)) { can[v][u] = true; p = mul(p, ps[v][u]); } else { p = mul(p, qs[v][u]); } } genMsk(k, n, 0, true, msk); add(b[msk], p); } for (int j = 0; j < ((int)(msks).size()); j++) { for (int i = 0; i < (1 << ((int)(msks).size())); i++) { if (!(i & (1 << j))) { add(a[i], a[i | (1 << j)]); } } } for (int j = 0; j < ((int)(msks).size()); j++) { for (int i = 0; i < (1 << ((int)(msks).size())); i++) { if (!(i & (1 << j))) { add(b[i], b[i | (1 << j)]); } } } for (int i = 0; i < (1 << ((int)(msks).size())); i++) { a[i] = mul(a[i], b[i]); } for (int j = 0; j < ((int)(msks).size()); j++) { for (int i = 0; i < (1 << ((int)(msks).size())); i++) { if (!(i & (1 << j))) { add(a[i], mod - a[i | (1 << j)]); } } } int res = 0; for (int i = 1; i < (1 << ((int)(msks).size())); i++) { add(res, a[i]); } printf("%d\n", res); } int main() { precalc(); while (read()) { solve(); } return 0; }
### Prompt Create a solution in cpp for the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = (int)1.01e9; const long long infll = (long long)1.01e18; const long double eps = 1e-9; const long double pi = acos((long double)-1); mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x) { return mrand() % x; } void precalc() {} const int mod = (int)1e9 + 7; int mul(int a, int b) { return (long long)a * b % mod; } void add(int &a, int b) { a += b; if (a >= mod) { a -= mod; } } pair<int, int> gcd(int a, int b) { if (!b) { return make_pair(1, 0); } pair<int, int> p = gcd(b, a % b); return make_pair(p.second, p.first - (a / b) * p.second); } int inv(int x) { int res = gcd(x, mod).first % mod; if (res < 0) { res += mod; } return res; } const int maxn = 6; int n; int ps[maxn][maxn]; int qs[maxn][maxn]; bool read() { if (scanf("%d", &n) < 1) { return false; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &ps[i][j]); } } return true; } const int pown = (1 << maxn); int ids[pown]; vector<int> msks; vector<pair<int, int>> es; vector<int> a, b; int can[maxn][maxn]; void genMsk(int i, int k, int cur, bool inv, int &msk) { if (i >= k) { if (inv) { cur = (((1 << n) - 1) ^ cur); } assert(ids[cur] != -1); msk |= (1 << ids[cur]); return; } for (int j = 0; j < n; j++) { if (!can[i][j] || (cur & (1 << j))) { continue; } genMsk(i + 1, k, (cur | (1 << j)), inv, msk); } } void solve() { { int tomul = inv(100); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ps[i][j] = mul(ps[i][j], tomul); } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { qs[i][j] = 1; add(qs[i][j], mod - ps[i][j]); } } } int k = n / 2; { msks.clear(); for (int i = 0; i < (1 << n); i++) { ids[i] = -1; if (__builtin_popcount(i) == k) { ids[i] = ((int)(msks).size()); msks.push_back(i); } } } es.clear(); for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { es.push_back(make_pair(i, j)); } } a.clear(); a.resize(1 << ((int)(msks).size())); for (int i = 0; i < (1 << ((int)(es).size())); i++) { int p = 1, msk = 0; for (int j = 0; j < ((int)(es).size()); j++) { int v = es[j].first, u = es[j].second; can[v][u] = false; if (i & (1 << j)) { can[v][u] = true; p = mul(p, ps[v][u]); } else { p = mul(p, qs[v][u]); } } genMsk(0, k, 0, false, msk); add(a[msk], p); } es.clear(); for (int i = k; i < n; i++) { for (int j = 0; j < n; j++) { es.push_back(make_pair(i, j)); } } b.clear(); b.resize(1 << ((int)(msks).size())); for (int i = 0; i < (1 << ((int)(es).size())); i++) { int p = 1, msk = 0; for (int j = 0; j < ((int)(es).size()); j++) { int v = es[j].first, u = es[j].second; can[v][u] = false; if (i & (1 << j)) { can[v][u] = true; p = mul(p, ps[v][u]); } else { p = mul(p, qs[v][u]); } } genMsk(k, n, 0, true, msk); add(b[msk], p); } for (int j = 0; j < ((int)(msks).size()); j++) { for (int i = 0; i < (1 << ((int)(msks).size())); i++) { if (!(i & (1 << j))) { add(a[i], a[i | (1 << j)]); } } } for (int j = 0; j < ((int)(msks).size()); j++) { for (int i = 0; i < (1 << ((int)(msks).size())); i++) { if (!(i & (1 << j))) { add(b[i], b[i | (1 << j)]); } } } for (int i = 0; i < (1 << ((int)(msks).size())); i++) { a[i] = mul(a[i], b[i]); } for (int j = 0; j < ((int)(msks).size()); j++) { for (int i = 0; i < (1 << ((int)(msks).size())); i++) { if (!(i & (1 << j))) { add(a[i], mod - a[i | (1 << j)]); } } } int res = 0; for (int i = 1; i < (1 << ((int)(msks).size())); i++) { add(res, a[i]); } printf("%d\n", res); } int main() { precalc(); while (read()) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inv = 570000004, mod = 1e9 + 7; unordered_map<bitset<128>, int> mp; bitset<128> E; int tot, p[8][8], trans[100005][128]; int n, S, fp[8][8], f[8][100005], P[100005]; int Dfs(bitset<128> x) { if (mp.find(x) != mp.end()) return mp[x]; int now = mp[x] = ++tot; bitset<128> A[8], tmp; for (int i = 0; i < n; ++i) for (int s = 0; s <= S; ++s) A[i][s | (1 << i)] = A[i][s | (1 << i)] | x[s]; for (int s = 0, i; s <= S; ++s) { for (tmp = x, i = 0; i < n; ++i) if (s >> i & 1) tmp |= A[i]; trans[now][s] = Dfs(tmp); } return now; } void Add(int &x, int y) { (x += y) >= mod ? x -= mod : x; } int main() { cin >> n, S = (1 << n) - 1, E[0] = 1, Dfs(E); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> p[i][j], p[i][j] = 1ll * p[i][j] * inv % mod, fp[i][j] = mod + 1 - p[i][j]; f[0][1] = 1; for (int i = 0; i < n; ++i) { for (int s = 0; s <= S; ++s) { P[s] = 1; for (int j = 0; j < n; ++j) if (s >> j & 1) P[s] = 1ll * P[s] * p[j][i] % mod; else P[s] = 1ll * P[s] * fp[j][i] % mod; } for (int now = 1; now <= tot; ++now) if (f[i][now]) for (int s = 0; s <= S; ++s) Add(f[i + 1][trans[now][s]], 1ll * f[i][now] * P[s] % mod); } for (int i = 1; i <= S; ++i) E[i] = 1; cout << f[n][mp[E]]; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inv = 570000004, mod = 1e9 + 7; unordered_map<bitset<128>, int> mp; bitset<128> E; int tot, p[8][8], trans[100005][128]; int n, S, fp[8][8], f[8][100005], P[100005]; int Dfs(bitset<128> x) { if (mp.find(x) != mp.end()) return mp[x]; int now = mp[x] = ++tot; bitset<128> A[8], tmp; for (int i = 0; i < n; ++i) for (int s = 0; s <= S; ++s) A[i][s | (1 << i)] = A[i][s | (1 << i)] | x[s]; for (int s = 0, i; s <= S; ++s) { for (tmp = x, i = 0; i < n; ++i) if (s >> i & 1) tmp |= A[i]; trans[now][s] = Dfs(tmp); } return now; } void Add(int &x, int y) { (x += y) >= mod ? x -= mod : x; } int main() { cin >> n, S = (1 << n) - 1, E[0] = 1, Dfs(E); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> p[i][j], p[i][j] = 1ll * p[i][j] * inv % mod, fp[i][j] = mod + 1 - p[i][j]; f[0][1] = 1; for (int i = 0; i < n; ++i) { for (int s = 0; s <= S; ++s) { P[s] = 1; for (int j = 0; j < n; ++j) if (s >> j & 1) P[s] = 1ll * P[s] * p[j][i] % mod; else P[s] = 1ll * P[s] * fp[j][i] % mod; } for (int now = 1; now <= tot; ++now) if (f[i][now]) for (int s = 0; s <= S; ++s) Add(f[i + 1][trans[now][s]], 1ll * f[i][now] * P[s] % mod); } for (int i = 1; i <= S; ++i) E[i] = 1; cout << f[n][mp[E]]; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 10, M = 1000005, K = 205, P = 1000000007; using ll = long long; using Node = bitset<40>; int n, e[N], s[M], wf[K], q[N][K], f[N][N], ans[N][M]; Node op, wq[N][M]; unordered_map<Node, int> rt[N]; ll qpow(ll a, ll b) { ll ret = 1; while (b) { if (b & 1) ret = ret * a % P; a = a * a % P; b >>= 1; } return ret; } void find(int pos) { q[pos][0] = 0; int os = 0, of = 0; for (int i = 0; i < (1 << n); i++) if (s[i] == pos) q[pos][++q[pos][0]] = i, wf[i] = ++os; for (int i = 1; i <= e[pos - 1]; i++) { if (!ans[pos - 1][i]) continue; for (int h = 1; h < (1 << n); h++) { ll opq = 1; for (int c = 0; c < n; c++) if ((1 << c) & h) opq = opq * f[pos][c + 1] % P; else opq = opq * (1 - f[pos][c + 1] + P) % P; Node c = wq[pos - 1][i], hc = op; bool tag = 0; for (int i = 1; i <= q[pos - 1][0]; i++) if (c[i]) { for (int t = 0; t < n; t++) if (((1 << t) & h) && (!(q[pos - 1][i] & (1 << t)))) { hc[wf[q[pos - 1][i] | (1 << t)]] = 1; tag = 1; } } if (!tag) continue; int p; if (!rt[pos][hc]) rt[pos][hc] = ++of, ++e[pos], wq[pos][e[pos]] = hc; p = rt[pos][hc]; ans[pos][p] = (ans[pos][p] + opq * ans[pos - 1][i]) % P; } } } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i <= (1 << n); i++) s[i] = __builtin_popcount(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { int tt; cin >> tt; f[i][j] = tt * qpow(100, P - 2) % P; } q[0][0] = 1; e[0] = 1; ans[0][1] = 1; wq[0][1][1] = 1; for (int i = 1; i <= n; i++) find(i); cout << ans[n][1] << endl; return 0; }
### Prompt Please formulate a CPP solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 10, M = 1000005, K = 205, P = 1000000007; using ll = long long; using Node = bitset<40>; int n, e[N], s[M], wf[K], q[N][K], f[N][N], ans[N][M]; Node op, wq[N][M]; unordered_map<Node, int> rt[N]; ll qpow(ll a, ll b) { ll ret = 1; while (b) { if (b & 1) ret = ret * a % P; a = a * a % P; b >>= 1; } return ret; } void find(int pos) { q[pos][0] = 0; int os = 0, of = 0; for (int i = 0; i < (1 << n); i++) if (s[i] == pos) q[pos][++q[pos][0]] = i, wf[i] = ++os; for (int i = 1; i <= e[pos - 1]; i++) { if (!ans[pos - 1][i]) continue; for (int h = 1; h < (1 << n); h++) { ll opq = 1; for (int c = 0; c < n; c++) if ((1 << c) & h) opq = opq * f[pos][c + 1] % P; else opq = opq * (1 - f[pos][c + 1] + P) % P; Node c = wq[pos - 1][i], hc = op; bool tag = 0; for (int i = 1; i <= q[pos - 1][0]; i++) if (c[i]) { for (int t = 0; t < n; t++) if (((1 << t) & h) && (!(q[pos - 1][i] & (1 << t)))) { hc[wf[q[pos - 1][i] | (1 << t)]] = 1; tag = 1; } } if (!tag) continue; int p; if (!rt[pos][hc]) rt[pos][hc] = ++of, ++e[pos], wq[pos][e[pos]] = hc; p = rt[pos][hc]; ans[pos][p] = (ans[pos][p] + opq * ans[pos - 1][i]) % P; } } } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i <= (1 << n); i++) s[i] = __builtin_popcount(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { int tt; cin >> tt; f[i][j] = tt * qpow(100, P - 2) % P; } q[0][0] = 1; e[0] = 1; ans[0][1] = 1; wq[0][1][1] = 1; for (int i = 1; i <= n; i++) find(i); cout << ans[n][1] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; unordered_map<bitset<130>, int> id; int trans[70005][130], p[10][10], tot, f[10][70005], n; int Power(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; y >>= 1, x = 1ll * x * x % mod; } return r; } int dfs(bitset<130> cur) { if (id.count(cur)) return id[cur]; int w = ++tot; bitset<130> t[10], tmp; id[cur] = w; for (int i = 0; i < n; i++) for (int j = 0; j < (1 << n); j++) if (cur[j]) t[i][j | (1 << i)] = 1; for (int i = 0; i < (1 << n); i++) { tmp = cur; for (int j = 0; j < n; j++) if (i & (1 << j)) tmp |= t[j]; trans[w][i] = dfs(tmp); } return w; } int main() { cin >> n; for (int i = 0, inv100 = Power(100, mod - 2); i < n; i++) for (int j = 0; j < n; j++) cin >> p[i][j], p[i][j] = 1ll * inv100 * p[i][j] % mod; bitset<130> tmp; tmp[0] = 1, f[0][dfs(tmp)] = 1; for (int i = 0; i < n; i++) { for (int j = 1; j <= tot; j++) { for (int k = 0; k < (1 << n); k++) { int pro = 1; for (int w = 0; w < n; w++) if (k & (1 << w)) pro = 1ll * pro * p[w][i] % mod; else pro = 1ll * pro * (1 - p[w][i] + mod) % mod; f[i + 1][trans[j][k]] = (f[i + 1][trans[j][k]] + 1ll * pro * f[i][j]) % mod; } } } for (int i = 0; i < (1 << n); i++) tmp[i] = 1; cout << f[n][id[tmp]]; }
### Prompt In CPP, your task is to solve the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; unordered_map<bitset<130>, int> id; int trans[70005][130], p[10][10], tot, f[10][70005], n; int Power(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; y >>= 1, x = 1ll * x * x % mod; } return r; } int dfs(bitset<130> cur) { if (id.count(cur)) return id[cur]; int w = ++tot; bitset<130> t[10], tmp; id[cur] = w; for (int i = 0; i < n; i++) for (int j = 0; j < (1 << n); j++) if (cur[j]) t[i][j | (1 << i)] = 1; for (int i = 0; i < (1 << n); i++) { tmp = cur; for (int j = 0; j < n; j++) if (i & (1 << j)) tmp |= t[j]; trans[w][i] = dfs(tmp); } return w; } int main() { cin >> n; for (int i = 0, inv100 = Power(100, mod - 2); i < n; i++) for (int j = 0; j < n; j++) cin >> p[i][j], p[i][j] = 1ll * inv100 * p[i][j] % mod; bitset<130> tmp; tmp[0] = 1, f[0][dfs(tmp)] = 1; for (int i = 0; i < n; i++) { for (int j = 1; j <= tot; j++) { for (int k = 0; k < (1 << n); k++) { int pro = 1; for (int w = 0; w < n; w++) if (k & (1 << w)) pro = 1ll * pro * p[w][i] % mod; else pro = 1ll * pro * (1 - p[w][i] + mod) % mod; f[i + 1][trans[j][k]] = (f[i + 1][trans[j][k]] + 1ll * pro * f[i][j]) % mod; } } } for (int i = 0; i < (1 << n); i++) tmp[i] = 1; cout << f[n][id[tmp]]; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, inv = 570000004; int read() { int a = 0; bool opt = 0; char c = getchar(); while (c < '0' || c > '9') opt |= c == '-', c = getchar(); while (c >= '0' && c <= '9') a = (a << 3) + (a << 1) + (c ^ 48), c = getchar(); return opt ? -a : a; } int n, p1[10][10], p2[10][10], tot, dp[10][65000], p[128], trans[65000][128]; int Mod(int x) { return x < mod ? x : x - mod; } void Add(int& x, int y) { x = Mod(x + y); } bitset<128> I; unordered_map<bitset<128>, int> a; int dfs(bitset<128> I) { if (a.find(I) != a.end()) return a[I]; a[I] = ++tot; bitset<128> A[10], tmp; for (int i = 0; i < n; ++i) { for (int j = 0; j < (1 << n); ++j) { A[i][j | (1 << i)] = A[i][j | 1 << i] | I[j]; } } for (int i = 0; i < (1 << n); ++i) { tmp = I; for (int j = 0; j < n; ++j) { if ((i >> j) & 1) tmp |= A[j]; } trans[a[I]][i] = dfs(tmp); } return a[I]; } int main() { n = read(); I[0] = 1; dfs(I); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { p1[i][j] = 1ll * read() * inv % mod; p2[i][j] = Mod(1 - p1[i][j] + mod); } } dp[0][1] = 1; for (int i = 0; i < n; ++i) { for (int k = 0; k < (1 << n); ++k) { p[k] = 1; for (int j = 1; j <= n; ++j) { if ((k >> j - 1) & 1) p[k] = 1ll * p[k] * p1[i + 1][j] % mod; else p[k] = 1ll * p[k] * p2[i + 1][j] % mod; } } for (int j = 1; j <= tot; ++j) { for (int k = 0; k < (1 << n); ++k) { int Next = trans[j][k]; Add(dp[i + 1][Next], 1ll * dp[i][j] * p[k] % mod); } } } for (int i = 0; i < (1 << n); ++i) I[i] = 1; printf("%d\n", dp[n][a[I]]); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, inv = 570000004; int read() { int a = 0; bool opt = 0; char c = getchar(); while (c < '0' || c > '9') opt |= c == '-', c = getchar(); while (c >= '0' && c <= '9') a = (a << 3) + (a << 1) + (c ^ 48), c = getchar(); return opt ? -a : a; } int n, p1[10][10], p2[10][10], tot, dp[10][65000], p[128], trans[65000][128]; int Mod(int x) { return x < mod ? x : x - mod; } void Add(int& x, int y) { x = Mod(x + y); } bitset<128> I; unordered_map<bitset<128>, int> a; int dfs(bitset<128> I) { if (a.find(I) != a.end()) return a[I]; a[I] = ++tot; bitset<128> A[10], tmp; for (int i = 0; i < n; ++i) { for (int j = 0; j < (1 << n); ++j) { A[i][j | (1 << i)] = A[i][j | 1 << i] | I[j]; } } for (int i = 0; i < (1 << n); ++i) { tmp = I; for (int j = 0; j < n; ++j) { if ((i >> j) & 1) tmp |= A[j]; } trans[a[I]][i] = dfs(tmp); } return a[I]; } int main() { n = read(); I[0] = 1; dfs(I); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { p1[i][j] = 1ll * read() * inv % mod; p2[i][j] = Mod(1 - p1[i][j] + mod); } } dp[0][1] = 1; for (int i = 0; i < n; ++i) { for (int k = 0; k < (1 << n); ++k) { p[k] = 1; for (int j = 1; j <= n; ++j) { if ((k >> j - 1) & 1) p[k] = 1ll * p[k] * p1[i + 1][j] % mod; else p[k] = 1ll * p[k] * p2[i + 1][j] % mod; } } for (int j = 1; j <= tot; ++j) { for (int k = 0; k < (1 << n); ++k) { int Next = trans[j][k]; Add(dp[i + 1][Next], 1ll * dp[i][j] * p[k] % mod); } } } for (int i = 0; i < (1 << n); ++i) I[i] = 1; printf("%d\n", dp[n][a[I]]); return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize "-O3" using ll = long long; using ull = unsigned long long; using ld = long double; using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; int mul(int x, int y) { return (1ll * x * y) % MOD; } void add(int& x, int y) { x += y; if (x >= MOD) { x -= MOD; } } int pw(int x, int y) { if (y == 0) { return 1; } else if (y % 2 == 0) { return pw(mul(x, x), y / 2); } else { return mul(x, pw(x, y - 1)); } } int inv(int x) { return pw(x, MOD - 2); } int neg(int x) { return (MOD + 1 - x) % MOD; } const int MX = 10; int n; int p[MX][MX]; int pm[MX][1 << MX]; map<vector<int>, int> dp[MX]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; p[i][j] = mul(p[i][j], inv(100)); } } for (int i = 0; i < n; i++) { for (int j = 0; j < (1 << n); j++) { pm[i][j] = 1; for (int k = 0; k < n; k++) { if (j & (1 << k)) { pm[i][j] = mul(pm[i][j], p[i][k]); } else { pm[i][j] = mul(pm[i][j], neg(p[i][k])); } } } } vector<pair<vector<int>, int> > states; map<vector<int>, int> nextStates; vector<int> nmts; states.emplace_back(vector<int>({0}), 1); for (int pos = 0; pos < n; pos++) { nextStates.clear(); for (int mask = 0; mask < (1 << n); mask++) { for (auto e : states) { vector<int> mts = e.first; int p = e.second; nmts.clear(); for (int mt : mts) { for (int v = 0; v < n; v++) { if ((mask & (1 << v)) && !(mt & (1 << v))) { nmts.push_back(mt | (1 << v)); } } } sort(nmts.begin(), nmts.end()); nmts.resize(unique(nmts.begin(), nmts.end()) - nmts.begin()); if (!nmts.empty()) { add(nextStates[nmts], mul(p, pm[pos][mask])); } } } states.clear(); for (auto e : nextStates) { states.emplace_back(e); } } int ans = 0; for (auto e : states) { add(ans, e.second); } cout << ans << "\n"; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize "-O3" using ll = long long; using ull = unsigned long long; using ld = long double; using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; int mul(int x, int y) { return (1ll * x * y) % MOD; } void add(int& x, int y) { x += y; if (x >= MOD) { x -= MOD; } } int pw(int x, int y) { if (y == 0) { return 1; } else if (y % 2 == 0) { return pw(mul(x, x), y / 2); } else { return mul(x, pw(x, y - 1)); } } int inv(int x) { return pw(x, MOD - 2); } int neg(int x) { return (MOD + 1 - x) % MOD; } const int MX = 10; int n; int p[MX][MX]; int pm[MX][1 << MX]; map<vector<int>, int> dp[MX]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; p[i][j] = mul(p[i][j], inv(100)); } } for (int i = 0; i < n; i++) { for (int j = 0; j < (1 << n); j++) { pm[i][j] = 1; for (int k = 0; k < n; k++) { if (j & (1 << k)) { pm[i][j] = mul(pm[i][j], p[i][k]); } else { pm[i][j] = mul(pm[i][j], neg(p[i][k])); } } } } vector<pair<vector<int>, int> > states; map<vector<int>, int> nextStates; vector<int> nmts; states.emplace_back(vector<int>({0}), 1); for (int pos = 0; pos < n; pos++) { nextStates.clear(); for (int mask = 0; mask < (1 << n); mask++) { for (auto e : states) { vector<int> mts = e.first; int p = e.second; nmts.clear(); for (int mt : mts) { for (int v = 0; v < n; v++) { if ((mask & (1 << v)) && !(mt & (1 << v))) { nmts.push_back(mt | (1 << v)); } } } sort(nmts.begin(), nmts.end()); nmts.resize(unique(nmts.begin(), nmts.end()) - nmts.begin()); if (!nmts.empty()) { add(nextStates[nmts], mul(p, pm[pos][mask])); } } } states.clear(); for (auto e : nextStates) { states.emplace_back(e); } } int ans = 0; for (auto e : states) { add(ans, e.second); } cout << ans << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize "-O3" #pragma GCC optimize("Ofast") #pragma comment(linker, "/STACK:1000000000") const int mod = 1e9 + 7; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp1(vector<long long> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp1(vector<int> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp2(vector<vector<int>> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cerr << ou[i][j] << ' '; } cerr << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 301; int fact[maxn], ofact[maxn]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } const int rsto = bp(100, mod - 2); void recalc(map<vector<int>, int> &dyn, const vector<int> &probs, int t, vector<vector<vector<int>>> &trans) { vector<int> goodsz; map<vector<int>, int> newdyn; int n = probs.size(); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; if (cnt == t) goodsz.push_back(mask); } for (int mask = 0; mask < (1 << n); mask++) { int prb = 1; vector<int> added; for (int i = 0; i < n; i++) { if (ibit(mask, i)) { added.push_back(i); prb = mlt(prb, probs[i]); } else prb = mlt(prb, mod + 1 - probs[i]); } for (auto it = dyn.begin(); it != dyn.end(); it++) { vector<int> va = it->first; vector<int> used(trans[t + 1].size()); for (int i = 0; i < va.size(); i++) { if (va[i]) { for (auto pos : added) { if (pos >= (int)trans[t][i].size()) { cout << 333 << endl; exit(0); } int val = trans[t][i][pos]; if (val >= (int)used.size()) { cout << 333 << endl; exit(0); } if (val != -1) used[val] = 1; } } } if (!newdyn.count(used)) newdyn[used] = mlt(prb, it->second); else newdyn[used] = sum(newdyn[used], mlt(prb, it->second)); } } dyn = newdyn; } vector<vector<int>> enc; vector<vector<vector<int>>> trans; void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n; cin >> n; enc.resize(n + 1); trans.resize(n + 1); vector<vector<int>> v(n, vector<int>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> v[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) v[i][j] = mlt(v[i][j], rsto); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; enc[cnt].push_back(mask); } for (int i = 0; i <= n; i++) { trans[i].resize(enc[i].size()); for (int pos = 0; pos < enc[i].size(); pos++) { int j = enc[i][pos]; for (int h = 0; h < n; h++) { if (ibit(j, h)) trans[i][pos].push_back(-1); else { int val = j + (1 << h); for (int q = 0; q < enc[i + 1].size(); q++) { if (enc[i + 1][q] == val) { trans[i][pos].push_back(q); } } } } } } map<vector<int>, int> dyn; vector<int> q = {1}; dyn[q] = 1; for (int i = 0; i < n; i++) recalc(dyn, v[i], i, trans); vector<int> vt = {1}; cout << dyn[vt] << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tests = 1; for (int i = 0; i < tests; i++) { solve(); } }
### Prompt Develop a solution in CPP to the problem described below: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimize "-O3" #pragma GCC optimize("Ofast") #pragma comment(linker, "/STACK:1000000000") const int mod = 1e9 + 7; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp1(vector<long long> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp1(vector<int> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp2(vector<vector<int>> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cerr << ou[i][j] << ' '; } cerr << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 301; int fact[maxn], ofact[maxn]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } const int rsto = bp(100, mod - 2); void recalc(map<vector<int>, int> &dyn, const vector<int> &probs, int t, vector<vector<vector<int>>> &trans) { vector<int> goodsz; map<vector<int>, int> newdyn; int n = probs.size(); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; if (cnt == t) goodsz.push_back(mask); } for (int mask = 0; mask < (1 << n); mask++) { int prb = 1; vector<int> added; for (int i = 0; i < n; i++) { if (ibit(mask, i)) { added.push_back(i); prb = mlt(prb, probs[i]); } else prb = mlt(prb, mod + 1 - probs[i]); } for (auto it = dyn.begin(); it != dyn.end(); it++) { vector<int> va = it->first; vector<int> used(trans[t + 1].size()); for (int i = 0; i < va.size(); i++) { if (va[i]) { for (auto pos : added) { if (pos >= (int)trans[t][i].size()) { cout << 333 << endl; exit(0); } int val = trans[t][i][pos]; if (val >= (int)used.size()) { cout << 333 << endl; exit(0); } if (val != -1) used[val] = 1; } } } if (!newdyn.count(used)) newdyn[used] = mlt(prb, it->second); else newdyn[used] = sum(newdyn[used], mlt(prb, it->second)); } } dyn = newdyn; } vector<vector<int>> enc; vector<vector<vector<int>>> trans; void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n; cin >> n; enc.resize(n + 1); trans.resize(n + 1); vector<vector<int>> v(n, vector<int>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> v[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) v[i][j] = mlt(v[i][j], rsto); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; enc[cnt].push_back(mask); } for (int i = 0; i <= n; i++) { trans[i].resize(enc[i].size()); for (int pos = 0; pos < enc[i].size(); pos++) { int j = enc[i][pos]; for (int h = 0; h < n; h++) { if (ibit(j, h)) trans[i][pos].push_back(-1); else { int val = j + (1 << h); for (int q = 0; q < enc[i + 1].size(); q++) { if (enc[i + 1][q] == val) { trans[i][pos].push_back(q); } } } } } } map<vector<int>, int> dyn; vector<int> q = {1}; dyn[q] = 1; for (int i = 0; i < n; i++) recalc(dyn, v[i], i, trans); vector<int> vt = {1}; cout << dyn[vt] << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tests = 1; for (int i = 0; i < tests; i++) { solve(); } } ```
#include <bits/stdc++.h> #pragma GCC optimize(2, 3, "Ofast", "unroll-loops") using namespace std; namespace fast_io { char buf[1 << 12], *p1 = buf, *p2 = buf, sr[1 << 23], z[23], nc; int C = -1, Z = 0, Bi = 0, ny; inline char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 12, stdin), p1 == p2) ? EOF : *p1++; } inline long long read() { long long x = 0; ny = 1; while (nc = gc(), (nc < 48 || nc > 57) && nc != EOF) if (nc == 45) ny = -1; Bi = 1; if (nc < 0) return nc; x = nc - 48; while (nc = gc(), 47 < nc && nc < 58 && nc != EOF) x = (x << 3) + (x << 1) + (nc ^ 48), Bi++; return x * ny; } inline double gf() { int a = read(), y = ny, b = (nc != '.') ? 0 : read(); return (b ? a + (double)b / pow(10, Bi) * y : a); } inline int gs(char *s) { char c, *t = s; while (c = gc(), c <= 32) ; *s++ = c; while (c = gc(), c > 32) *s++ = c; return s - t; } inline void ot() { fwrite(sr, 1, C + 1, stdout); C = -1; } inline void flush() { if (C > 1 << 22) ot(); } template <typename T> inline void write(T x, char t) { int y = 0; if (x < 0) y = 1, x = -x; while (z[++Z] = x % 10 + 48, x /= 10) ; if (y) z[++Z] = '-'; while (sr[++C] = z[Z], --Z) ; sr[++C] = t; flush(); } inline void write(char *s) { int l = strlen(s); for (int i = 0; i < l; i++) sr[++C] = *s++; sr[++C] = '\n'; flush(); } } // namespace fast_io using namespace fast_io; const int N = 8, p = 1e9 + 7; int n, tot, ans, a[N][N], b[N][1 << N]; vector<pair<vector<int>, int> > v; map<vector<int>, int> mp; vector<int> st; inline void M(int &x) { x -= p; x += x >> 31 & p; } inline int qpow(int a, int b) { int c = 1; for (; b; b >>= 1, a = 1ll * a * a % p) if (b & 1) c = 1ll * a * c % p; return c; } int main() { n = read(), tot = 1 << n, v.push_back({vector<int>({0}), 1}); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = 1ll * read() * qpow(100, p - 2) % p; for (int i = 0; i < n; i++) for (int j = 0; j < tot; j++) { b[i][j] = 1; for (int k = 0; k < n; k++) b[i][j] = 1ll * b[i][j] * ((j & (1 << k)) ? a[i][k] : p + 1 - a[i][k]) % p; } for (int i = 0; i < n; i++) { mp.clear(); for (int s = 0; s < tot; s++) for (pair<vector<int>, int> e : v) { vector<int> nt = e.first; int o = e.second; st.clear(); for (int t : nt) for (int j = 0; j < n; j++) if (s & (1 << j) && !(t & (1 << j))) st.push_back(t | (1 << j)); sort(st.begin(), st.end()); st.resize(unique(st.begin(), st.end()) - st.begin()); if (st.size()) M(mp[st] += 1ll * o * b[i][s] % p); } v.clear(); for (pair<vector<int>, int> e : mp) v.push_back(e); } for (pair<vector<int>, int> e : v) M(ans += e.second); write(ans, '\n'); return ot(), 0; }
### Prompt Develop a solution in CPP to the problem described below: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize(2, 3, "Ofast", "unroll-loops") using namespace std; namespace fast_io { char buf[1 << 12], *p1 = buf, *p2 = buf, sr[1 << 23], z[23], nc; int C = -1, Z = 0, Bi = 0, ny; inline char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 12, stdin), p1 == p2) ? EOF : *p1++; } inline long long read() { long long x = 0; ny = 1; while (nc = gc(), (nc < 48 || nc > 57) && nc != EOF) if (nc == 45) ny = -1; Bi = 1; if (nc < 0) return nc; x = nc - 48; while (nc = gc(), 47 < nc && nc < 58 && nc != EOF) x = (x << 3) + (x << 1) + (nc ^ 48), Bi++; return x * ny; } inline double gf() { int a = read(), y = ny, b = (nc != '.') ? 0 : read(); return (b ? a + (double)b / pow(10, Bi) * y : a); } inline int gs(char *s) { char c, *t = s; while (c = gc(), c <= 32) ; *s++ = c; while (c = gc(), c > 32) *s++ = c; return s - t; } inline void ot() { fwrite(sr, 1, C + 1, stdout); C = -1; } inline void flush() { if (C > 1 << 22) ot(); } template <typename T> inline void write(T x, char t) { int y = 0; if (x < 0) y = 1, x = -x; while (z[++Z] = x % 10 + 48, x /= 10) ; if (y) z[++Z] = '-'; while (sr[++C] = z[Z], --Z) ; sr[++C] = t; flush(); } inline void write(char *s) { int l = strlen(s); for (int i = 0; i < l; i++) sr[++C] = *s++; sr[++C] = '\n'; flush(); } } // namespace fast_io using namespace fast_io; const int N = 8, p = 1e9 + 7; int n, tot, ans, a[N][N], b[N][1 << N]; vector<pair<vector<int>, int> > v; map<vector<int>, int> mp; vector<int> st; inline void M(int &x) { x -= p; x += x >> 31 & p; } inline int qpow(int a, int b) { int c = 1; for (; b; b >>= 1, a = 1ll * a * a % p) if (b & 1) c = 1ll * a * c % p; return c; } int main() { n = read(), tot = 1 << n, v.push_back({vector<int>({0}), 1}); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = 1ll * read() * qpow(100, p - 2) % p; for (int i = 0; i < n; i++) for (int j = 0; j < tot; j++) { b[i][j] = 1; for (int k = 0; k < n; k++) b[i][j] = 1ll * b[i][j] * ((j & (1 << k)) ? a[i][k] : p + 1 - a[i][k]) % p; } for (int i = 0; i < n; i++) { mp.clear(); for (int s = 0; s < tot; s++) for (pair<vector<int>, int> e : v) { vector<int> nt = e.first; int o = e.second; st.clear(); for (int t : nt) for (int j = 0; j < n; j++) if (s & (1 << j) && !(t & (1 << j))) st.push_back(t | (1 << j)); sort(st.begin(), st.end()); st.resize(unique(st.begin(), st.end()) - st.begin()); if (st.size()) M(mp[st] += 1ll * o * b[i][s] % p); } v.clear(); for (pair<vector<int>, int> e : mp) v.push_back(e); } for (pair<vector<int>, int> e : v) M(ans += e.second); write(ans, '\n'); return ot(), 0; } ```
#include <bits/stdc++.h> using namespace std; inline int Rd() { int s = 0; char fl = 0, c = getchar(); while (c < '0' || c > '9') fl |= (c == '-'), c = getchar(); while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); return fl ? -s : s; } const int N = 65000, mod = 1e9 + 7, inv = 570000004; unordered_map<bitset<128>, int> mp; int n, tot, mx, a[7][7], trans[N][128]; int f[N], g[N], p[7][128]; inline void cadd(int &x, int y) { x = (x += y) >= mod ? x - mod : x; } int DFS(bitset<128> S) { if (mp.count(S)) return mp[S]; int id = mp[S] = ++tot; bitset<128> pre[7]; for (int k = 0; k < n; ++k) for (int j = 0; j < mx; ++j) if (j >> k & 1 && S[j ^ (1 << k)]) pre[k][j] = 1; for (int i = 0; i < mx; ++i) { bitset<128> T = S; for (int k = 0; k < n; ++k) if (i >> k & 1) T |= pre[k]; trans[id][i] = DFS(T); } return id; } int main() { n = Rd(), mx = (1 << n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) a[i][j] = 1ll * Rd() * inv % mod; ++f[DFS(1)]; for (int i = 0; i < n; ++i) for (int j = 0; j < mx; ++j) { p[i][j] = 1; for (int k = 0; k < n; ++k) if (j >> k & 1) p[i][j] = 1ll * p[i][j] * a[i][k] % mod; else p[i][j] = 1ll * p[i][j] * (mod + 1 - a[i][k]) % mod; } for (int i = 0; i < n; ++i) { memset(g, 0, sizeof(int) * (tot + 1)); for (int S = 1; S <= tot; ++S) if (f[S]) for (int j = 0; j < mx; ++j) cadd(g[trans[S][j]], 1ll * f[S] * p[i][j] % mod); memcpy(f, g, sizeof(int) * (tot + 1)); } bitset<128> tmp; for (int i = 0; i < mx; ++i) tmp[i] = 1; printf("%d\n", f[mp[tmp]]); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int Rd() { int s = 0; char fl = 0, c = getchar(); while (c < '0' || c > '9') fl |= (c == '-'), c = getchar(); while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); return fl ? -s : s; } const int N = 65000, mod = 1e9 + 7, inv = 570000004; unordered_map<bitset<128>, int> mp; int n, tot, mx, a[7][7], trans[N][128]; int f[N], g[N], p[7][128]; inline void cadd(int &x, int y) { x = (x += y) >= mod ? x - mod : x; } int DFS(bitset<128> S) { if (mp.count(S)) return mp[S]; int id = mp[S] = ++tot; bitset<128> pre[7]; for (int k = 0; k < n; ++k) for (int j = 0; j < mx; ++j) if (j >> k & 1 && S[j ^ (1 << k)]) pre[k][j] = 1; for (int i = 0; i < mx; ++i) { bitset<128> T = S; for (int k = 0; k < n; ++k) if (i >> k & 1) T |= pre[k]; trans[id][i] = DFS(T); } return id; } int main() { n = Rd(), mx = (1 << n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) a[i][j] = 1ll * Rd() * inv % mod; ++f[DFS(1)]; for (int i = 0; i < n; ++i) for (int j = 0; j < mx; ++j) { p[i][j] = 1; for (int k = 0; k < n; ++k) if (j >> k & 1) p[i][j] = 1ll * p[i][j] * a[i][k] % mod; else p[i][j] = 1ll * p[i][j] * (mod + 1 - a[i][k]) % mod; } for (int i = 0; i < n; ++i) { memset(g, 0, sizeof(int) * (tot + 1)); for (int S = 1; S <= tot; ++S) if (f[S]) for (int j = 0; j < mx; ++j) cadd(g[trans[S][j]], 1ll * f[S] * p[i][j] % mod); memcpy(f, g, sizeof(int) * (tot + 1)); } bitset<128> tmp; for (int i = 0; i < mx; ++i) tmp[i] = 1; printf("%d\n", f[mp[tmp]]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 128, mod = 1e9 + 7; template <typename T> void read(T &x) { int ch = getchar(); x = 0; bool f = false; for (; ch < '0' || ch > '9'; ch = getchar()) f |= ch == '-'; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; if (f) x = -x; } void qmo(int &x) { x += x >> 31 & mod; } int ksm(int a, int b) { int res = 1; for (; b; b >>= 1, a = (unsigned long long)a * a % mod) if (b & 1) res = (unsigned long long)res * a % mod; return res; } int n, lim, p[7][7], siz[N], id[N], bit[8], tra[8][7][35], val[N]; unordered_map<unsigned long long, int> f[2]; unsigned long long pos[N]; int main() { read(n); lim = 1 << n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) read(p[i][j]); for (int i = 0; i < lim; ++i) id[i] = bit[siz[i] = siz[i >> 1] + (i & 1)]++; for (int i = 0; i < lim; ++i) for (int j = 0; j < n; ++j) tra[siz[i]][j][id[i]] = (i >> j & 1) ? -1 : id[i | (1 << j)]; int cur = 0; f[0][1] = 1; for (int i = 0; i < n; ++i, cur ^= 1) { f[!cur].clear(); for (int j = 0; j < lim; ++j) { val[j] = 1; for (int k = 0; k < n; ++k) if (j >> k & 1) val[j] = (unsigned long long)val[j] * p[i][k] % mod; else val[j] = val[j] * (100ll - p[i][k]) % mod; } int d = bit[i]; for (auto [u, v] : f[cur]) { memset(pos, 0, sizeof pos); for (int j = 0; j < n; ++j) for (int k = 0; k < d; ++k) if ((u >> k & 1) && ~tra[i][j][k]) pos[1 << j] |= 1ull << tra[i][j][k]; for (int j = 0; j < lim; ++j) { pos[j] = pos[j & -j] | pos[j ^ (j & -j)]; f[!cur][pos[j]] = (f[!cur][pos[j]] + (unsigned long long)v * val[j]) % mod; } } } printf("%lld\n", (unsigned long long)f[cur][1] * ksm(100, mod - 1 - n * n) % mod); }
### Prompt Please provide a Cpp coded solution to the problem described below: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 128, mod = 1e9 + 7; template <typename T> void read(T &x) { int ch = getchar(); x = 0; bool f = false; for (; ch < '0' || ch > '9'; ch = getchar()) f |= ch == '-'; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; if (f) x = -x; } void qmo(int &x) { x += x >> 31 & mod; } int ksm(int a, int b) { int res = 1; for (; b; b >>= 1, a = (unsigned long long)a * a % mod) if (b & 1) res = (unsigned long long)res * a % mod; return res; } int n, lim, p[7][7], siz[N], id[N], bit[8], tra[8][7][35], val[N]; unordered_map<unsigned long long, int> f[2]; unsigned long long pos[N]; int main() { read(n); lim = 1 << n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) read(p[i][j]); for (int i = 0; i < lim; ++i) id[i] = bit[siz[i] = siz[i >> 1] + (i & 1)]++; for (int i = 0; i < lim; ++i) for (int j = 0; j < n; ++j) tra[siz[i]][j][id[i]] = (i >> j & 1) ? -1 : id[i | (1 << j)]; int cur = 0; f[0][1] = 1; for (int i = 0; i < n; ++i, cur ^= 1) { f[!cur].clear(); for (int j = 0; j < lim; ++j) { val[j] = 1; for (int k = 0; k < n; ++k) if (j >> k & 1) val[j] = (unsigned long long)val[j] * p[i][k] % mod; else val[j] = val[j] * (100ll - p[i][k]) % mod; } int d = bit[i]; for (auto [u, v] : f[cur]) { memset(pos, 0, sizeof pos); for (int j = 0; j < n; ++j) for (int k = 0; k < d; ++k) if ((u >> k & 1) && ~tra[i][j][k]) pos[1 << j] |= 1ull << tra[i][j][k]; for (int j = 0; j < lim; ++j) { pos[j] = pos[j & -j] | pos[j ^ (j & -j)]; f[!cur][pos[j]] = (f[!cur][pos[j]] + (unsigned long long)v * val[j]) % mod; } } } printf("%lld\n", (unsigned long long)f[cur][1] * ksm(100, mod - 1 - n * n) % mod); } ```
#include <bits/stdc++.h> using namespace std; const int N = 7, M = 20, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { a += b; if (a < 0) a += MOD; else if (a >= MOD) a -= MOD; return a; } void _sum(int &a, int b) { a = sum(a, b); } int mul(int a, int b) { return 1LL * a * b % MOD; } void _mul(int &a, int b) { a = mul(a, b); } int power(int a, int b) { int res = 1; while (b) { if (b & 1) _mul(res, a); _mul(a, a); b >>= 1; } return res; } int inv(int x) { return power(x, MOD - 2); } int n, f[1 << N], p[N][N], dp[N][1 << M], pro[N][1 << N]; vector<int> vec[N]; void input() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; _mul(p[i][j], inv(100)); } for (int mask = 0; mask < (1 << n); mask++) { pro[i][mask] = 1; for (int j = 0; j < n; j++) if (mask & (1 << j)) _mul(pro[i][mask], p[i][j]); else _mul(pro[i][mask], sum(1, -p[i][j])); } } } void pre_pro() { for (int i = 0; i < (1 << n); i++) { f[i] = vec[__builtin_popcount(i)].size(); vec[__builtin_popcount(i)].push_back(i); } } void act(int i, int mask, int nei) { int nxt = 0; vector<int> a, b; for (int j = 0; j < vec[i].size(); j++) if (mask & (1 << j)) a.push_back(vec[i][j]); for (int j = 0; j < n; j++) if (nei & (1 << j)) b.push_back(j); for (int x : a) for (int y : b) if ((x & (1 << y)) == false) nxt |= (1 << f[x | (1 << y)]); _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei])); } void solve() { dp[0][1] = 1; for (int i = 0; i < n; i++) for (int mask = 0; mask < (1 << vec[i].size()); mask++) if (dp[i][mask]) for (int nei = 0; nei < (1 << n); nei++) act(i, mask, nei); } int main() { ios::sync_with_stdio(false), cin.tie(0); input(); pre_pro(); solve(); cout << dp[n][1]; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 7, M = 20, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { a += b; if (a < 0) a += MOD; else if (a >= MOD) a -= MOD; return a; } void _sum(int &a, int b) { a = sum(a, b); } int mul(int a, int b) { return 1LL * a * b % MOD; } void _mul(int &a, int b) { a = mul(a, b); } int power(int a, int b) { int res = 1; while (b) { if (b & 1) _mul(res, a); _mul(a, a); b >>= 1; } return res; } int inv(int x) { return power(x, MOD - 2); } int n, f[1 << N], p[N][N], dp[N][1 << M], pro[N][1 << N]; vector<int> vec[N]; void input() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; _mul(p[i][j], inv(100)); } for (int mask = 0; mask < (1 << n); mask++) { pro[i][mask] = 1; for (int j = 0; j < n; j++) if (mask & (1 << j)) _mul(pro[i][mask], p[i][j]); else _mul(pro[i][mask], sum(1, -p[i][j])); } } } void pre_pro() { for (int i = 0; i < (1 << n); i++) { f[i] = vec[__builtin_popcount(i)].size(); vec[__builtin_popcount(i)].push_back(i); } } void act(int i, int mask, int nei) { int nxt = 0; vector<int> a, b; for (int j = 0; j < vec[i].size(); j++) if (mask & (1 << j)) a.push_back(vec[i][j]); for (int j = 0; j < n; j++) if (nei & (1 << j)) b.push_back(j); for (int x : a) for (int y : b) if ((x & (1 << y)) == false) nxt |= (1 << f[x | (1 << y)]); _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei])); } void solve() { dp[0][1] = 1; for (int i = 0; i < n; i++) for (int mask = 0; mask < (1 << vec[i].size()); mask++) if (dp[i][mask]) for (int nei = 0; nei < (1 << n); nei++) act(i, mask, nei); } int main() { ios::sync_with_stdio(false), cin.tie(0); input(); pre_pro(); solve(); cout << dp[n][1]; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) x = x * 10 - '0' + c, c = getchar(); x *= f; } const int mod = 1e9 + 7; inline int Pow(int x, int y) { int res = 1; for (; y; y >>= 1, x = x * (long long)x % mod) if (y & 1) res = res * (long long)x % mod; return res; } const int inv100 = Pow(100, mod - 2); unordered_map<long long, int> f[8]; int mp[8][8], n; int cnt[1 << 7], id[1 << 7], siz[1 << 7], pos[8][1 << 7]; int main() { rd(n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) rd(mp[i][j]), mp[i][j] = mp[i][j] * (long long)inv100 % mod; for (int S = 0; S < (1 << n); ++S) { cnt[S] = cnt[S >> 1] + (S & 1); pos[cnt[S]][id[S] = siz[cnt[S]]++] = S; } f[0][1] = 1; for (int i = 0; i < n; ++i) { vector<int> item[8]; for (int x = 0; x < n; ++x) for (int k = 0; k < siz[i]; ++k) if (!(pos[i][k] >> x & 1)) item[x].push_back(k); for (auto it = f[i].begin(); it != f[i].end(); ++it) for (int S = 0; S < (1 << n); ++S) { int v = it->second; long long u = 0; for (int x = 0; x < n; ++x) if (S >> x & 1) { v = v * (long long)mp[i][x] % mod; for (int t = 0; t < item[x].size(); ++t) if ((it->first) >> item[x][t] & 1) u |= 1ll << id[pos[i][item[x][t]] | 1 << x]; } else v = v * (long long)(1 - mp[i][x]) % mod; if (!u) continue; (f[i + 1][u] += v) %= mod; } } printf("%d\n", (f[n][1] + mod) % mod); return 0; }
### Prompt Create a solution in CPP for the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) x = x * 10 - '0' + c, c = getchar(); x *= f; } const int mod = 1e9 + 7; inline int Pow(int x, int y) { int res = 1; for (; y; y >>= 1, x = x * (long long)x % mod) if (y & 1) res = res * (long long)x % mod; return res; } const int inv100 = Pow(100, mod - 2); unordered_map<long long, int> f[8]; int mp[8][8], n; int cnt[1 << 7], id[1 << 7], siz[1 << 7], pos[8][1 << 7]; int main() { rd(n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) rd(mp[i][j]), mp[i][j] = mp[i][j] * (long long)inv100 % mod; for (int S = 0; S < (1 << n); ++S) { cnt[S] = cnt[S >> 1] + (S & 1); pos[cnt[S]][id[S] = siz[cnt[S]]++] = S; } f[0][1] = 1; for (int i = 0; i < n; ++i) { vector<int> item[8]; for (int x = 0; x < n; ++x) for (int k = 0; k < siz[i]; ++k) if (!(pos[i][k] >> x & 1)) item[x].push_back(k); for (auto it = f[i].begin(); it != f[i].end(); ++it) for (int S = 0; S < (1 << n); ++S) { int v = it->second; long long u = 0; for (int x = 0; x < n; ++x) if (S >> x & 1) { v = v * (long long)mp[i][x] % mod; for (int t = 0; t < item[x].size(); ++t) if ((it->first) >> item[x][t] & 1) u |= 1ll << id[pos[i][item[x][t]] | 1 << x]; } else v = v * (long long)(1 - mp[i][x]) % mod; if (!u) continue; (f[i + 1][u] += v) %= mod; } } printf("%d\n", (f[n][1] + mod) % mod); return 0; } ```
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long MOD = (long long)1e9 + 7; long long add(long long x, long long y) { x += y; if (x >= MOD) return x - MOD; return x; } long long sub(long long x, long long y) { x -= y; if (x < 0) return x + MOD; return x; } long long mult(long long x, long long y) { return (x * y) % MOD; } long long bin_pow(long long x, long long p) { if (p == 0) return 1; if (p & 1) return mult(x, bin_pow(x, p - 1)); return bin_pow(mult(x, x), p / 2); } long long rev(long long x) { return bin_pow(x, MOD - 2); } const int N = 7; const int M = (1 << N) + 1; int ppc[M]; int idInPpc[M]; vector<int> maskForPpc[N + 1]; long long trans[N + 1][M][M]; int n; unordered_map<long long, long long> dp[N + 1]; long long A[N][N]; void precalc() { for (int mask = 1; mask < (1 << n); mask++) ppc[mask] = ppc[mask >> 1] + (mask & 1); for (int mask = 0; mask < (1 << n); mask++) { int k = ppc[mask]; idInPpc[mask] = (int)maskForPpc[k].size(); maskForPpc[k].push_back(mask); } for (int k = 0; k <= n; k++) for (int i = 0; i < (int)maskForPpc[k].size(); i++) { int inM = maskForPpc[k][i]; for (int mask = 0; mask < (1 << n); mask++) { long long res = 0; if ((mask & inM) == mask) continue; for (int v = 0; v < n; v++) { if (((mask >> v) & 1) == 0) continue; if ((inM >> v) & 1) continue; int nmask = inM | (1 << v); res |= 1LL << idInPpc[nmask]; } trans[k][i][mask] = res; } } } void read() { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { scanf("%lld", &A[i][j]); A[i][j] = mult(A[i][j], rev(100)); } } int main() { scanf("%d", &n); precalc(); read(); dp[0][1] = 1; for (int k = 0; k < n; k++) for (int mask = 0; mask < (1 << n); mask++) { long long W = 1; for (int i = 0; i < n; i++) { if ((mask >> i) & 1) W = mult(W, A[k][i]); else W = mult(W, sub(1, A[k][i])); } for (pair<long long, long long> s : dp[k]) { long long zmask = s.first, val = s.second; long long nmask = 0; for (int i = 0; i < (int)maskForPpc[k].size(); i++) { if ((zmask >> i) & 1) nmask |= trans[k][i][mask]; } if (nmask == 0) continue; val = mult(val, W); dp[k + 1][nmask] = add(dp[k + 1][nmask], val); } } printf("%lld\n", dp[n][1]); return 0; }
### Prompt Create a solution in Cpp for the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long MOD = (long long)1e9 + 7; long long add(long long x, long long y) { x += y; if (x >= MOD) return x - MOD; return x; } long long sub(long long x, long long y) { x -= y; if (x < 0) return x + MOD; return x; } long long mult(long long x, long long y) { return (x * y) % MOD; } long long bin_pow(long long x, long long p) { if (p == 0) return 1; if (p & 1) return mult(x, bin_pow(x, p - 1)); return bin_pow(mult(x, x), p / 2); } long long rev(long long x) { return bin_pow(x, MOD - 2); } const int N = 7; const int M = (1 << N) + 1; int ppc[M]; int idInPpc[M]; vector<int> maskForPpc[N + 1]; long long trans[N + 1][M][M]; int n; unordered_map<long long, long long> dp[N + 1]; long long A[N][N]; void precalc() { for (int mask = 1; mask < (1 << n); mask++) ppc[mask] = ppc[mask >> 1] + (mask & 1); for (int mask = 0; mask < (1 << n); mask++) { int k = ppc[mask]; idInPpc[mask] = (int)maskForPpc[k].size(); maskForPpc[k].push_back(mask); } for (int k = 0; k <= n; k++) for (int i = 0; i < (int)maskForPpc[k].size(); i++) { int inM = maskForPpc[k][i]; for (int mask = 0; mask < (1 << n); mask++) { long long res = 0; if ((mask & inM) == mask) continue; for (int v = 0; v < n; v++) { if (((mask >> v) & 1) == 0) continue; if ((inM >> v) & 1) continue; int nmask = inM | (1 << v); res |= 1LL << idInPpc[nmask]; } trans[k][i][mask] = res; } } } void read() { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { scanf("%lld", &A[i][j]); A[i][j] = mult(A[i][j], rev(100)); } } int main() { scanf("%d", &n); precalc(); read(); dp[0][1] = 1; for (int k = 0; k < n; k++) for (int mask = 0; mask < (1 << n); mask++) { long long W = 1; for (int i = 0; i < n; i++) { if ((mask >> i) & 1) W = mult(W, A[k][i]); else W = mult(W, sub(1, A[k][i])); } for (pair<long long, long long> s : dp[k]) { long long zmask = s.first, val = s.second; long long nmask = 0; for (int i = 0; i < (int)maskForPpc[k].size(); i++) { if ((zmask >> i) & 1) nmask |= trans[k][i][mask]; } if (nmask == 0) continue; val = mult(val, W); dp[k + 1][nmask] = add(dp[k + 1][nmask], val); } } printf("%lld\n", dp[n][1]); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; string direc = "URDL"; const long long MOD2 = (long long)1000000007 * (long long)1000000007; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } unordered_map<bitset<128>, int> mp; int cnt, dp[65000][130]; int dfs(bitset<128> s) { if (mp.find(s) != mp.end()) return mp[s]; int z = cnt++; mp[s] = z; for (int(i) = 0; (i) < (int)(1 << n); (i)++) { bitset<128> t = s; for (int(j) = 0; (j) < (int)(1 << n); (j)++) if (!t[j]) { for (int k = i & j; k; k ^= k & -k) if (s[j ^ (k & -k)]) { t[j] = 1; break; } } dp[z][i] = dfs(t); } return z; } int p[10][10], q[10][130], f[9][65000]; void fmain(int tid) { scanf("%d", &n); dfs(1); int inv = fastPow(100, 1000000007 - 2); for (int(i) = 0; (i) < (int)(n); (i)++) { for (int(j) = 0; (j) < (int)(n); (j)++) { scanf("%d", &p[i][j]); p[i][j] = (long long)p[i][j] * inv % 1000000007; } } for (int(j) = 0; (j) < (int)(n); (j)++) { for (int(s) = 0; (s) < (int)(1 << n); (s)++) { q[j][s] = 1; for (int(z) = 0; (z) < (int)(n); (z)++) if (s & (1 << z)) q[j][s] = (long long)q[j][s] * p[z][j] % 1000000007; else q[j][s] = (long long)q[j][s] * (1000000007 + 1 - p[z][j]) % 1000000007; } } f[0][0] = 1; for (int(i) = 0; (i) < (int)(n); (i)++) for (int(j) = 0; (j) < (int)(cnt); (j)++) { for (int(k) = 0; (k) < (int)(1 << n); (k)++) { addmod(f[i + 1][dp[j][k]], (long long)f[i][j] * q[i][k] % 1000000007); } } bitset<128> z; for (int(i) = 0; (i) < (int)(1 << n); (i)++) z[i] = 1; printf("%d\n", f[n][mp[z]]); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
### Prompt Generate a CPP solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; string direc = "URDL"; const long long MOD2 = (long long)1000000007 * (long long)1000000007; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } unordered_map<bitset<128>, int> mp; int cnt, dp[65000][130]; int dfs(bitset<128> s) { if (mp.find(s) != mp.end()) return mp[s]; int z = cnt++; mp[s] = z; for (int(i) = 0; (i) < (int)(1 << n); (i)++) { bitset<128> t = s; for (int(j) = 0; (j) < (int)(1 << n); (j)++) if (!t[j]) { for (int k = i & j; k; k ^= k & -k) if (s[j ^ (k & -k)]) { t[j] = 1; break; } } dp[z][i] = dfs(t); } return z; } int p[10][10], q[10][130], f[9][65000]; void fmain(int tid) { scanf("%d", &n); dfs(1); int inv = fastPow(100, 1000000007 - 2); for (int(i) = 0; (i) < (int)(n); (i)++) { for (int(j) = 0; (j) < (int)(n); (j)++) { scanf("%d", &p[i][j]); p[i][j] = (long long)p[i][j] * inv % 1000000007; } } for (int(j) = 0; (j) < (int)(n); (j)++) { for (int(s) = 0; (s) < (int)(1 << n); (s)++) { q[j][s] = 1; for (int(z) = 0; (z) < (int)(n); (z)++) if (s & (1 << z)) q[j][s] = (long long)q[j][s] * p[z][j] % 1000000007; else q[j][s] = (long long)q[j][s] * (1000000007 + 1 - p[z][j]) % 1000000007; } } f[0][0] = 1; for (int(i) = 0; (i) < (int)(n); (i)++) for (int(j) = 0; (j) < (int)(cnt); (j)++) { for (int(k) = 0; (k) < (int)(1 << n); (k)++) { addmod(f[i + 1][dp[j][k]], (long long)f[i][j] * q[i][k] % 1000000007); } } bitset<128> z; for (int(i) = 0; (i) < (int)(1 << n); (i)++) z[i] = 1; printf("%d\n", f[n][mp[z]]); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize "-O3" #pragma GCC optimize("Ofast") #pragma comment(linker, "/STACK:1000000000") const int mod = 1e9 + 7; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp1(vector<long long> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp1(vector<int> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp2(vector<vector<int>> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cerr << ou[i][j] << ' '; } cerr << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 301; int fact[maxn], ofact[maxn]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } const int rsto = bp(100, mod - 2); void recalc(map<vector<int>, int> &dyn, const vector<int> &probs, int t, vector<vector<vector<int>>> &trans) { vector<int> goodsz; map<vector<int>, int> newdyn; int n = probs.size(); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; if (cnt == t) goodsz.push_back(mask); } for (int mask = 0; mask < (1 << n); mask++) { int prb = 1; vector<int> added; for (int i = 0; i < n; i++) { if (ibit(mask, i)) { added.push_back(i); prb = mlt(prb, probs[i]); } else prb = mlt(prb, mod + 1 - probs[i]); } for (auto it = dyn.begin(); it != dyn.end(); it++) { vector<int> va = it->first; vector<int> used(trans[t + 1].size()); for (int i = 0; i < va.size(); i++) { if (va[i]) { for (auto pos : added) { int val = trans[t][i][pos]; if (val != -1) used[val] = 1; } } } if (!newdyn.count(used)) newdyn[used] = mlt(prb, it->second); else newdyn[used] = sum(newdyn[used], mlt(prb, it->second)); } } dyn = newdyn; } vector<vector<int>> enc; vector<vector<vector<int>>> trans; void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n; cin >> n; enc.resize(n + 1); trans.resize(n + 1); vector<vector<int>> v(n, vector<int>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> v[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) v[i][j] = mlt(v[i][j], rsto); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; enc[cnt].push_back(mask); } for (int i = 0; i <= n; i++) { trans[i].resize(enc[i].size()); for (int pos = 0; pos < enc[i].size(); pos++) { int j = enc[i][pos]; for (int h = 0; h < n; h++) { if (ibit(j, h)) trans[i][pos].push_back(-1); else { int val = j + (1 << h); for (int q = 0; q < enc[i + 1].size(); q++) { if (enc[i + 1][q] == val) { trans[i][pos].push_back(q); } } } } } } map<vector<int>, int> dyn; vector<int> q = {1}; dyn[q] = 1; for (int i = 0; i < n; i++) recalc(dyn, v[i], i, trans); vector<int> vt = {1}; cout << dyn[vt] << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tests = 1; for (int i = 0; i < tests; i++) { solve(); } }
### Prompt Generate a Cpp solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimize "-O3" #pragma GCC optimize("Ofast") #pragma comment(linker, "/STACK:1000000000") const int mod = 1e9 + 7; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp1(vector<long long> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp1(vector<int> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { cerr << ou[i] << ' '; } cerr << endl; } void outp2(vector<vector<int>> &ou, string s = " ") { cerr << s << endl; for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cerr << ou[i][j] << ' '; } cerr << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 301; int fact[maxn], ofact[maxn]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } const int rsto = bp(100, mod - 2); void recalc(map<vector<int>, int> &dyn, const vector<int> &probs, int t, vector<vector<vector<int>>> &trans) { vector<int> goodsz; map<vector<int>, int> newdyn; int n = probs.size(); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; if (cnt == t) goodsz.push_back(mask); } for (int mask = 0; mask < (1 << n); mask++) { int prb = 1; vector<int> added; for (int i = 0; i < n; i++) { if (ibit(mask, i)) { added.push_back(i); prb = mlt(prb, probs[i]); } else prb = mlt(prb, mod + 1 - probs[i]); } for (auto it = dyn.begin(); it != dyn.end(); it++) { vector<int> va = it->first; vector<int> used(trans[t + 1].size()); for (int i = 0; i < va.size(); i++) { if (va[i]) { for (auto pos : added) { int val = trans[t][i][pos]; if (val != -1) used[val] = 1; } } } if (!newdyn.count(used)) newdyn[used] = mlt(prb, it->second); else newdyn[used] = sum(newdyn[used], mlt(prb, it->second)); } } dyn = newdyn; } vector<vector<int>> enc; vector<vector<vector<int>>> trans; void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n; cin >> n; enc.resize(n + 1); trans.resize(n + 1); vector<vector<int>> v(n, vector<int>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> v[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) v[i][j] = mlt(v[i][j], rsto); for (int mask = 0; mask < (1 << n); mask++) { int cnt = 0; for (int i = 0; i < n; i++) if (ibit(mask, i)) cnt++; enc[cnt].push_back(mask); } for (int i = 0; i <= n; i++) { trans[i].resize(enc[i].size()); for (int pos = 0; pos < enc[i].size(); pos++) { int j = enc[i][pos]; for (int h = 0; h < n; h++) { if (ibit(j, h)) trans[i][pos].push_back(-1); else { int val = j + (1 << h); for (int q = 0; q < enc[i + 1].size(); q++) { if (enc[i + 1][q] == val) { trans[i][pos].push_back(q); } } } } } } map<vector<int>, int> dyn; vector<int> q = {1}; dyn[q] = 1; for (int i = 0; i < n; i++) recalc(dyn, v[i], i, trans); vector<int> vt = {1}; cout << dyn[vt] << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tests = 1; for (int i = 0; i < tests; i++) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; inline long long mul(const long long& a, const long long& b) { return (a * b) % mod; } inline long long add(const long long& a, const long long& b) { return (a + b + mod) % mod; } inline void upd(long long& a, const long long& b) { a = (a + b + mod) % mod; } inline bool has(int x, int i) { return (x & (1 << i)) > 0; } long long pwr(const long long& a, const long long& b) { if (!b) return 1; long long h = pwr(a, b >> 1); return mul(mul(h, h), ((b & 1) ? a : 1)); } const int n = 6, k = (n * (n - 1) * (n - 2)) / 6, i100 = pwr(100, mod - 2); vector<int> num(1 << n, -1), mask; vector<vector<long long> > p(2, vector<long long>((1 << k), 0)); int find_matchings(int& g, int x) { int ans = 0; for (int l0 = 0; l0 < n; l0++) for (int l1 = 0; l1 < n; l1++) for (int l2 = 0; l2 < n; l2++) if (l0 != l1 && l1 != l2 && l0 != l2 && has(g, l0) && has(g, n + l1) && has(g, 2 * n + l2)) ans |= (1 << num[x ^ (1 << l0) ^ (1 << l1) ^ (1 << l2)]); return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); for (int i = 0; i < (1 << n); i++) { int c = 0; for (int j = 0; j < n; j++) if (i & (1 << j)) c++; if (c == n / 2) num[i] = mask.size(), mask.push_back(i); } int m; cin >> m; vector<long long> pe(n * n, 0); for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) cin >> pe[i * n + j]; for (int i = m; i < n; i++) pe[i * n + i] = 100; for (int i = 0; i < n * n; i++) pe[i] = mul(pe[i], i100); int e = n * (n / 2); for (int c = 0; c < 2; c++) for (int i = 0; i < (1 << e); i++) { long long pr = 1; for (int j = 0; j < e; j++) if (has(i, j)) pr = mul(pr, pe[c * e + j]); else pr = mul(pr, add(1, -pe[c * e + j])); int mask = find_matchings(i, c * ((1 << n) - 1)); upd(p[c][mask], pr); } for (int i = 0; i < k; i++) for (int mask = 0; mask < (1 << k); mask++) if (has(mask, i)) upd(p[1][mask], p[1][mask ^ (1 << i)]); long long ans = 0; for (int mask = 0; mask < (1 << k); mask++) upd(ans, mul(p[0][mask], p[1][((1 << k) - 1) ^ mask])); cout << add(1, -ans) << "\n"; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; inline long long mul(const long long& a, const long long& b) { return (a * b) % mod; } inline long long add(const long long& a, const long long& b) { return (a + b + mod) % mod; } inline void upd(long long& a, const long long& b) { a = (a + b + mod) % mod; } inline bool has(int x, int i) { return (x & (1 << i)) > 0; } long long pwr(const long long& a, const long long& b) { if (!b) return 1; long long h = pwr(a, b >> 1); return mul(mul(h, h), ((b & 1) ? a : 1)); } const int n = 6, k = (n * (n - 1) * (n - 2)) / 6, i100 = pwr(100, mod - 2); vector<int> num(1 << n, -1), mask; vector<vector<long long> > p(2, vector<long long>((1 << k), 0)); int find_matchings(int& g, int x) { int ans = 0; for (int l0 = 0; l0 < n; l0++) for (int l1 = 0; l1 < n; l1++) for (int l2 = 0; l2 < n; l2++) if (l0 != l1 && l1 != l2 && l0 != l2 && has(g, l0) && has(g, n + l1) && has(g, 2 * n + l2)) ans |= (1 << num[x ^ (1 << l0) ^ (1 << l1) ^ (1 << l2)]); return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); for (int i = 0; i < (1 << n); i++) { int c = 0; for (int j = 0; j < n; j++) if (i & (1 << j)) c++; if (c == n / 2) num[i] = mask.size(), mask.push_back(i); } int m; cin >> m; vector<long long> pe(n * n, 0); for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) cin >> pe[i * n + j]; for (int i = m; i < n; i++) pe[i * n + i] = 100; for (int i = 0; i < n * n; i++) pe[i] = mul(pe[i], i100); int e = n * (n / 2); for (int c = 0; c < 2; c++) for (int i = 0; i < (1 << e); i++) { long long pr = 1; for (int j = 0; j < e; j++) if (has(i, j)) pr = mul(pr, pe[c * e + j]); else pr = mul(pr, add(1, -pe[c * e + j])); int mask = find_matchings(i, c * ((1 << n) - 1)); upd(p[c][mask], pr); } for (int i = 0; i < k; i++) for (int mask = 0; mask < (1 << k); mask++) if (has(mask, i)) upd(p[1][mask], p[1][mask ^ (1 << i)]); long long ans = 0; for (int mask = 0; mask < (1 << k); mask++) upd(ans, mul(p[0][mask], p[1][((1 << k) - 1) ^ mask])); cout << add(1, -ans) << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; unordered_map<bitset<130>, int> id; int trans[100005][130], p[10][10], tot, f[10][100005], n; int Power(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; y >>= 1, x = 1ll * x * x % mod; } return r; } int dfs(bitset<130> cur) { if (id.count(cur)) return id[cur]; int w = ++tot; bitset<130> t[10], tmp; id[cur] = w; for (int i = 0; i < n; i++) for (int j = 0; j < (1 << n); j++) if (cur[j]) t[i][j | (1 << i)] = 1; for (int i = 0; i < (1 << n); i++) { tmp = cur; for (int j = 0; j < n; j++) if (i & (1 << j)) tmp |= t[j]; trans[w][i] = dfs(tmp); } return w; } int main() { cin >> n; for (int i = 0, inv100 = Power(100, mod - 2); i < n; i++) for (int j = 0; j < n; j++) cin >> p[i][j], p[i][j] = 1ll * inv100 * p[i][j] % mod; bitset<130> tmp; tmp[0] = 1, f[0][dfs(tmp)] = 1; for (int i = 0; i < n; i++) { for (int j = 1; j <= tot; j++) { for (int k = 0; k < (1 << n); k++) { int pro = 1; for (int w = 0; w < n; w++) if (k & (1 << w)) pro = 1ll * pro * p[w][i] % mod; else pro = 1ll * pro * (1 - p[w][i] + mod) % mod; f[i + 1][trans[j][k]] = (f[i + 1][trans[j][k]] + 1ll * pro * f[i][j]) % mod; } } } for (int i = 0; i < (1 << n); i++) tmp[i] = 1; cout << f[n][id[tmp]]; }
### Prompt Your challenge is to write a CPP solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; unordered_map<bitset<130>, int> id; int trans[100005][130], p[10][10], tot, f[10][100005], n; int Power(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; y >>= 1, x = 1ll * x * x % mod; } return r; } int dfs(bitset<130> cur) { if (id.count(cur)) return id[cur]; int w = ++tot; bitset<130> t[10], tmp; id[cur] = w; for (int i = 0; i < n; i++) for (int j = 0; j < (1 << n); j++) if (cur[j]) t[i][j | (1 << i)] = 1; for (int i = 0; i < (1 << n); i++) { tmp = cur; for (int j = 0; j < n; j++) if (i & (1 << j)) tmp |= t[j]; trans[w][i] = dfs(tmp); } return w; } int main() { cin >> n; for (int i = 0, inv100 = Power(100, mod - 2); i < n; i++) for (int j = 0; j < n; j++) cin >> p[i][j], p[i][j] = 1ll * inv100 * p[i][j] % mod; bitset<130> tmp; tmp[0] = 1, f[0][dfs(tmp)] = 1; for (int i = 0; i < n; i++) { for (int j = 1; j <= tot; j++) { for (int k = 0; k < (1 << n); k++) { int pro = 1; for (int w = 0; w < n; w++) if (k & (1 << w)) pro = 1ll * pro * p[w][i] % mod; else pro = 1ll * pro * (1 - p[w][i] + mod) % mod; f[i + 1][trans[j][k]] = (f[i + 1][trans[j][k]] + 1ll * pro * f[i][j]) % mod; } } } for (int i = 0; i < (1 << n); i++) tmp[i] = 1; cout << f[n][id[tmp]]; } ```
#include <bits/stdc++.h> using namespace std; const int N = 6; const int MOD = (int)1e9 + 7; int ipow(int a, int b) { if (b == 0) return 1; int ans = ipow(a, b / 2); ans = 1LL * ans * ans % MOD; if (b & 1) ans = 1LL * ans * a % MOD; return ans; } int modInv(int a) { return ipow(a, MOD - 2); } int p[N][N]; int solve() { int NCN2 = 1; for (int i = N / 2 + 1; i <= N; ++i) NCN2 *= i; for (int i = 1; i <= N / 2; ++i) NCN2 /= i; vector<vector<int> > fg(2, vector<int>(1 << (NCN2))); for (int msk = 0; msk < (1 << (N * N / 2)); ++msk) { for (int side = 0; side < 2; ++side) { int prob = 1; for (int r = 0; r < N; ++r) for (int c = 0; c < N / 2; ++c) { int i = c * N + r; int pr; if (msk & (1 << i)) pr = p[r][c + side * N / 2]; else pr = MOD + 1 - p[r][c + side * N / 2]; prob = 1LL * prob * pr % MOD; } vector<int> choice(N); fill(choice.begin() + N / 2, choice.end(), 1); if (side) reverse(choice.begin(), choice.end()); auto Do = [&]() { if (side) return prev_permutation(choice.begin(), choice.end()); else return next_permutation(choice.begin(), choice.end()); }; int comb_idx = 0, comb_mask = 0; do { int mask = 0; for (int i = 0; i < N; ++i) if (choice[i]) mask |= 1 << i; bool marriage = true; for (int i = 0; i < (1 << (N / 2)); ++i) { int tot_mask = 0; for (int j = 0; j < N / 2; ++j) if (i & (1 << j)) tot_mask |= msk >> (j * N); tot_mask &= mask; if (__builtin_popcount(tot_mask) < __builtin_popcount(i)) { marriage = false; break; } } if (marriage) comb_mask |= 1 << comb_idx; ++comb_idx; } while (Do()); fg[side][comb_mask] += prob; if (fg[side][comb_mask] >= MOD) fg[side][comb_mask] -= MOD; } } for (int i = 0; i < NCN2; ++i) for (int j = 0; j < (1 << NCN2); ++j) if (!(j & (1 << i))) { fg[1][j | (1 << i)] += fg[1][j]; if (fg[1][j | (1 << i)] >= MOD) fg[1][j | (1 << i)] -= MOD; } int ans = 0; for (int i = 0; i < (1 << NCN2); ++i) ans = (ans + 1LL * fg[0][i] * fg[1][((1 << NCN2) - 1) ^ i]) % MOD; return (MOD + 1 - ans) % MOD; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { int t; scanf("%d", &t); p[i][j] = 1LL * t * modInv(100) % MOD; } for (int i = n; i < N; ++i) p[i][i] = 1; printf("%d\n", solve()); }
### Prompt Please provide a CPP coded solution to the problem described below: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 6; const int MOD = (int)1e9 + 7; int ipow(int a, int b) { if (b == 0) return 1; int ans = ipow(a, b / 2); ans = 1LL * ans * ans % MOD; if (b & 1) ans = 1LL * ans * a % MOD; return ans; } int modInv(int a) { return ipow(a, MOD - 2); } int p[N][N]; int solve() { int NCN2 = 1; for (int i = N / 2 + 1; i <= N; ++i) NCN2 *= i; for (int i = 1; i <= N / 2; ++i) NCN2 /= i; vector<vector<int> > fg(2, vector<int>(1 << (NCN2))); for (int msk = 0; msk < (1 << (N * N / 2)); ++msk) { for (int side = 0; side < 2; ++side) { int prob = 1; for (int r = 0; r < N; ++r) for (int c = 0; c < N / 2; ++c) { int i = c * N + r; int pr; if (msk & (1 << i)) pr = p[r][c + side * N / 2]; else pr = MOD + 1 - p[r][c + side * N / 2]; prob = 1LL * prob * pr % MOD; } vector<int> choice(N); fill(choice.begin() + N / 2, choice.end(), 1); if (side) reverse(choice.begin(), choice.end()); auto Do = [&]() { if (side) return prev_permutation(choice.begin(), choice.end()); else return next_permutation(choice.begin(), choice.end()); }; int comb_idx = 0, comb_mask = 0; do { int mask = 0; for (int i = 0; i < N; ++i) if (choice[i]) mask |= 1 << i; bool marriage = true; for (int i = 0; i < (1 << (N / 2)); ++i) { int tot_mask = 0; for (int j = 0; j < N / 2; ++j) if (i & (1 << j)) tot_mask |= msk >> (j * N); tot_mask &= mask; if (__builtin_popcount(tot_mask) < __builtin_popcount(i)) { marriage = false; break; } } if (marriage) comb_mask |= 1 << comb_idx; ++comb_idx; } while (Do()); fg[side][comb_mask] += prob; if (fg[side][comb_mask] >= MOD) fg[side][comb_mask] -= MOD; } } for (int i = 0; i < NCN2; ++i) for (int j = 0; j < (1 << NCN2); ++j) if (!(j & (1 << i))) { fg[1][j | (1 << i)] += fg[1][j]; if (fg[1][j | (1 << i)] >= MOD) fg[1][j | (1 << i)] -= MOD; } int ans = 0; for (int i = 0; i < (1 << NCN2); ++i) ans = (ans + 1LL * fg[0][i] * fg[1][((1 << NCN2) - 1) ^ i]) % MOD; return (MOD + 1 - ans) % MOD; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { int t; scanf("%d", &t); p[i][j] = 1LL * t * modInv(100) % MOD; } for (int i = n; i < N; ++i) p[i][i] = 1; printf("%d\n", solve()); } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int mod = 1e9 + 7; int n, m, tt = 0, ch[N][64], p[10][10], q[10][64], f[10][N]; map<unsigned long long, int> id; int qpow(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod, b >>= 1; } return ret; } int dfs(unsigned long long s) { if (id[s]) return id[s]; id[s] = ++tt; for (int i = 0; i < (1 << n); i++) { unsigned long long t = s; for (int j = 0; j < (1 << n); j++) for (int k = 0; k < n; k++) if ((j >> k & 1) && (i >> k & 1) && (s >> (j ^ (1 << k)) & 1)) t |= 1llu << j; ch[id[s]][i] = dfs(t); } return id[s]; } int main() { cin >> n; dfs(1); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> p[i][j], p[i][j] = 1ll * p[i][j] * qpow(100, mod - 2) % mod; for (int i = 0; i < n; i++) for (int j = 0; j < (1 << n); j++) { q[i][j] = 1; for (int k = 0; k < n; k++) { if (j >> k & 1) q[i][j] = 1ll * q[i][j] * p[k][i] % mod; else q[i][j] = 1ll * q[i][j] * (mod + 1 - p[k][i]) % mod; } } f[0][1] = 1; for (int i = 0; i < n; i++) for (int j = 1; j <= tt; j++) for (int k = 0; k < (1 << n); k++) f[i + 1][ch[j][k]] = (f[i + 1][ch[j][k]] + 1ll * f[i][j] * q[i][k]) % mod; if (n < 6) cout << f[n][id[(1llu << (1 << n)) - 1]]; else cout << f[n][id[18446744073709551615llu]]; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int mod = 1e9 + 7; int n, m, tt = 0, ch[N][64], p[10][10], q[10][64], f[10][N]; map<unsigned long long, int> id; int qpow(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod, b >>= 1; } return ret; } int dfs(unsigned long long s) { if (id[s]) return id[s]; id[s] = ++tt; for (int i = 0; i < (1 << n); i++) { unsigned long long t = s; for (int j = 0; j < (1 << n); j++) for (int k = 0; k < n; k++) if ((j >> k & 1) && (i >> k & 1) && (s >> (j ^ (1 << k)) & 1)) t |= 1llu << j; ch[id[s]][i] = dfs(t); } return id[s]; } int main() { cin >> n; dfs(1); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> p[i][j], p[i][j] = 1ll * p[i][j] * qpow(100, mod - 2) % mod; for (int i = 0; i < n; i++) for (int j = 0; j < (1 << n); j++) { q[i][j] = 1; for (int k = 0; k < n; k++) { if (j >> k & 1) q[i][j] = 1ll * q[i][j] * p[k][i] % mod; else q[i][j] = 1ll * q[i][j] * (mod + 1 - p[k][i]) % mod; } } f[0][1] = 1; for (int i = 0; i < n; i++) for (int j = 1; j <= tt; j++) for (int k = 0; k < (1 << n); k++) f[i + 1][ch[j][k]] = (f[i + 1][ch[j][k]] + 1ll * f[i][j] * q[i][k]) % mod; if (n < 6) cout << f[n][id[(1llu << (1 << n)) - 1]]; else cout << f[n][id[18446744073709551615llu]]; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 10, M = 1e6 + 7, K = 200, p = 1e9 + 7; int n, e[N], s[M], wf[K], q[N][K], f[N][N], ans[N][M]; bitset<40> op, wq[N][M]; unordered_map<bitset<40>, int> rt[N]; inline int read() { int num = 0; char g = getchar(); while (g < 48 || 57 < g) g = getchar(); while (47 < g && g < 58) num = (num << 1) + (num << 3) + g - 48, g = getchar(); return num; } inline long long pows(long long u, int v) { long long ans = 1; while (v > 0) { if (v & 1) ans = ans * u % p; u = u * u % p, v = v >> 1; } return ans; } inline void find(int u) { q[u][0] = 0; int os = 0, of = 0; for (int i = 0; i < (1 << n); i++) if (s[i] == u) q[u][++q[u][0]] = i, wf[i] = ++os; for (int i = 1; i <= e[u - 1]; i++) { if (!ans[u - 1][i]) continue; for (int h = 1; h < (1 << n); h++) { long long opq = 1; for (int c = 0; c < n; c++) if ((1 << c) & h) opq = opq * f[u][c + 1] % p; else opq = opq * (1 - f[u][c + 1] + p) % p; bitset<40> c = wq[u - 1][i], hc = op; bool tg = 0; for (int x = 1; x <= q[u - 1][0]; x++) { if (c[x] == 1) { for (int t = 0; t < n; t++) if (((1 << t) & h) && (!(q[u - 1][x] & (1 << t)))) { hc[wf[q[u - 1][x] | (1 << t)]] = 1, tg = 1; } } } if (!tg) continue; int pos; if (rt[u][hc] == 0) rt[u][hc] = ++of, e[u]++, wq[u][e[u]] = hc; pos = rt[u][hc]; ans[u][pos] = (ans[u][pos] + opq * ans[u - 1][i]) % p; } } } int main() { n = read(); for (int i = 0; i <= (1 << n); i++) s[i] = __builtin_popcount(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[i][j] = read() * pows(100, p - 2) % p; q[0][0] = 1; e[0] = 1, ans[0][1] = 1, wq[0][1][1] = 1; for (int i = 1; i <= n; i++) find(i); cout << ans[n][1] << endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 10, M = 1e6 + 7, K = 200, p = 1e9 + 7; int n, e[N], s[M], wf[K], q[N][K], f[N][N], ans[N][M]; bitset<40> op, wq[N][M]; unordered_map<bitset<40>, int> rt[N]; inline int read() { int num = 0; char g = getchar(); while (g < 48 || 57 < g) g = getchar(); while (47 < g && g < 58) num = (num << 1) + (num << 3) + g - 48, g = getchar(); return num; } inline long long pows(long long u, int v) { long long ans = 1; while (v > 0) { if (v & 1) ans = ans * u % p; u = u * u % p, v = v >> 1; } return ans; } inline void find(int u) { q[u][0] = 0; int os = 0, of = 0; for (int i = 0; i < (1 << n); i++) if (s[i] == u) q[u][++q[u][0]] = i, wf[i] = ++os; for (int i = 1; i <= e[u - 1]; i++) { if (!ans[u - 1][i]) continue; for (int h = 1; h < (1 << n); h++) { long long opq = 1; for (int c = 0; c < n; c++) if ((1 << c) & h) opq = opq * f[u][c + 1] % p; else opq = opq * (1 - f[u][c + 1] + p) % p; bitset<40> c = wq[u - 1][i], hc = op; bool tg = 0; for (int x = 1; x <= q[u - 1][0]; x++) { if (c[x] == 1) { for (int t = 0; t < n; t++) if (((1 << t) & h) && (!(q[u - 1][x] & (1 << t)))) { hc[wf[q[u - 1][x] | (1 << t)]] = 1, tg = 1; } } } if (!tg) continue; int pos; if (rt[u][hc] == 0) rt[u][hc] = ++of, e[u]++, wq[u][e[u]] = hc; pos = rt[u][hc]; ans[u][pos] = (ans[u][pos] + opq * ans[u - 1][i]) % p; } } } int main() { n = read(); for (int i = 0; i <= (1 << n); i++) s[i] = __builtin_popcount(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[i][j] = read() * pows(100, p - 2) % p; q[0][0] = 1; e[0] = 1, ans[0][1] = 1, wq[0][1][1] = 1; for (int i = 1; i <= n; i++) find(i); cout << ans[n][1] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; struct mod_int { int val; mod_int(long long v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int mod_inv(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } mod_int &operator+=(const mod_int &other) { val += other.val; if (val >= MOD) val -= MOD; return *this; } mod_int &operator-=(const mod_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } mod_int &operator*=(const mod_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); } friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; } friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; } friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; } friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; } mod_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } mod_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } mod_int operator++(int) { mod_int before = *this; ++*this; return before; } mod_int operator--(int) { mod_int before = *this; --*this; return before; } mod_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const mod_int &other) const { return val == other.val; } bool operator!=(const mod_int &other) const { return val != other.val; } mod_int inv() const { return mod_inv(val); } mod_int pow(long long p) const { assert(p >= 0); mod_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } }; const int N_MAX = 7; int N; mod_int probability[N_MAX][N_MAX]; int main() { cin >> N; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { int p; cin >> p; probability[i][j] = mod_int(p) / mod_int(100); } vector<vector<int>> masks_with_count(N_MAX + 1); vector<int> lookup(1 << N, -1); for (int mask = 0; mask < 1 << N; mask++) { int count = __builtin_popcount(mask); lookup[mask] = masks_with_count[count].size(); masks_with_count[count].push_back(mask); } map<long long, mod_int> possible_masks; possible_masks[1 << lookup[0]] = 1; for (int left = 0; left < N; left++) { map<long long, mod_int> new_possible_masks; vector<mod_int> products(1 << N, 1); for (int current = 0; current < 1 << N; current++) for (int right = 0; right < N; right++) products[current] *= (current >> right & 1) ? probability[left][right] : 1 - probability[left][right]; for (auto &p : possible_masks) { long long possible = p.first; mod_int prob = p.second; vector<long long> new_bits(N, 0); for (int mask : masks_with_count[left]) if (possible >> lookup[mask] & 1) for (int i = 0; i < N; i++) if ((mask >> i & 1) == 0) new_bits[i] |= 1LL << lookup[mask | 1 << i]; for (int current = 0; current < 1 << N; current++) { long long new_possible = 0; for (int prefix = current; prefix != 0; prefix &= prefix - 1) new_possible |= new_bits[__builtin_ctz(prefix)]; new_possible_masks[new_possible] += prob * products[current]; } } possible_masks = new_possible_masks; cerr << possible_masks.size() << endl; } mod_int answer = 0; for (auto &p : possible_masks) if (p.first >> lookup[(1 << N) - 1] & 1) answer += p.second; cout << (int)answer << '\n'; }
### Prompt Your task is to create a Cpp solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; struct mod_int { int val; mod_int(long long v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int mod_inv(int a, int m = MOD) { int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } mod_int &operator+=(const mod_int &other) { val += other.val; if (val >= MOD) val -= MOD; return *this; } mod_int &operator-=(const mod_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m)); return rem; } mod_int &operator*=(const mod_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); } friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; } friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; } friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; } friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; } mod_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } mod_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } mod_int operator++(int) { mod_int before = *this; ++*this; return before; } mod_int operator--(int) { mod_int before = *this; --*this; return before; } mod_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const mod_int &other) const { return val == other.val; } bool operator!=(const mod_int &other) const { return val != other.val; } mod_int inv() const { return mod_inv(val); } mod_int pow(long long p) const { assert(p >= 0); mod_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } }; const int N_MAX = 7; int N; mod_int probability[N_MAX][N_MAX]; int main() { cin >> N; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { int p; cin >> p; probability[i][j] = mod_int(p) / mod_int(100); } vector<vector<int>> masks_with_count(N_MAX + 1); vector<int> lookup(1 << N, -1); for (int mask = 0; mask < 1 << N; mask++) { int count = __builtin_popcount(mask); lookup[mask] = masks_with_count[count].size(); masks_with_count[count].push_back(mask); } map<long long, mod_int> possible_masks; possible_masks[1 << lookup[0]] = 1; for (int left = 0; left < N; left++) { map<long long, mod_int> new_possible_masks; vector<mod_int> products(1 << N, 1); for (int current = 0; current < 1 << N; current++) for (int right = 0; right < N; right++) products[current] *= (current >> right & 1) ? probability[left][right] : 1 - probability[left][right]; for (auto &p : possible_masks) { long long possible = p.first; mod_int prob = p.second; vector<long long> new_bits(N, 0); for (int mask : masks_with_count[left]) if (possible >> lookup[mask] & 1) for (int i = 0; i < N; i++) if ((mask >> i & 1) == 0) new_bits[i] |= 1LL << lookup[mask | 1 << i]; for (int current = 0; current < 1 << N; current++) { long long new_possible = 0; for (int prefix = current; prefix != 0; prefix &= prefix - 1) new_possible |= new_bits[__builtin_ctz(prefix)]; new_possible_masks[new_possible] += prob * products[current]; } } possible_masks = new_possible_masks; cerr << possible_masks.size() << endl; } mod_int answer = 0; for (auto &p : possible_masks) if (p.first >> lookup[(1 << N) - 1] & 1) answer += p.second; cout << (int)answer << '\n'; } ```
#include <bits/stdc++.h> using namespace std; const int N = 7, M = 20, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { return ((a + b) % MOD + MOD) % MOD; } void _sum(int &a, int b) { a = sum(a, b); } int mul(int a, int b) { return 1LL * a * b % MOD; } void _mul(int &a, int b) { a = mul(a, b); } int power(int a, int b) { int res = 1; while (b) { if (b & 1) _mul(res, a); _mul(a, a); b >>= 1; } return res; } int inv(int x) { return power(x, MOD - 2); } int n, f[1 << N], p[N][N], dp[N][1 << M], pro[N][1 << N]; vector<int> vec[N]; void input() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; _mul(p[i][j], inv(100)); } for (int mask = 0; mask < (1 << n); mask++) { pro[i][mask] = 1; for (int j = 0; j < n; j++) if (mask & (1 << j)) _mul(pro[i][mask], p[i][j]); else _mul(pro[i][mask], sum(1, -p[i][j])); } } } void pre_pro() { for (int i = 0; i < (1 << n); i++) { f[i] = vec[__builtin_popcount(i)].size(); vec[__builtin_popcount(i)].push_back(i); } } void act(int i, int mask, int nei) { int nxt = 0; vector<int> a, b; for (int j = 0; j < vec[i].size(); j++) if (mask & (1 << j)) a.push_back(vec[i][j]); for (int j = 0; j < n; j++) if (nei & (1 << j)) b.push_back(j); for (int x : a) for (int y : b) if ((x & (1 << y)) == false) nxt |= (1 << f[x | (1 << y)]); _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei])); } void solve() { dp[0][1] = 1; for (int i = 0; i < n; i++) for (int mask = 0; mask < (1 << vec[i].size()); mask++) if (dp[i][mask]) for (int nei = 0; nei < (1 << n); nei++) act(i, mask, nei); } int main() { ios::sync_with_stdio(false), cin.tie(0); input(); pre_pro(); solve(); cout << dp[n][1]; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 7, M = 20, MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { return ((a + b) % MOD + MOD) % MOD; } void _sum(int &a, int b) { a = sum(a, b); } int mul(int a, int b) { return 1LL * a * b % MOD; } void _mul(int &a, int b) { a = mul(a, b); } int power(int a, int b) { int res = 1; while (b) { if (b & 1) _mul(res, a); _mul(a, a); b >>= 1; } return res; } int inv(int x) { return power(x, MOD - 2); } int n, f[1 << N], p[N][N], dp[N][1 << M], pro[N][1 << N]; vector<int> vec[N]; void input() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) { cin >> p[i][j]; _mul(p[i][j], inv(100)); } for (int mask = 0; mask < (1 << n); mask++) { pro[i][mask] = 1; for (int j = 0; j < n; j++) if (mask & (1 << j)) _mul(pro[i][mask], p[i][j]); else _mul(pro[i][mask], sum(1, -p[i][j])); } } } void pre_pro() { for (int i = 0; i < (1 << n); i++) { f[i] = vec[__builtin_popcount(i)].size(); vec[__builtin_popcount(i)].push_back(i); } } void act(int i, int mask, int nei) { int nxt = 0; vector<int> a, b; for (int j = 0; j < vec[i].size(); j++) if (mask & (1 << j)) a.push_back(vec[i][j]); for (int j = 0; j < n; j++) if (nei & (1 << j)) b.push_back(j); for (int x : a) for (int y : b) if ((x & (1 << y)) == false) nxt |= (1 << f[x | (1 << y)]); _sum(dp[i + 1][nxt], mul(dp[i][mask], pro[i + 1][nei])); } void solve() { dp[0][1] = 1; for (int i = 0; i < n; i++) for (int mask = 0; mask < (1 << vec[i].size()); mask++) if (dp[i][mask]) for (int nei = 0; nei < (1 << n); nei++) act(i, mask, nei); } int main() { ios::sync_with_stdio(false), cin.tie(0); input(); pre_pro(); solve(); cout << dp[n][1]; return 0; } ```
#include <bits/stdc++.h> using namespace std; template <int MOD> struct ModInt { using Mint = ModInt; int val; ModInt(int64_t __val = 0) { fix(__val % MOD + MOD); } Mint& fix(int __val) { val = __val; if (val >= MOD) val -= MOD; return *this; } explicit operator int() { return val; } Mint operator+(const Mint& mt) const { return Mint().fix(val + mt.val); } Mint operator-(const Mint& mt) const { return Mint().fix(val - mt.val + MOD); } Mint operator*(const Mint& mt) const { return Mint().fix(int64_t(val) * mt.val % MOD + MOD); } Mint& operator+=(const Mint& mt) { return *this = *this + mt; } Mint& operator-=(const Mint& mt) { return *this = *this - mt; } Mint& operator*=(const Mint& mt) { return *this = *this * mt; } Mint pow(int y) const { Mint x = *this; Mint ans(1); for (; y > 0; y >>= 1, x *= x) { if (y & 1) ans *= x; } return ans; } Mint operator-() const { return Mint().fix(MOD - val); } Mint inv() const { return pow(MOD - 2); } Mint operator/(const Mint& mt) const { return *this * mt.inv(); } Mint& operator/=(const Mint& mt) const { return *this / mt; } friend ostream& operator<<(ostream& os, const Mint& mt) { return os << mt.val; } }; using Mint = ModInt<1000000007>; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<Mint>> p(6, vector<Mint>(6)); for (int i = 0; i < 6; ++i) { if (i < N) { for (int j = 0; j < N; ++j) { int _p; cin >> _p; p[i][j] = Mint(_p) / Mint(100); } } else p[i][i] = 1; } vector<Mint> lval(1 << 20); vector<Mint> rval(1 << 20); for (int mask = 0; mask < (1 << 18); ++mask) { Mint prob = 1; for (int z = 0; z < 18; ++z) { if (mask & (1 << z)) prob *= p[z / 6][z % 6]; else prob *= (Mint(1) - p[z / 6][z % 6]); } int idx = 0; int state = 0; for (int a = 0; a < 6; ++a) { for (int b = a + 1; b < 6; ++b) { for (int c = b + 1; c < 6; ++c) { bool good = false; int arr[3] = {a, b, c}; do { bool ok = true; for (int z : {arr[0], 6 + arr[1], 12 + arr[2]}) { if (!(mask & (1 << z))) ok = false; } if (ok) good = true; } while (next_permutation(arr, arr + 3)); if (good) state |= (1 << idx); idx++; } } } lval[state] += prob; } for (int mask = 0; mask < (1 << 18); ++mask) { Mint prob = 1; for (int z = 0; z < 18; ++z) if (mask & (1 << z)) { prob *= p[z / 6 + 3][z % 6]; } else prob *= (Mint(1) - p[z / 6 + 3][z % 6]); int idx = 0; int state = 0; for (int a = 0; a < 6; ++a) { for (int b = a + 1; b < 6; ++b) { for (int c = b + 1; c < 6; ++c) { bool good = false; int arr[3]; int sz = 0; for (int i = 0; i < 6; ++i) if (i != a && i != b && i != c) arr[sz++] = i; do { bool ok = true; for (int z : {arr[0], 6 + arr[1], 12 + arr[2]}) { if (!(mask & (1 << z))) ok = false; } if (ok) good = true; } while (next_permutation(arr, arr + 3)); if (good) state |= (1 << idx); idx++; } } } rval[state] += prob; } for (int i = 0; i < 20; ++i) { for (int j = 0; j < (1 << 20); ++j) if (!(j & (1 << i))) rval[j ^ (1 << i)] += rval[j]; } Mint ans = 1; for (int i = 0; i < (1 << 20); ++i) ans -= lval[i] * rval[((1 << 20) - 1) ^ i]; cout << ans << '\n'; return 0; }
### Prompt Develop a solution in cpp to the problem described below: This is an easier version of the problem. In this version, n ≀ 6. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer n and n^2 integers p_{ij} (1 ≀ i ≀ n, 1 ≀ j ≀ n). He then generates a random bipartite graph with 2n vertices. There are n vertices on the left side: β„“_1, β„“_2, ..., β„“_n, and n vertices on the right side: r_1, r_2, ..., r_n. For each i and j, he puts an edge between vertices β„“_i and r_j with probability p_{ij} percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as P/Q where P and Q are coprime integers and Q not≑ 0 \pmod{10^9+7}. Let Q^{-1} be an integer for which Q β‹… Q^{-1} ≑ 1 \pmod{10^9+7}. Print the value of P β‹… Q^{-1} modulo 10^9+7. Input The first line of the input contains a single integer n (1 ≀ n ≀ 6). The following n lines describe the probabilities of each edge appearing in the graph. The i-th of the lines contains n integers p_{i1}, p_{i2}, ..., p_{in} (0 ≀ p_{ij} ≀ 100); p_{ij} denotes the probability, in percent, of an edge appearing between β„“_i and r_j. Output Print a single integer β€” the probability that the perfect matching exists in the bipartite graph, written as P β‹… Q^{-1} \pmod{10^9+7} for P, Q defined above. Examples Input 2 50 50 50 50 Output 937500007 Input 3 3 1 4 1 5 9 2 6 5 Output 351284554 Note In the first sample test, each of the 16 graphs below is equally probable. Out of these, 7 have a perfect matching: <image> Therefore, the probability is equal to 7/16. As 16 β‹… 562 500 004 = 1 \pmod{10^9+7}, the answer to the testcase is 7 β‹… 562 500 004 mod{(10^9+7)} = 937 500 007. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <int MOD> struct ModInt { using Mint = ModInt; int val; ModInt(int64_t __val = 0) { fix(__val % MOD + MOD); } Mint& fix(int __val) { val = __val; if (val >= MOD) val -= MOD; return *this; } explicit operator int() { return val; } Mint operator+(const Mint& mt) const { return Mint().fix(val + mt.val); } Mint operator-(const Mint& mt) const { return Mint().fix(val - mt.val + MOD); } Mint operator*(const Mint& mt) const { return Mint().fix(int64_t(val) * mt.val % MOD + MOD); } Mint& operator+=(const Mint& mt) { return *this = *this + mt; } Mint& operator-=(const Mint& mt) { return *this = *this - mt; } Mint& operator*=(const Mint& mt) { return *this = *this * mt; } Mint pow(int y) const { Mint x = *this; Mint ans(1); for (; y > 0; y >>= 1, x *= x) { if (y & 1) ans *= x; } return ans; } Mint operator-() const { return Mint().fix(MOD - val); } Mint inv() const { return pow(MOD - 2); } Mint operator/(const Mint& mt) const { return *this * mt.inv(); } Mint& operator/=(const Mint& mt) const { return *this / mt; } friend ostream& operator<<(ostream& os, const Mint& mt) { return os << mt.val; } }; using Mint = ModInt<1000000007>; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<Mint>> p(6, vector<Mint>(6)); for (int i = 0; i < 6; ++i) { if (i < N) { for (int j = 0; j < N; ++j) { int _p; cin >> _p; p[i][j] = Mint(_p) / Mint(100); } } else p[i][i] = 1; } vector<Mint> lval(1 << 20); vector<Mint> rval(1 << 20); for (int mask = 0; mask < (1 << 18); ++mask) { Mint prob = 1; for (int z = 0; z < 18; ++z) { if (mask & (1 << z)) prob *= p[z / 6][z % 6]; else prob *= (Mint(1) - p[z / 6][z % 6]); } int idx = 0; int state = 0; for (int a = 0; a < 6; ++a) { for (int b = a + 1; b < 6; ++b) { for (int c = b + 1; c < 6; ++c) { bool good = false; int arr[3] = {a, b, c}; do { bool ok = true; for (int z : {arr[0], 6 + arr[1], 12 + arr[2]}) { if (!(mask & (1 << z))) ok = false; } if (ok) good = true; } while (next_permutation(arr, arr + 3)); if (good) state |= (1 << idx); idx++; } } } lval[state] += prob; } for (int mask = 0; mask < (1 << 18); ++mask) { Mint prob = 1; for (int z = 0; z < 18; ++z) if (mask & (1 << z)) { prob *= p[z / 6 + 3][z % 6]; } else prob *= (Mint(1) - p[z / 6 + 3][z % 6]); int idx = 0; int state = 0; for (int a = 0; a < 6; ++a) { for (int b = a + 1; b < 6; ++b) { for (int c = b + 1; c < 6; ++c) { bool good = false; int arr[3]; int sz = 0; for (int i = 0; i < 6; ++i) if (i != a && i != b && i != c) arr[sz++] = i; do { bool ok = true; for (int z : {arr[0], 6 + arr[1], 12 + arr[2]}) { if (!(mask & (1 << z))) ok = false; } if (ok) good = true; } while (next_permutation(arr, arr + 3)); if (good) state |= (1 << idx); idx++; } } } rval[state] += prob; } for (int i = 0; i < 20; ++i) { for (int j = 0; j < (1 << 20); ++j) if (!(j & (1 << i))) rval[j ^ (1 << i)] += rval[j]; } Mint ans = 1; for (int i = 0; i < (1 << 20); ++i) ans -= lval[i] * rval[((1 << 20) - 1) ^ i]; cout << ans << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int ans = 0; void gen(vector<int> &A, vector<vector<int>> &G, int i) { if (i < G.size()) { for (int j = 0; j < 6; ++j) { A[i] = j; gen(A, G, i + 1); } } else { bool was[6][6]; for (int a = 0; a < 6; ++a) { for (int b = 0; b < 6; ++b) was[a][b] = false; } for (int a = 0; a < G.size(); ++a) { for (int b : G[a]) { was[A[a]][A[b]] = true; was[A[b]][A[a]] = true; } } int res = 0; for (int a = 0; a < 6; ++a) { for (int b = 0; b <= a; ++b) res += was[a][b]; } ans = max(ans, res); } } int main() { int n, m; cin >> n >> m; vector<vector<int>> G(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; G[a].emplace_back(b); G[b].emplace_back(a); } vector<int> A(n); gen(A, G, 0); cout << ans; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int ans = 0; void gen(vector<int> &A, vector<vector<int>> &G, int i) { if (i < G.size()) { for (int j = 0; j < 6; ++j) { A[i] = j; gen(A, G, i + 1); } } else { bool was[6][6]; for (int a = 0; a < 6; ++a) { for (int b = 0; b < 6; ++b) was[a][b] = false; } for (int a = 0; a < G.size(); ++a) { for (int b : G[a]) { was[A[a]][A[b]] = true; was[A[b]][A[a]] = true; } } int res = 0; for (int a = 0; a < 6; ++a) { for (int b = 0; b <= a; ++b) res += was[a][b]; } ans = max(ans, res); } } int main() { int n, m; cin >> n >> m; vector<vector<int>> G(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; G[a].emplace_back(b); G[b].emplace_back(a); } vector<int> A(n); gen(A, G, 0); cout << ans; return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MAXN = 10, INF = 1e9, MAXM = 30; int n, m; int ans = 0; int used[MAXN][MAXN], c[MAXN]; pair<int, int> e[MAXM]; void check() { int cnt = 0; for (int i = 0; i < m; ++i) { if (used[c[e[i].first]][c[e[i].second]]) continue; used[c[e[i].first]][c[e[i].second]] = used[c[e[i].second]][c[e[i].first]] = 1; ++cnt; } for (int i = 0; i < MAXN; ++i) for (int j = 0; j < MAXN; ++j) used[i][j] = 0; ans = max(ans, cnt); } void kek(int i) { if (i == n) { check(); return; } for (int j = 1; j <= 6; ++j) { c[i] = j; kek(i + 1); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> e[i].first >> e[i].second; e[i].first--; e[i].second--; } kek(0); cout << ans; return 0; }
### Prompt Create a solution in CPP for the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MAXN = 10, INF = 1e9, MAXM = 30; int n, m; int ans = 0; int used[MAXN][MAXN], c[MAXN]; pair<int, int> e[MAXM]; void check() { int cnt = 0; for (int i = 0; i < m; ++i) { if (used[c[e[i].first]][c[e[i].second]]) continue; used[c[e[i].first]][c[e[i].second]] = used[c[e[i].second]][c[e[i].first]] = 1; ++cnt; } for (int i = 0; i < MAXN; ++i) for (int j = 0; j < MAXN; ++j) used[i][j] = 0; ans = max(ans, cnt); } void kek(int i) { if (i == n) { check(); return; } for (int j = 1; j <= 6; ++j) { c[i] = j; kek(i + 1); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> e[i].first >> e[i].second; e[i].first--; e[i].second--; } kek(0); cout << ans; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> v[10]; int n, ans, m, tmp; vector<int> v2; void recur(int now) { if (now == n) { int vis[7][7]; memset(vis, 0, sizeof(vis)); int ans2 = 0; for (int e = 1; e <= n; e++) { for (int p = 0; p < v[e].size(); p++) { int next = v[e][p]; int f = v2[e - 1], s = v2[next - 1]; if (f > s) swap(f, s); if (vis[f][s] == 0) { vis[f][s] = 1; ans2++; } } } ans = max(ans, ans2); } else { for (int p = 1; p <= min(n, 6); p++) { v2.push_back(p); recur(now + 1); v2.pop_back(); } } } int main(void) { scanf("%d%d", &n, &m); for (int e = 0; e < m; e++) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } recur(0); printf("%d", ans); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> v[10]; int n, ans, m, tmp; vector<int> v2; void recur(int now) { if (now == n) { int vis[7][7]; memset(vis, 0, sizeof(vis)); int ans2 = 0; for (int e = 1; e <= n; e++) { for (int p = 0; p < v[e].size(); p++) { int next = v[e][p]; int f = v2[e - 1], s = v2[next - 1]; if (f > s) swap(f, s); if (vis[f][s] == 0) { vis[f][s] = 1; ans2++; } } } ans = max(ans, ans2); } else { for (int p = 1; p <= min(n, 6); p++) { v2.push_back(p); recur(now + 1); v2.pop_back(); } } } int main(void) { scanf("%d%d", &n, &m); for (int e = 0; e < m; e++) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } recur(0); printf("%d", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> vec; bool linked(int a, int b) { for (pair<int, int> p : vec) { if ((p.first == a && p.second == b) || (p.first == b && p.second == a)) { return true; } } return false; } int main() { int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; vec.emplace_back(pair<int, int>(x, y)); } int minimum = 7; for (int a = 1; a <= 7; ++a) { for (int b = a + 1; b <= 7; ++b) { int match = 0; for (int i = 1; i <= 7; ++i) { if (linked(a, i) && linked(b, i)) { ++match; } } if (match < minimum) { minimum = match; } } } cout << m - minimum << endl; }
### Prompt Create a solution in cpp for the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<pair<int, int>> vec; bool linked(int a, int b) { for (pair<int, int> p : vec) { if ((p.first == a && p.second == b) || (p.first == b && p.second == a)) { return true; } } return false; } int main() { int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; vec.emplace_back(pair<int, int>(x, y)); } int minimum = 7; for (int a = 1; a <= 7; ++a) { for (int b = a + 1; b <= 7; ++b) { int match = 0; for (int i = 1; i <= 7; ++i) { if (linked(a, i) && linked(b, i)) { ++match; } } if (match < minimum) { minimum = match; } } } cout << m - minimum << endl; } ```
#include <bits/stdc++.h> using namespace std; inline int mul(int a, int b) { return int(a * 1ll * b % 998244353); } inline int norm(int a) { if (a >= 998244353) a -= 998244353; if (a < 0) a += 998244353; return a; } inline int binPow(int a, int k) { int ans = 1; while (k > 0) { if (k & 1) ans = mul(ans, a); a = mul(a, a); k >>= 1; } return ans; } inline int subtract(int a, int b) { return norm(a - b); } inline int sum(int a, int b) { return int((a + b) % 998244353); } inline int inv(int a) { return binPow(a, 998244353 - 2); } long long ceil_int(long long a, long long b) { return a / b + (a % b != 0); } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } int n, m; vector<vector<int>> g; vector<int> stp; int main() { cin >> n >> m; g.resize(n); stp.resize(n); vector<int> used(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); stp[a]++; stp[b]++; } if (m == 0) { cout << 0; return 0; } vector<vector<int>> d(6, vector<int>(6)); int res = 0; vector<int> color(n); int change = 0; for (int i = 0; i < pow(6, n); i++) { int cur = 0; if (change) { for (int j = n - 1; j >= 0; j--) { if (color[j] == 5) { color[j] = 0; } else { color[j]++; break; } } } for (int j = 0; j < n; j++) { for (int t = 0; t < g[j].size(); t++) { int to = g[j][t]; if (d[color[j]][color[to]] == 0) { cur++; d[color[j]][color[to]] = 1; d[color[to]][color[j]] = 1; } } } change = 1; res = max(res, cur); for (int j = 0; j < 6; j++) { for (int t = 0; t < 6; t++) { d[j][t] = 0; } } } cout << res; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int mul(int a, int b) { return int(a * 1ll * b % 998244353); } inline int norm(int a) { if (a >= 998244353) a -= 998244353; if (a < 0) a += 998244353; return a; } inline int binPow(int a, int k) { int ans = 1; while (k > 0) { if (k & 1) ans = mul(ans, a); a = mul(a, a); k >>= 1; } return ans; } inline int subtract(int a, int b) { return norm(a - b); } inline int sum(int a, int b) { return int((a + b) % 998244353); } inline int inv(int a) { return binPow(a, 998244353 - 2); } long long ceil_int(long long a, long long b) { return a / b + (a % b != 0); } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } int n, m; vector<vector<int>> g; vector<int> stp; int main() { cin >> n >> m; g.resize(n); stp.resize(n); vector<int> used(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); stp[a]++; stp[b]++; } if (m == 0) { cout << 0; return 0; } vector<vector<int>> d(6, vector<int>(6)); int res = 0; vector<int> color(n); int change = 0; for (int i = 0; i < pow(6, n); i++) { int cur = 0; if (change) { for (int j = n - 1; j >= 0; j--) { if (color[j] == 5) { color[j] = 0; } else { color[j]++; break; } } } for (int j = 0; j < n; j++) { for (int t = 0; t < g[j].size(); t++) { int to = g[j][t]; if (d[color[j]][color[to]] == 0) { cur++; d[color[j]][color[to]] = 1; d[color[to]][color[j]] = 1; } } } change = 1; res = max(res, cur); for (int j = 0; j < 6; j++) { for (int t = 0; t < 6; t++) { d[j][t] = 0; } } } cout << res; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 10; int n, m, res, cnt[maxn]; bool e[maxn][maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; int u, v; memset(e, 0, sizeof(e)); for (int i = 1; i <= m; i++) { cin >> u >> v; e[u][v] = 1; e[v][u] = 1; cnt[u]++; cnt[v]++; } if (n < 7) { cout << m; return 0; } for (int i = 1; i <= 7; i++) if (cnt[i] == 0) { cout << m; return 0; } int res = 0; for (int i = 1; i <= 6; i++) for (int j = i + 1; j <= 7; j++) { int count = 0; for (int k = 1; k <= 7; k++) if (e[i][k] && e[k][j]) count++; res = max(res, max(m - cnt[j] + min(7 - cnt[i], cnt[j] - count), m - cnt[i] + min(7 - cnt[j], cnt[i] - count))); } cout << res; return 0; }
### Prompt Generate a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 10; int n, m, res, cnt[maxn]; bool e[maxn][maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; int u, v; memset(e, 0, sizeof(e)); for (int i = 1; i <= m; i++) { cin >> u >> v; e[u][v] = 1; e[v][u] = 1; cnt[u]++; cnt[v]++; } if (n < 7) { cout << m; return 0; } for (int i = 1; i <= 7; i++) if (cnt[i] == 0) { cout << m; return 0; } int res = 0; for (int i = 1; i <= 6; i++) for (int j = i + 1; j <= 7; j++) { int count = 0; for (int k = 1; k <= 7; k++) if (e[i][k] && e[k][j]) count++; res = max(res, max(m - cnt[j] + min(7 - cnt[i], cnt[j] - count), m - cnt[i] + min(7 - cnt[j], cnt[i] - count))); } cout << res; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int> > g(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } if (n < 7) { cout << m; } else { int best = 7; for (int i = 0; i < 7; ++i) { for (int j = i + 1; j < 7; ++j) { int x = 0; for (int k = 0; k < g[i].size(); k++) { for (int h = 0; h < g[j].size(); h++) { if (g[i][k] == g[j][h]) { x++; } } } if (x < best) { best = x; } } } cout << m - best; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int> > g(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } if (n < 7) { cout << m; } else { int best = 7; for (int i = 0; i < 7; ++i) { for (int j = i + 1; j < 7; ++j) { int x = 0; for (int k = 0; k < g[i].size(); k++) { for (int h = 0; h < g[j].size(); h++) { if (g[i][k] == g[j][h]) { x++; } } } if (x < best) { best = x; } } } cout << m - best; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, a, b; int graph[8][8]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n >> m; if (m <= 6) { cout << m << '\n'; return 0; } for (int i = 0; i < m; i++) { cin >> a >> b; graph[a][b] = graph[b][a] = 1; } int ans = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { if (i == j) continue; int cnt = 0; for (int k = 1; k <= 7; k++) cnt += (graph[i][k] & graph[j][k]); ans = min(ans, cnt); } } cout << m - ans << '\n'; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, a, b; int graph[8][8]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n >> m; if (m <= 6) { cout << m << '\n'; return 0; } for (int i = 0; i < m; i++) { cin >> a >> b; graph[a][b] = graph[b][a] = 1; } int ans = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { if (i == j) continue; int cnt = 0; for (int k = 1; k <= 7; k++) cnt += (graph[i][k] & graph[j][k]); ans = min(ans, cnt); } } cout << m - ans << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mx = 17; bool g[mx][mx]; int main() { int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a][b] = g[b][a] = true; } int mn = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { int x = 0; for (int k = 1; k <= 7; k++) if (g[i][k] && g[k][j]) x++; mn = min(mn, x); } } cout << m - mn << "\n"; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mx = 17; bool g[mx][mx]; int main() { int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a][b] = g[b][a] = true; } int mn = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { int x = 0; for (int k = 1; k <= 7; k++) if (g[i][k] && g[k][j]) x++; mn = min(mn, x); } } cout << m - mn << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; char G[8][8]; int main() { int n, m; cin >> n >> m; if (n < 7) { cout << m; return 0; } for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; G[x][y] = G[y][x] = 1; } int best = 69; for (int i = 1; i <= 7; i++) for (int j = 1; j <= 7; j++) if (j != i) { int alt = 0; for (int k = 1; k <= 7; k++) alt += G[i][k] & G[j][k]; best = min(best, alt); } cout << m - best; }
### Prompt Your task is to create a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char G[8][8]; int main() { int n, m; cin >> n >> m; if (n < 7) { cout << m; return 0; } for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; G[x][y] = G[y][x] = 1; } int best = 69; for (int i = 1; i <= 7; i++) for (int j = 1; j <= 7; j++) if (j != i) { int alt = 0; for (int k = 1; k <= 7; k++) alt += G[i][k] & G[j][k]; best = min(best, alt); } cout << m - best; } ```
#include <bits/stdc++.h> using namespace std; bool comp(pair<long long, long long> l, pair<long long, long long> m) { return l.second < m.second; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cout << fixed << setprecision(0); ; long long n, m; cin >> n >> m; long long v, u; long long ct[n]; vector<long long> g[n]; memset(ct, 0, sizeof(ct)); for (long long i = 0; i < m && cin >> v >> u; i++) v--, u--, g[v].push_back(u), g[u].push_back(v); if (n < 7) return cout << m, 0; long long mn = 7; for (long long i = 0; i < n; i++) for (long long j = i + 1; j < n; j++) { set<long long> edges; for (long long k = 0; k < g[i].size(); k++) edges.insert(g[i][k]); for (long long k = 0; k < g[j].size(); k++) edges.insert(g[j][k]); long long k = g[i].size() + g[j].size() - edges.size(); mn = min(mn, k); } cout << m - mn; }
### Prompt Please formulate a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool comp(pair<long long, long long> l, pair<long long, long long> m) { return l.second < m.second; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cout << fixed << setprecision(0); ; long long n, m; cin >> n >> m; long long v, u; long long ct[n]; vector<long long> g[n]; memset(ct, 0, sizeof(ct)); for (long long i = 0; i < m && cin >> v >> u; i++) v--, u--, g[v].push_back(u), g[u].push_back(v); if (n < 7) return cout << m, 0; long long mn = 7; for (long long i = 0; i < n; i++) for (long long j = i + 1; j < n; j++) { set<long long> edges; for (long long k = 0; k < g[i].size(); k++) edges.insert(g[i][k]); for (long long k = 0; k < g[j].size(); k++) edges.insert(g[j][k]); long long k = g[i].size() + g[j].size() - edges.size(); mn = min(mn, k); } cout << m - mn; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } if (n <= 6 || m == 0) { cout << m << endl; return 0; } int sum = 0; for (int i = 1; i < 7; i++) { for (int j = i + 1; j <= 7; j++) { vector<bool> vis(8, false); int cnt = 0, u = 0, v = 0; for (; u < adj[i].size(); u++) vis[adj[i][u]] = true; for (; v < adj[j].size(); v++) if (!vis[adj[j][v]]) vis[adj[j][v]] = true; for (int k = 1; k <= 7; k++) if (vis[k]) cnt++; sum = max(sum, m - u - v + cnt); } } cout << sum << endl; }
### Prompt Develop a solution in Cpp to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } if (n <= 6 || m == 0) { cout << m << endl; return 0; } int sum = 0; for (int i = 1; i < 7; i++) { for (int j = i + 1; j <= 7; j++) { vector<bool> vis(8, false); int cnt = 0, u = 0, v = 0; for (; u < adj[i].size(); u++) vis[adj[i][u]] = true; for (; v < adj[j].size(); v++) if (!vis[adj[j][v]]) vis[adj[j][v]] = true; for (int k = 1; k <= 7; k++) if (vis[k]) cnt++; sum = max(sum, m - u - v + cnt); } } cout << sum << endl; } ```
#include <bits/stdc++.h> using namespace std; void set_program() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int MAXN = 7; const int INF = 1e9; int n, m; int gr[MAXN][MAXN]; int col[6]; int res[6][6]; int ans; void set_var() { ans = 0; for (int i = 0; i < MAXN; ++i) { for (int j = 0; j < MAXN; ++j) { gr[i][j] = 0; } } for (int i = 0; i < 6; ++i) { col[i] = -1; } for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { res[i][j] = 0; } } } void relax() { for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { res[i][j] = 0; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (gr[i][j]) { res[col[i]][col[j]] = res[col[j]][col[i]] = 1; } } } int sum_double = 0, sum = 0; for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { if (i == j) { sum += res[i][j]; } else { sum_double += res[i][j]; } } } int result = sum + sum_double / 2; ans = max(ans, result); } void gen(int len) { if (len == n) { relax(); return; } for (int i = 0; i < 6; ++i) { col[len] = i; gen(len + 1); } } int main() { set_program(); set_var(); cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a, --b; gr[a][b] = gr[b][a] = 1; } gen(0); cout << ans << "\n"; return 0; }
### Prompt In Cpp, your task is to solve the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void set_program() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int MAXN = 7; const int INF = 1e9; int n, m; int gr[MAXN][MAXN]; int col[6]; int res[6][6]; int ans; void set_var() { ans = 0; for (int i = 0; i < MAXN; ++i) { for (int j = 0; j < MAXN; ++j) { gr[i][j] = 0; } } for (int i = 0; i < 6; ++i) { col[i] = -1; } for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { res[i][j] = 0; } } } void relax() { for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { res[i][j] = 0; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (gr[i][j]) { res[col[i]][col[j]] = res[col[j]][col[i]] = 1; } } } int sum_double = 0, sum = 0; for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { if (i == j) { sum += res[i][j]; } else { sum_double += res[i][j]; } } } int result = sum + sum_double / 2; ans = max(ans, result); } void gen(int len) { if (len == n) { relax(); return; } for (int i = 0; i < 6; ++i) { col[len] = i; gen(len + 1); } } int main() { set_program(); set_var(); cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a, --b; gr[a][b] = gr[b][a] = 1; } gen(0); cout << ans << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1005; int hs[10] = {0}; int save[N][2]; int ans = 0, n, m; struct node { int x, i; } deg[10]; int main() { cin >> n >> m; for (int i = 0; i < 10; i++) { deg[i].x = 0; deg[i].i = i; } for (int i = 1; i <= m; i++) { cin >> save[i][0] >> save[i][1]; if (save[i][0] != 7 && save[i][1] != 7) deg[save[i][0]].x++; else deg[save[i][0]].x++, deg[save[i][1]].x++; } sort(deg + 1, deg + 8, [](node a, node b) { return a.x > b.x; }); for (int i = 1; i <= 6; i++) hs[deg[i].i] = i; for (int pos = 1; pos <= 6; pos++) { hs[deg[7].i] = pos; set<pair<int, int>> sp; for (int i = 1; i <= m; i++) { sp.insert(make_pair(min(hs[save[i][0]], hs[save[i][1]]), max(hs[save[i][0]], hs[save[i][1]]))); } ans = max(ans, (int)sp.size()); } for (int i = 1; i <= m; i++) { if (save[i][0] > save[i][1]) swap(save[i][0], save[i][1]); if (save[i][0] != 7 && save[i][1] != 7) deg[save[i][0]].x++; else deg[save[i][0]].x++, deg[save[i][1]].x++; } sort(deg + 1, deg + 8, [](node a, node b) { return a.x > b.x; }); for (int i = 1; i <= 6; i++) hs[deg[i].i] = i; for (int pos = 1; pos <= 6; pos++) { hs[deg[7].i] = pos; set<pair<int, int>> sp; for (int i = 1; i <= m; i++) { sp.insert(make_pair(min(hs[save[i][0]], hs[save[i][1]]), max(hs[save[i][0]], hs[save[i][1]]))); } ans = max(ans, (int)sp.size()); } cout << ans << endl; }
### Prompt Construct a Cpp code solution to the problem outlined: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1005; int hs[10] = {0}; int save[N][2]; int ans = 0, n, m; struct node { int x, i; } deg[10]; int main() { cin >> n >> m; for (int i = 0; i < 10; i++) { deg[i].x = 0; deg[i].i = i; } for (int i = 1; i <= m; i++) { cin >> save[i][0] >> save[i][1]; if (save[i][0] != 7 && save[i][1] != 7) deg[save[i][0]].x++; else deg[save[i][0]].x++, deg[save[i][1]].x++; } sort(deg + 1, deg + 8, [](node a, node b) { return a.x > b.x; }); for (int i = 1; i <= 6; i++) hs[deg[i].i] = i; for (int pos = 1; pos <= 6; pos++) { hs[deg[7].i] = pos; set<pair<int, int>> sp; for (int i = 1; i <= m; i++) { sp.insert(make_pair(min(hs[save[i][0]], hs[save[i][1]]), max(hs[save[i][0]], hs[save[i][1]]))); } ans = max(ans, (int)sp.size()); } for (int i = 1; i <= m; i++) { if (save[i][0] > save[i][1]) swap(save[i][0], save[i][1]); if (save[i][0] != 7 && save[i][1] != 7) deg[save[i][0]].x++; else deg[save[i][0]].x++, deg[save[i][1]].x++; } sort(deg + 1, deg + 8, [](node a, node b) { return a.x > b.x; }); for (int i = 1; i <= 6; i++) hs[deg[i].i] = i; for (int pos = 1; pos <= 6; pos++) { hs[deg[7].i] = pos; set<pair<int, int>> sp; for (int i = 1; i <= m; i++) { sp.insert(make_pair(min(hs[save[i][0]], hs[save[i][1]]), max(hs[save[i][0]], hs[save[i][1]]))); } ans = max(ans, (int)sp.size()); } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { int m, n; int connect[30][30]; int a, b; memset(connect, 0, sizeof connect); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; connect[a][b] = 1; connect[b][a] = 1; } if (n <= 6) printf("%d\n", m); else { int ans = 999999; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { int sum = 0; for (int k = 1; k <= n; k++) { if (connect[i][k] && connect[j][k]) sum++; } ans = min(sum, ans); } } printf("%d\n", m - ans); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int m, n; int connect[30][30]; int a, b; memset(connect, 0, sizeof connect); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; connect[a][b] = 1; connect[b][a] = 1; } if (n <= 6) printf("%d\n", m); else { int ans = 999999; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { int sum = 0; for (int k = 1; k <= n; k++) { if (connect[i][k] && connect[j][k]) sum++; } ans = min(sum, ans); } } printf("%d\n", m - ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[8][8]; int n, m, ans = 0x3f3f3f3f; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); a[u][v] = 1, a[v][u] = 1; } if (n <= 6) { printf("%d\n", m); return 0; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { int cur = 0; for (int k = 1; k <= n; ++k) if (a[k][i] && a[k][j]) ++cur; ans = min(ans, cur); } printf("%d\n", m - ans); return 0; }
### Prompt Create a solution in Cpp for the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[8][8]; int n, m, ans = 0x3f3f3f3f; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); a[u][v] = 1, a[v][u] = 1; } if (n <= 6) { printf("%d\n", m); return 0; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { int cur = 0; for (int k = 1; k <= n; ++k) if (a[k][i] && a[k][j]) ++cur; ans = min(ans, cur); } printf("%d\n", m - ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool G[8][8]; int main() { int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u][v] = G[v][u] = true; } int mini = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { if (j == i) continue; int cur = 0; for (int k = 1; k <= 7; k++) cur += G[i][k] & G[j][k]; mini = min(mini, cur); } } cout << m - mini << endl; }
### Prompt Your challenge is to write a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool G[8][8]; int main() { int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u][v] = G[v][u] = true; } int mini = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { if (j == i) continue; int cur = 0; for (int k = 1; k <= 7; k++) cur += G[i][k] & G[j][k]; mini = min(mini, cur); } } cout << m - mini << endl; } ```
#include <bits/stdc++.h> using namespace std; int n, m, i, it, r, x[55], y[55], a[9], was[7][7]; void rec(int l) { if (l > n) { ++it; int cnt = 0; for (int i = 0; i < m; i++) { int le = min(a[x[i]], a[y[i]]); int ri = max(a[x[i]], a[y[i]]); if (was[le][ri] != it) { was[le][ri] = it; cnt++; } } r = max(r, cnt); return; } for (int i = 1; i <= 6; i++) { a[l] = i; rec(l + 1); } } int main() { scanf("%d%d", &n, &m); for (i = 0; i < m; i++) scanf("%d%d", &x[i], &y[i]); rec(1); printf("%d\n", r); return 0; }
### Prompt Develop a solution in cpp to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, i, it, r, x[55], y[55], a[9], was[7][7]; void rec(int l) { if (l > n) { ++it; int cnt = 0; for (int i = 0; i < m; i++) { int le = min(a[x[i]], a[y[i]]); int ri = max(a[x[i]], a[y[i]]); if (was[le][ri] != it) { was[le][ri] = it; cnt++; } } r = max(r, cnt); return; } for (int i = 1; i <= 6; i++) { a[l] = i; rec(l + 1); } } int main() { scanf("%d%d", &n, &m); for (i = 0; i < m; i++) scanf("%d%d", &x[i], &y[i]); rec(1); printf("%d\n", r); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } if (n <= 6 || m == 0) { cout << m << endl; return 0; } int sum = 0; for (int i = 1; i < 7; i++) { for (int j = i + 1; j <= 7; j++) { vector<bool> vis(8, false); int cnt = 0, u = 0, v = 0; for (; u < adj[i].size(); u++) vis[adj[i][u]] = true; for (; v < adj[j].size(); v++) if (!vis[adj[j][v]]) vis[adj[j][v]] = true; for (int k = 1; k <= 7; k++) if (vis[k]) cnt++; sum = max(sum, m - u - v + cnt); } } cout << sum << endl; }
### Prompt Construct a Cpp code solution to the problem outlined: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } if (n <= 6 || m == 0) { cout << m << endl; return 0; } int sum = 0; for (int i = 1; i < 7; i++) { for (int j = i + 1; j <= 7; j++) { vector<bool> vis(8, false); int cnt = 0, u = 0, v = 0; for (; u < adj[i].size(); u++) vis[adj[i][u]] = true; for (; v < adj[j].size(); v++) if (!vis[adj[j][v]]) vis[adj[j][v]] = true; for (int k = 1; k <= 7; k++) if (vis[k]) cnt++; sum = max(sum, m - u - v + cnt); } } cout << sum << endl; } ```
#include <bits/stdc++.h> using namespace std; void solve() { long long n, t, a, b; scanf("%lld %lld %lld %lld", &n, &t, &a, &b); vector<pair<long long, long long> > v(n); vector<int> h(n); int cnte = 0; int cnth = 0; for (int i = 0; i < n; i++) { scanf("%d", &h[i]); if (h[i]) { cnth++; } else cnte++; } for (int i = 0; i < n; i++) { scanf("%lld", &v[i].first); v[i].second = h[i]; } v.push_back({t + 1, 0}); sort(v.begin(), v.end()); long long cnt1 = 0; long long cnt2 = 0; long long ans = 0; for (int i = 0; i <= n; i++) { long long need = cnt1 * a + cnt2 * b; long long has = v[i].first - 1 - need; if (has >= 0) { long long cane = min((cnte - cnt1), has / a); has -= cane * a; long long canh = min((cnth - cnt2), has / b); ans = max(ans, cnt1 + cnt2 + cane + canh); } int l = i; while (l < v.size() && v[l].first == v[i].first) { if (v[l].second) { cnt2++; } else { cnt1++; } l++; } i = l - 1; } printf("%lld\n", ans); } int g[8][8]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); g[u][v] = g[v][u] = 1; } if (n <= 6) { printf("%d", m); } else { int ans = INT_MAX; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int res = 0; for (int k = 1; k <= n; k++) { if (g[i][k] && g[k][j]) res++; } ans = min(ans, res); } } printf("%d", m - ans); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { long long n, t, a, b; scanf("%lld %lld %lld %lld", &n, &t, &a, &b); vector<pair<long long, long long> > v(n); vector<int> h(n); int cnte = 0; int cnth = 0; for (int i = 0; i < n; i++) { scanf("%d", &h[i]); if (h[i]) { cnth++; } else cnte++; } for (int i = 0; i < n; i++) { scanf("%lld", &v[i].first); v[i].second = h[i]; } v.push_back({t + 1, 0}); sort(v.begin(), v.end()); long long cnt1 = 0; long long cnt2 = 0; long long ans = 0; for (int i = 0; i <= n; i++) { long long need = cnt1 * a + cnt2 * b; long long has = v[i].first - 1 - need; if (has >= 0) { long long cane = min((cnte - cnt1), has / a); has -= cane * a; long long canh = min((cnth - cnt2), has / b); ans = max(ans, cnt1 + cnt2 + cane + canh); } int l = i; while (l < v.size() && v[l].first == v[i].first) { if (v[l].second) { cnt2++; } else { cnt1++; } l++; } i = l - 1; } printf("%lld\n", ans); } int g[8][8]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); g[u][v] = g[v][u] = 1; } if (n <= 6) { printf("%d", m); } else { int ans = INT_MAX; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int res = 0; for (int k = 1; k <= n; k++) { if (g[i][k] && g[k][j]) res++; } ans = min(ans, res); } } printf("%d", m - ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int m, n; int connect[10][10]; int a, b; memset(connect, 0, sizeof connect); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; connect[a][b] = 1; connect[b][a] = 1; } if (n <= 6) printf("%d\n", m); else { int ans = 99999; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { int sum = 0; for (int k = 1; k <= n; k++) { if (connect[i][k] && connect[j][k]) sum++; } ans = min(sum, ans); } } printf("%d\n", m - ans); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int m, n; int connect[10][10]; int a, b; memset(connect, 0, sizeof connect); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; connect[a][b] = 1; connect[b][a] = 1; } if (n <= 6) printf("%d\n", m); else { int ans = 99999; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { int sum = 0; for (int k = 1; k <= n; k++) { if (connect[i][k] && connect[j][k]) sum++; } ans = min(sum, ans); } } printf("%d\n", m - ans); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a[8][8], n, m, u, v, k = INT_MAX; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n >> m; for (long long i = 0; i < m; i++) { cin >> u >> v; a[u][v] = 1; a[v][u] = 1; } if (n <= 6) cout << m << "\n"; else { for (long long i = 1; i <= 7; i++) { for (long long j = i + 1; j <= 7; j++) { long long c = 0; for (long long l = 1; l <= 7; l++) if (a[i][l] == 1 && a[j][l] == 1) ++c; k = min(k, c); } } cout << m - k << "\n"; } return 0; }
### Prompt Create a solution in cpp for the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a[8][8], n, m, u, v, k = INT_MAX; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n >> m; for (long long i = 0; i < m; i++) { cin >> u >> v; a[u][v] = 1; a[v][u] = 1; } if (n <= 6) cout << m << "\n"; else { for (long long i = 1; i <= 7; i++) { for (long long j = i + 1; j <= 7; j++) { long long c = 0; for (long long l = 1; l <= 7; l++) if (a[i][l] == 1 && a[j][l] == 1) ++c; k = min(k, c); } } cout << m - k << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 10, M = 110; long long n, m, ans, a[N], x[M], y[M], b[N][N]; inline long long read() { long long x = 0, tmp = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') tmp = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return tmp * x; } inline long long check() { memset(b, 0, sizeof(b)); long long ans = 0; for (long long i = 1; i <= m; i++) { if (!b[a[x[i]]][a[y[i]]]) { b[a[x[i]]][a[y[i]]] = b[a[y[i]]][a[x[i]]] = 1; ans++; } } return ans; } void dfs(long long x) { if (x == n + 1) { ans = max(ans, check()); return; } for (long long i = 1; i <= 6; i++) { a[x] = i; dfs(x + 1); } } int main() { n = read(); m = read(); for (long long i = 1; i <= m; i++) { x[i] = read(); y[i] = read(); } dfs(1); cout << ans << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 10, M = 110; long long n, m, ans, a[N], x[M], y[M], b[N][N]; inline long long read() { long long x = 0, tmp = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') tmp = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return tmp * x; } inline long long check() { memset(b, 0, sizeof(b)); long long ans = 0; for (long long i = 1; i <= m; i++) { if (!b[a[x[i]]][a[y[i]]]) { b[a[x[i]]][a[y[i]]] = b[a[y[i]]][a[x[i]]] = 1; ans++; } } return ans; } void dfs(long long x) { if (x == n + 1) { ans = max(ans, check()); return; } for (long long i = 1; i <= 6; i++) { a[x] = i; dfs(x + 1); } } int main() { n = read(); m = read(); for (long long i = 1; i <= m; i++) { x[i] = read(); y[i] = read(); } dfs(1); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; edges.push_back({a, b}); } vector<int> d(n); for (int i = 0; i < n; i++) d[i] = i + 1; if (n == 7) d[6] = 6; int ans = 0; do { set<pair<int, int>> sd; for (auto e : edges) if (sd.find({d[e.second], d[e.first]}) == sd.end()) sd.insert({d[e.first], d[e.second]}); ans = max(ans, (int)sd.size()); } while (next_permutation(d.begin(), d.end())); cout << ans << endl; return 0; }
### Prompt Please create a solution in CPP to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<pair<int, int>> edges; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; edges.push_back({a, b}); } vector<int> d(n); for (int i = 0; i < n; i++) d[i] = i + 1; if (n == 7) d[6] = 6; int ans = 0; do { set<pair<int, int>> sd; for (auto e : edges) if (sd.find({d[e.second], d[e.first]}) == sd.end()) sd.insert({d[e.first], d[e.second]}); ans = max(ans, (int)sd.size()); } while (next_permutation(d.begin(), d.end())); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> edges[8]; int calc(int a, int b) { vector<int> cnt(8); for (auto e : edges[a]) cnt[e]++; for (auto e : edges[b]) cnt[e]++; int sol = 0; for (int i = 1; i <= 7; i++) if (cnt[i] == 2) sol++; return sol; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, a, b; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; edges[a].push_back(b); edges[b].push_back(a); } if (n <= 6) { cout << m << "\n"; return 0; } int mmin = 1e9; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { mmin = min(mmin, calc(i, j)); } } cout << m - mmin << "\n"; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<int> edges[8]; int calc(int a, int b) { vector<int> cnt(8); for (auto e : edges[a]) cnt[e]++; for (auto e : edges[b]) cnt[e]++; int sol = 0; for (int i = 1; i <= 7; i++) if (cnt[i] == 2) sol++; return sol; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, a, b; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; edges[a].push_back(b); edges[b].push_back(a); } if (n <= 6) { cout << m << "\n"; return 0; } int mmin = 1e9; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { mmin = min(mmin, calc(i, j)); } } cout << m - mmin << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; map<string, int> ret; int n, m; void permute(string a, int i = 0) { if (i == signed(a.size())) { ret[a]++; return; } for (int j = i; j < signed(a.size()); j++) { swap(a[i], a[j]); permute(a, i + 1); swap(a[i], a[j]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); ; cin >> n >> m; unordered_map<int, vector<int>> g; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } string a = ""; for (int i = 6; i >= max(6 - n + 1, 0); i--) { a.push_back('0' + i); } permute(a); int ans = 0; for (auto i : ret) { unordered_map<int, int> cur; int val = 0; for (int j = 0; j < signed(i.first.size()); j++) { cur[j + 1] = (i.first[j] - '0'); if (i.first[j] - '0' == 0) { val = j + 1; } } for (int j = 1; j <= n; j++) { cur[val] = j; map<pair<int, int>, int> dom; for (int i = 1; i <= 6; i++) { for (int j = i; j <= 6; j++) { dom[make_pair(i, j)]++; } } for (auto k : g) { for (auto l : k.second) { if (dom.count(make_pair(cur[k.first], cur[l]))) { dom.erase(make_pair(cur[k.first], cur[l])); } } } ans = max(ans, 21 - signed(dom.size())); } } cout << ans; }
### Prompt Please formulate a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; map<string, int> ret; int n, m; void permute(string a, int i = 0) { if (i == signed(a.size())) { ret[a]++; return; } for (int j = i; j < signed(a.size()); j++) { swap(a[i], a[j]); permute(a, i + 1); swap(a[i], a[j]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); ; cin >> n >> m; unordered_map<int, vector<int>> g; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } string a = ""; for (int i = 6; i >= max(6 - n + 1, 0); i--) { a.push_back('0' + i); } permute(a); int ans = 0; for (auto i : ret) { unordered_map<int, int> cur; int val = 0; for (int j = 0; j < signed(i.first.size()); j++) { cur[j + 1] = (i.first[j] - '0'); if (i.first[j] - '0' == 0) { val = j + 1; } } for (int j = 1; j <= n; j++) { cur[val] = j; map<pair<int, int>, int> dom; for (int i = 1; i <= 6; i++) { for (int j = i; j <= 6; j++) { dom[make_pair(i, j)]++; } } for (auto k : g) { for (auto l : k.second) { if (dom.count(make_pair(cur[k.first], cur[l]))) { dom.erase(make_pair(cur[k.first], cur[l])); } } } ans = max(ans, 21 - signed(dom.size())); } } cout << ans; } ```
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; long long mod = 1000000007; const int MX = 0x3f3f3f3f; bool G[8][8]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u][v] = G[v][u] = true; } int mini = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { if (j == i) continue; int cur = 0; for (int k = 1; k <= 7; k++) cur += G[i][k] & G[j][k]; mini = min(mini, cur); } } cout << m - mini << "\n"; return 0; }
### Prompt Generate a cpp solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std::chrono; long long mod = 1000000007; const int MX = 0x3f3f3f3f; bool G[8][8]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; if (n <= 6) { cout << m << endl; return 0; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u][v] = G[v][u] = true; } int mini = INT_MAX; for (int i = 1; i <= 7; i++) { for (int j = 1; j <= 7; j++) { if (j == i) continue; int cur = 0; for (int k = 1; k <= 7; k++) cur += G[i][k] & G[j][k]; mini = min(mini, cur); } } cout << m - mini << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; unordered_map<string, int> ret; int n, m; void permute(string a, int i = 0) { if (i == signed(a.size())) { ret[a]++; return; } for (int j = i; j < signed(a.size()); j++) { swap(a[i], a[j]); permute(a, i + 1); swap(a[i], a[j]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); ; cin >> n >> m; unordered_map<int, vector<int>> g; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } string a = ""; for (int i = 6; i >= max(6 - n + 1, 0); i--) { a.push_back('0' + i); } permute(a); int ans = 0; for (auto i : ret) { unordered_map<int, int> cur; int val = 0; for (int j = 0; j < signed(i.first.size()); j++) { cur[j + 1] = (i.first[j] - '0'); if (i.first[j] - '0' == 0) { val = j + 1; } } for (int j = 1; j <= n; j++) { cur[val] = j; map<pair<int, int>, int> dom; for (int i = 1; i <= 6; i++) { for (int j = i; j <= 6; j++) { dom[make_pair(i, j)]++; } } for (auto k : g) { for (auto l : k.second) { if (dom.count(make_pair(cur[k.first], cur[l]))) { dom.erase(make_pair(cur[k.first], cur[l])); } } } ans = max(ans, 21 - signed(dom.size())); } } cout << ans; }
### Prompt Your task is to create a CPP solution to the following problem: Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every a and b such that 1 ≀ a ≀ b ≀ 6, there is exactly one domino with a dots on one half and b dots on the other half. The set contains exactly 21 dominoes. Here is an exact illustration of his set: <image> Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers n and m (1 ≀ n ≀ 7, 0 ≀ m ≀ (nβ‹…(n-1))/(2)) β€” the number of vertices and the number of edges in the graph. The next m lines contain two integers each. Integers in the i-th line are a_i and b_i (1 ≀ a, b ≀ n, a β‰  b) and denote that there is an edge which connects vertices a_i and b_i. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 4 4 1 2 2 3 3 4 4 1 Output 4 Input 7 0 Output 0 Input 3 1 1 3 Output 1 Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Output 16 Note Here is an illustration of Anadi's graph from the first sample test: <image> And here is one of the ways to place a domino on each of its edges: <image> Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex 1 have three dots. ### Response ```cpp #include <bits/stdc++.h> using namespace std; unordered_map<string, int> ret; int n, m; void permute(string a, int i = 0) { if (i == signed(a.size())) { ret[a]++; return; } for (int j = i; j < signed(a.size()); j++) { swap(a[i], a[j]); permute(a, i + 1); swap(a[i], a[j]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); ; cin >> n >> m; unordered_map<int, vector<int>> g; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } string a = ""; for (int i = 6; i >= max(6 - n + 1, 0); i--) { a.push_back('0' + i); } permute(a); int ans = 0; for (auto i : ret) { unordered_map<int, int> cur; int val = 0; for (int j = 0; j < signed(i.first.size()); j++) { cur[j + 1] = (i.first[j] - '0'); if (i.first[j] - '0' == 0) { val = j + 1; } } for (int j = 1; j <= n; j++) { cur[val] = j; map<pair<int, int>, int> dom; for (int i = 1; i <= 6; i++) { for (int j = i; j <= 6; j++) { dom[make_pair(i, j)]++; } } for (auto k : g) { for (auto l : k.second) { if (dom.count(make_pair(cur[k.first], cur[l]))) { dom.erase(make_pair(cur[k.first], cur[l])); } } } ans = max(ans, 21 - signed(dom.size())); } } cout << ans; } ```