Advertisement
Guest User

BagAndCards.cpp

a guest
Jan 20th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstdlib>
  18. #include <ctime>
  19.  
  20. using namespace std;
  21.  
  22. class BagAndCards {
  23. public:
  24. int getHash(int, int, int, int, int, int, string);
  25. };
  26.  
  27. const int md = 1000000007;
  28.  
  29. inline void add(int &a, int b) {
  30. a += b;
  31. if (a >= md) a -= md;
  32. }
  33.  
  34. inline int mul(int a, int b) { return (long long)a * b % md; }
  35.  
  36. const int N = 1010;
  37.  
  38. int a[N][N];
  39. int sum[N][N];
  40.  
  41. int BagAndCards::getHash(int n, int m, int x, int A, int B, int C,
  42. string isGood) {
  43. for (int i = 0; i < n; i++) {
  44. for (int j = 0; j < m; j++) {
  45. a[i][j] = x;
  46. x = (((long long)x * A + B) ^ C) % md;
  47. }
  48. }
  49. for (int j = 0; j < n; j++) {
  50. for (int k = 0; k < m; k++) {
  51. sum[j][k] = 0;
  52. for (int t = 0; t < m; t++) {
  53. if (isGood[k + t] == 'Y') {
  54. add(sum[j][k], a[j][t]);
  55. }
  56. }
  57. }
  58. }
  59. int ans = 0;
  60. for (int i = 0; i < n; i++) {
  61. for (int j = i + 1; j < n; j++) {
  62. int res = 0;
  63. for (int k = 0; k < m; k++) {
  64. add(res, mul(a[i][k], sum[j][k]));
  65. }
  66. ans ^= res;
  67. }
  68. }
  69. return ans;
  70. }
  71.  
  72. // CUT begin
  73. ifstream data("BagAndCards.sample");
  74.  
  75. string next_line() {
  76. string s;
  77. getline(data, s);
  78. return s;
  79. }
  80.  
  81. template <typename T>
  82. void from_stream(T &t) {
  83. stringstream ss(next_line());
  84. ss >> t;
  85. }
  86.  
  87. void from_stream(string &s) { s = next_line(); }
  88.  
  89. template <typename T>
  90. string to_string(T t) {
  91. stringstream s;
  92. s << t;
  93. return s.str();
  94. }
  95.  
  96. string to_string(string t) { return "\"" + t + "\""; }
  97.  
  98. bool do_test(int n, int m, int x, int a, int b, int c, string isGood,
  99. int __expected) {
  100. time_t startClock = clock();
  101. BagAndCards *instance = new BagAndCards();
  102. int __result = instance->getHash(n, m, x, a, b, c, isGood);
  103. double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
  104. delete instance;
  105.  
  106. if (__result == __expected) {
  107. cout << "PASSED!"
  108. << " (" << elapsed << " seconds)" << endl;
  109. return true;
  110. } else {
  111. cout << "FAILED!"
  112. << " (" << elapsed << " seconds)" << endl;
  113. cout << " Expected: " << to_string(__expected) << endl;
  114. cout << " Received: " << to_string(__result) << endl;
  115. return false;
  116. }
  117. }
  118.  
  119. int run_test(bool mainProcess, const set<int> &case_set, const string command) {
  120. int cases = 0, passed = 0;
  121. while (true) {
  122. if (next_line().find("--") != 0) break;
  123. int n;
  124. from_stream(n);
  125. int m;
  126. from_stream(m);
  127. int x;
  128. from_stream(x);
  129. int a;
  130. from_stream(a);
  131. int b;
  132. from_stream(b);
  133. int c;
  134. from_stream(c);
  135. string isGood;
  136. from_stream(isGood);
  137. next_line();
  138. int __answer;
  139. from_stream(__answer);
  140.  
  141. cases++;
  142. if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end())
  143. continue;
  144.  
  145. cout << " Testcase #" << cases - 1 << " ... ";
  146. if (do_test(n, m, x, a, b, c, isGood, __answer)) {
  147. passed++;
  148. }
  149. }
  150. if (mainProcess) {
  151. cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
  152. int T = time(NULL) - 1453292642;
  153. double PT = T / 60.0, TT = 75.0;
  154. cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl;
  155. cout << "Score : "
  156. << 900 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT))
  157. << " points" << endl;
  158. }
  159. return 0;
  160. }
  161.  
  162. int main(int argc, char *argv[]) {
  163. cout.setf(ios::fixed, ios::floatfield);
  164. cout.precision(2);
  165. set<int> cases;
  166. bool mainProcess = true;
  167. for (int i = 1; i < argc; ++i) {
  168. if (string(argv[i]) == "-") {
  169. mainProcess = false;
  170. } else {
  171. cases.insert(atoi(argv[i]));
  172. }
  173. }
  174. if (mainProcess) {
  175. cout << "BagAndCards (900 Points)" << endl << endl;
  176. }
  177. return run_test(mainProcess, cases, argv[0]);
  178. }
  179. // CUT end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement