fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. using ivector = vector<int>;
  6. using imatrix = vector<ivector>;
  7. const int M = 100, P = 10;
  8. const ivector multiplier = {-1,0,1};
  9. const bool do_test = true;
  10. ivector arr(P), pow3(P+1,1);
  11. for (int x = 1; x <= P; ++x)
  12. pow3[x] = 3*pow3[x-1];
  13. const function<int(int,int)> recursive_solution = [&](int n, int k) {
  14. if (n == 0)
  15. return int(k == 0);
  16. int ans = 0;
  17. for (int p = n-1, a = arr[p], i = 0; i < 3; ++i)
  18. ans += recursive_solution(p,k+a*multiplier[i]);
  19. return ans; };
  20. const function<int(int,int)> brute_force_solution = [&](int n, int k) {
  21. int ans = 0;
  22. for (int U = pow3[n], s = 0; s < U; ++s) {
  23. int sum = 0;
  24. for (int x = s, i = 0; i < n; ++i, x /= 3)
  25. sum += arr[i]*multiplier[x%3];
  26. if (sum == k)
  27. ++ans; }
  28. return ans; };
  29. const auto submit = [&]() {
  30. int T; cin.tie(nullptr)->sync_with_stdio(false), cin >> T;
  31. for (int N, K, i; T--; cout << recursive_solution(N,K) << '\n')
  32. for (cin >> N >> K, i = 0; i < N; ++i)
  33. cin >> arr[i]; };
  34. const auto test = [&]() {
  35. random_device device;
  36. mt19937_64 random(device());
  37. uniform_int_distribution<int> uniform_N(1,P);
  38. uniform_int_distribution<int> uniform_K(1,M);
  39. uniform_int_distribution<int> uniform_A(-M,M);
  40. int T; cin >> T;
  41. int tests = 0;
  42. while (T--) {
  43. const int N = uniform_N(random), K = uniform_K(random);
  44. for (int i = 0; i < N; ++i)
  45. arr[i] = uniform_A(random);
  46. const int rc_ans = recursive_solution(N,K);
  47. const int bf_ans = brute_force_solution(N,K);
  48. if (++tests%100 == 0)
  49. cout << tests << " tests" << endl;
  50. if (rc_ans != bf_ans) {
  51. cout << "Generated test" << endl;
  52. cout << 1 << endl << N << ' ' << K << endl << arr[0];
  53. for (int i = 1; i < N; ++i)
  54. cout << ' ' << arr[i];
  55. cout << endl;
  56. cout << "RC ans = " << rc_ans << endl;
  57. cout << "BF ans = " << bf_ans << endl;
  58. return; } } };
  59. if (do_test)
  60. test();
  61. else
  62. submit();
  63. return 0; }
  64.  
Success #stdin #stdout 2.59s 5468KB
stdin
10000
stdout
100 tests
200 tests
300 tests
400 tests
500 tests
600 tests
700 tests
800 tests
900 tests
1000 tests
1100 tests
1200 tests
1300 tests
1400 tests
1500 tests
1600 tests
1700 tests
1800 tests
1900 tests
2000 tests
2100 tests
2200 tests
2300 tests
2400 tests
2500 tests
2600 tests
2700 tests
2800 tests
2900 tests
3000 tests
3100 tests
3200 tests
3300 tests
3400 tests
3500 tests
3600 tests
3700 tests
3800 tests
3900 tests
4000 tests
4100 tests
4200 tests
4300 tests
4400 tests
4500 tests
4600 tests
4700 tests
4800 tests
4900 tests
5000 tests
5100 tests
5200 tests
5300 tests
5400 tests
5500 tests
5600 tests
5700 tests
5800 tests
5900 tests
6000 tests
6100 tests
6200 tests
6300 tests
6400 tests
6500 tests
6600 tests
6700 tests
6800 tests
6900 tests
7000 tests
7100 tests
7200 tests
7300 tests
7400 tests
7500 tests
7600 tests
7700 tests
7800 tests
7900 tests
8000 tests
8100 tests
8200 tests
8300 tests
8400 tests
8500 tests
8600 tests
8700 tests
8800 tests
8900 tests
9000 tests
9100 tests
9200 tests
9300 tests
9400 tests
9500 tests
9600 tests
9700 tests
9800 tests
9900 tests
10000 tests