Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- using namespace std;
- struct Number {
- long long value;
- vector<int> add;
- vector<int> sub;
- };
- bool operator<(Number n1, Number n2) {
- return n1.value < n2.value;
- }
- Number sub(Number n1, const Number& n2, long long m) {
- n1.value = ((n1.value - n2.value) % m + m) % m;
- copy(n2.add.begin(), n2.add.end(), back_inserter(n1.sub));
- copy(n2.sub.begin(), n2.sub.end(), back_inserter(n1.add));
- return n1;
- }
- long long h(string s, long long p, long long m) {
- long long h = 0;
- long long q = 1;
- for (int i = 0; i < s.size(); ++i) {
- h = (h + s[i] * q) % m;
- q = (p * q) % m;
- }
- return h;
- }
- bool solve(long long p, long long m, int n) {
- long long q = 1;
- vector<Number> numbers(n);
- for (int i = 0; i < n; ++i) {
- numbers[i].value = q;
- numbers[i].add.push_back(i);
- q = (q * p) % m;
- }
- while (numbers.size() > 1) {
- sort(numbers.begin(), numbers.end());
- if (numbers[0].value == 0) break;
- for (int i = 0; i < numbers.size() / 2; ++i) {
- Number& n1 = numbers[2 * i + 0];
- Number& n2 = numbers[2 * i + 1];
- numbers[i] = sub(n2, n1, m);
- }
- numbers.resize(numbers.size() / 2);
- }
- if (numbers[0].value != 0) {
- return false;
- }
- string s1 = "";
- string s2 = "";
- for (int i = 0; i < n; ++i) {
- s1 += 'a';
- s2 += 'a';
- }
- for (int i : numbers[0].add) s1[i]++;
- for (int i : numbers[0].sub) s2[i]++;
- cout << s1 << endl;
- cout << s2 << endl;
- cout << h(s1, p, m) << endl;
- cout << h(s2, p, m) << endl;
- return true;
- }
- int main() {
- for (int n = 1; n <= 1000; ++n) {
- if (solve(31, 1000000007, n)) {
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement