// B3 - Drybling Bajtessiego #include <iostream> #include <vector> #include <algorithm> #include <deque> #include <cmath> using namespace std; #define MOD 1000000007 bool testSubtrick(unsigned long long subtrick, unsigned long long trick) { while (subtrick <= trick) { if (subtrick == trick) { return(true); } if (subtrick % 2 == trick % 2) { subtrick /= 2; trick /= 2; } else { trick /= 2; } } return(false); } unsigned long long getPerfectCombos(string trick, vector<unsigned long long> *perfectNums) { unsigned long long res = 0; unsigned long long trickNum = 0; for (unsigned long long i = 0; i < trick.length(); ++i) { trickNum *= 2; if (trick[i] == 'L') { trickNum += 1; } } for (unsigned long long i = 0; i < perfectNums->size() && (*perfectNums)[i] <= trickNum; ++i) { if (testSubtrick((*perfectNums)[i], trickNum)) { ++res; if (res >= MOD) { res -= MOD; } } } return res; } int main() { ios_base::sync_with_stdio(0); unsigned long long n, maxLen, maxNum; string str; string *tricks; long long *numBalance; unsigned long long *rightOnes; bool *isNumLeft; bool *isNumPerfect; bool *isPow2; vector<unsigned long long> perfectNums; cin >> n; maxLen = 0; tricks = new string[n]; for (unsigned long long i = 0; i < n; ++i) { cin >> str; tricks[i] = str; if (maxLen < 2 * str.length()) { maxLen = 2 * str.length(); } } maxNum = pow(2.0, maxLen); numBalance = new long long[maxNum]; rightOnes = new unsigned long long[maxNum]; isNumLeft = new bool[maxNum]; isNumPerfect = new bool[maxNum]; isPow2 = new bool[maxNum]; numBalance[0] = -1; rightOnes[0] = 0; isNumLeft[0] = true; isNumPerfect[0] = false; for (unsigned long long i = 1; i < maxNum; ++i) { isPow2[i] = false; } for (unsigned long long i = 2; i < maxNum; i *= 2) { isPow2[i] = true; } for (unsigned long long i = 1; i < maxNum; ++i) { if (i % 2) { numBalance[i] = numBalance[i - 1] + 2; rightOnes[i] = rightOnes[i / 2] + 1; } else { numBalance[i] = numBalance[i - 1] - 2 * rightOnes[i - 1] + 1; if (isPow2[i] == false) { ++numBalance[i]; } rightOnes[i] = 0; } if (isNumLeft[i / 2] && numBalance[i] >= 0) { isNumLeft[i] = true; } else { isNumLeft[i] = false; } if (isNumLeft[i / 2] && numBalance[i] == 0) { isNumPerfect[i] = true; perfectNums.push_back(i); } else { isNumPerfect[i] = false; } } for (unsigned long long i = 0; i < n; ++i) { for (unsigned long long j = 0; j < n; ++j) { cout << getPerfectCombos(tricks[i] + tricks[j], &perfectNums) % MOD << " "; } cout << endl; } delete [] numBalance; delete [] rightOnes; delete [] isNumLeft; delete [] isNumPerfect; delete [] isPow2; 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | // B3 - Drybling Bajtessiego #include <iostream> #include <vector> #include <algorithm> #include <deque> #include <cmath> using namespace std; #define MOD 1000000007 bool testSubtrick(unsigned long long subtrick, unsigned long long trick) { while (subtrick <= trick) { if (subtrick == trick) { return(true); } if (subtrick % 2 == trick % 2) { subtrick /= 2; trick /= 2; } else { trick /= 2; } } return(false); } unsigned long long getPerfectCombos(string trick, vector<unsigned long long> *perfectNums) { unsigned long long res = 0; unsigned long long trickNum = 0; for (unsigned long long i = 0; i < trick.length(); ++i) { trickNum *= 2; if (trick[i] == 'L') { trickNum += 1; } } for (unsigned long long i = 0; i < perfectNums->size() && (*perfectNums)[i] <= trickNum; ++i) { if (testSubtrick((*perfectNums)[i], trickNum)) { ++res; if (res >= MOD) { res -= MOD; } } } return res; } int main() { ios_base::sync_with_stdio(0); unsigned long long n, maxLen, maxNum; string str; string *tricks; long long *numBalance; unsigned long long *rightOnes; bool *isNumLeft; bool *isNumPerfect; bool *isPow2; vector<unsigned long long> perfectNums; cin >> n; maxLen = 0; tricks = new string[n]; for (unsigned long long i = 0; i < n; ++i) { cin >> str; tricks[i] = str; if (maxLen < 2 * str.length()) { maxLen = 2 * str.length(); } } maxNum = pow(2.0, maxLen); numBalance = new long long[maxNum]; rightOnes = new unsigned long long[maxNum]; isNumLeft = new bool[maxNum]; isNumPerfect = new bool[maxNum]; isPow2 = new bool[maxNum]; numBalance[0] = -1; rightOnes[0] = 0; isNumLeft[0] = true; isNumPerfect[0] = false; for (unsigned long long i = 1; i < maxNum; ++i) { isPow2[i] = false; } for (unsigned long long i = 2; i < maxNum; i *= 2) { isPow2[i] = true; } for (unsigned long long i = 1; i < maxNum; ++i) { if (i % 2) { numBalance[i] = numBalance[i - 1] + 2; rightOnes[i] = rightOnes[i / 2] + 1; } else { numBalance[i] = numBalance[i - 1] - 2 * rightOnes[i - 1] + 1; if (isPow2[i] == false) { ++numBalance[i]; } rightOnes[i] = 0; } if (isNumLeft[i / 2] && numBalance[i] >= 0) { isNumLeft[i] = true; } else { isNumLeft[i] = false; } if (isNumLeft[i / 2] && numBalance[i] == 0) { isNumPerfect[i] = true; perfectNums.push_back(i); } else { isNumPerfect[i] = false; } } for (unsigned long long i = 0; i < n; ++i) { for (unsigned long long j = 0; j < n; ++j) { cout << getPerfectCombos(tricks[i] + tricks[j], &perfectNums) % MOD << " "; } cout << endl; } delete [] numBalance; delete [] rightOnes; delete [] isNumLeft; delete [] isNumPerfect; delete [] isPow2; return 0; } |