#include <iostream> #include <vector> #include <unordered_set> #include <cmath> using namespace std; // Funkcja do odwracania bitów void flipBits(vector<int>& arr, int a, int b) { if (arr[a] == arr[b]) { arr[a] = !arr[a]; arr[b] = !arr[b]; } } // Funkcja do generowania unikalnych liczb z tablicy bitów int generateUniqueNumber(const vector<int>& arr) { int num = 0; for (int i = 1; i < arr.size(); ++i) { num += arr[i] * pow(2, i); } return num; } void generate_permutations(vector<int>& nums, int start, vector<vector<int>>& result) { if (start == nums.size()) { result.push_back(nums); } else { for (int i = start; i < nums.size(); ++i) { swap(nums[start], nums[i]); generate_permutations(nums, start + 1, result); swap(nums[start], nums[i]); } } } vector<vector<int>> generateAllPermutations(int N) { vector<int> nums(N); for (int i = 0; i < N; ++i) { nums[i] = i + 1; } vector<vector<int>> result; generate_permutations(nums, 0, result); return result; } int main() { int n, m; cin >> n >> m; vector<int> bits(n + 1); for (int i = 1; i <= n; ++i) { cin >> bits[i]; } vector<pair<int, int>> operations; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; operations.push_back({ a, b }); } unordered_set<int> uniqueNumbers; uniqueNumbers.insert(generateUniqueNumber(bits)); vector<vector<int>> permutations = generateAllPermutations(m); for (const auto& perm : permutations) { for (int num : perm) { int a = operations[num - 1].first; int b = operations[num - 1].second; flipBits(bits, a, b); uniqueNumbers.insert(generateUniqueNumber(bits)); } } cout << uniqueNumbers.size() % 1000000007; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <iostream> #include <vector> #include <unordered_set> #include <cmath> using namespace std; // Funkcja do odwracania bitów void flipBits(vector<int>& arr, int a, int b) { if (arr[a] == arr[b]) { arr[a] = !arr[a]; arr[b] = !arr[b]; } } // Funkcja do generowania unikalnych liczb z tablicy bitów int generateUniqueNumber(const vector<int>& arr) { int num = 0; for (int i = 1; i < arr.size(); ++i) { num += arr[i] * pow(2, i); } return num; } void generate_permutations(vector<int>& nums, int start, vector<vector<int>>& result) { if (start == nums.size()) { result.push_back(nums); } else { for (int i = start; i < nums.size(); ++i) { swap(nums[start], nums[i]); generate_permutations(nums, start + 1, result); swap(nums[start], nums[i]); } } } vector<vector<int>> generateAllPermutations(int N) { vector<int> nums(N); for (int i = 0; i < N; ++i) { nums[i] = i + 1; } vector<vector<int>> result; generate_permutations(nums, 0, result); return result; } int main() { int n, m; cin >> n >> m; vector<int> bits(n + 1); for (int i = 1; i <= n; ++i) { cin >> bits[i]; } vector<pair<int, int>> operations; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; operations.push_back({ a, b }); } unordered_set<int> uniqueNumbers; uniqueNumbers.insert(generateUniqueNumber(bits)); vector<vector<int>> permutations = generateAllPermutations(m); for (const auto& perm : permutations) { for (int num : perm) { int a = operations[num - 1].first; int b = operations[num - 1].second; flipBits(bits, a, b); uniqueNumbers.insert(generateUniqueNumber(bits)); } } cout << uniqueNumbers.size() % 1000000007; return 0; } |