QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#705876#8340. 3 Sumnhuang685AC ✓237ms3916kbC++235.0kb2024-11-03 03:07:452024-11-03 03:07:46

Judging History

你现在查看的是最新测评结果

  • [2024-11-03 03:07:46]
  • 评测
  • 测评结果:AC
  • 用时:237ms
  • 内存:3916kb
  • [2024-11-03 03:07:45]
  • 提交

answer

/**
 * @author n685
 * @brief
 * @date 2024-11-02 10:54:24
 *
 *
 */
#include "bits/stdc++.h"

#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif

namespace rs = std::ranges;
namespace rv = std::views;

namespace multihash {

template <class T> constexpr std::pair<T, T> ex_eucl(T a, T b) {
  if (a < b) {
    auto [x, y] = ex_eucl(b, a);
    return {y, x};
  }
  if (b == 0) {
    assert(a == 1);
    return {1, 0};
  }
  auto [x, y] = ex_eucl(b, a % b);
  return {y, x - (a / b) * y};
}

template <int NUM> struct MultiHash {
  static constexpr int MAX = 1'000'000'007;
  // static inline const std::array<int, NUM> mods = []() {
  //   std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  //   std::uniform_int_distribution<int> rnd(10000, MAX);
  //   std::array<int, NUM> ans{};
  //   for (int& v : ans) {
  //     v = rnd(rng);
  //   }
  //   return ans;
  // }();
  static inline const std::array<int, NUM> mods{1'000'000'007, 1'000'000'009, 998'244'353};
  template <class T> static constexpr T normalize(std::integral auto val, T mod) {
    using U = decltype(mod + val);
    U uval = static_cast<U>(val);
    U umd = static_cast<U>(mod);
    if (uval <= -umd || umd <= uval) {
      uval %= umd;
    }
    if (val < 0) {
      uval += umd;
    }
    return static_cast<T>(uval);
  }

  std::array<int, NUM> vals{};
  MultiHash() = default;
  explicit MultiHash(std::integral auto val) {
    for (int i = 0; i < NUM; ++i) {
      vals[i] = normalize(val, mods[i]);
    }
  }
  MultiHash& operator+=(const MultiHash& rhs) {
    for (int i = 0; i < NUM; ++i) {
      vals[i] += rhs.vals[i];
      if (vals[i] >= mods[i]) {
        vals[i] -= mods[i];
      }
    }
    return *this;
  }
  friend MultiHash operator+(MultiHash lhs, const MultiHash& rhs) { return lhs += rhs; }
  MultiHash& operator-=(const MultiHash& rhs) {
    for (int i = 0; i < NUM; ++i) {
      vals[i] -= rhs.vals[i];
      if (vals[i] < 0) {
        vals[i] += mods[i];
      }
    }
    return *this;
  }
  MultiHash operator-() const {
    MultiHash ans;
    for (int i = 0; i < NUM; ++i) {
      ans.vals[i] = normalize(-vals[i], mods[i]);
    }
    return ans;
  }
  friend MultiHash operator-(MultiHash lhs, const MultiHash& rhs) { return lhs -= rhs; }
  MultiHash& operator*=(const MultiHash& rhs) {
    for (int i = 0; i < NUM; ++i) {
      vals[i] = static_cast<int>(int64_t{vals[i]} * rhs.vals[i] % mods[i]);
    }
    return *this;
  }
  friend MultiHash operator*(MultiHash lhs, const MultiHash& rhs) { return lhs *= rhs; }

  MultiHash binpow(std::integral auto b) const {
    MultiHash res{1}, a = *this;
    while (b > 0) {
      if (b % 2 == 1) {
        res *= a;
      }
      a *= a;
      b /= 2;
    }
    return res;
  }
  MultiHash inv() const {
    MultiHash ans;
    for (int i = 0; i < NUM; ++i) {
      ans.vals[i] = normalize(ex_eucl<int64_t>(vals[i], mods[i]).first);
    }
    return ans;
  }
  bool operator==(const MultiHash& rhs) const = default;
  auto operator<=>(const MultiHash& rhs) const = default;
};

} // namespace multihash

using Hash = multihash::MultiHash<3>;
constexpr int MX = 10;

int main() {
#ifndef LOCAL
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  const std::array<Hash, MX + 1> digits = []() {
    std::array<Hash, MX + 1> ans{};
    for (int i = 0; i <= 10; ++i) {
      ans[i] = Hash{i};
    }
    return ans;
  }();

  int n, k;
  std::cin >> n >> k;

  std::vector<int> m(k, 9);
  Hash hm;
  for (int j : m | rv::reverse) {
    hm = digits[10] * hm + Hash{j};
  }

  std::vector<Hash> a(n);
  for (int i = 0; i < n; ++i) {
    std::vector<int> v;
    std::string s;
    std::cin >> s;
    for (char c : s) {
      v.push_back(c - '0');
    }
    rs::reverse(v);
    while (std::ssize(v) > k) {
      std::vector<int> nv(k);
      for (int j = 0; j < std::ssize(v); ++j) {
        nv[j % k] += v[j];
      }
      for (int j = 0; j < std::ssize(nv); ++j) {
        if (nv[j] >= 10) {
          if (j == std::ssize(nv) - 1) {
            nv.push_back(0);
          }
          nv[j + 1] += nv[j] / 10;
          nv[j] %= 10;
        }
      }
      v.swap(nv);
    }
    if (v == m) {
      v = {};
    }
    for (int j : v | rv::reverse) {
      a[i] = digits[10] * a[i] + Hash{j};
    }
  }
  std::map<Hash, int> freq;
  int ans = 0;
  Hash hm2 = digits[2] * hm;
  for (int i = 0; i < n; ++i) {
    ++freq[a[i]];
    for (int j = i; j < n; ++j) {
      Hash sum = a[i] + a[j];
      if (freq.contains(-sum)) {
        ans += freq[-sum];
      }
      if (freq.contains(hm - sum)) {
        ans += freq[hm - sum];
      }
      if (freq.contains(hm2 - sum)) {
        ans += freq[hm2 - sum];
      }
    }
  }
  std::cout << ans << '\n';
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3640kb

input:

4 1
0
1
10
17

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 26ms
memory: 3756kb

input:

500 859
7118711592236878297922359501613604144948355616986970837340677671376753603836852811886591300370143151943368529129749813118476151865844255212534355441611481420938483178075143062691345257288242460282715389758789648541099090735875617822348551942134616963557723055980260082230902505269975518146286...

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 237ms
memory: 3876kb

input:

500 17336
11871159223687829792235950161360414494835561698697083734067767137675360383685281188659130037014315194336852912974981311847615186584425521253435544161148142093848317807514306269134525728824246028271538975878964854109909073587561782234855194213461696355772305598026008223090250526997551814628...

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 44ms
memory: 3748kb

input:

500 1
751324443898124078584847834484321089092662321556147445230263526014359393841194947303407593948729802551881289193716611867931891257925091769456350249725997883453296895094445731130479434019358742162771547784250401546380268386074363779242500860317042151185119666027858022664683818314351285215150806...

output:

2327631

result:

ok 1 number(s): "2327631"

Test #5:

score: 0
Accepted
time: 33ms
memory: 3700kb

input:

500 2
408542968136435277974575411503179002415404345446801430469044749372949272333801248935236224652806667129749035002588943020176263162139056819871274824302889304493205266143688886696157147111754418731401856424401766968832165255416237731963027205324149660112574729610391396555581935236134531799950318...

output:

212002

result:

ok 1 number(s): "212002"

Test #6:

score: 0
Accepted
time: 177ms
memory: 3884kb

input:

500 11500
75411775796562109942642493394321873284995260953010112281856775261847503626737348402159485133662757032091519863427156582689971229143089317472838196453888261138079171290535429921921548971897026706656838415620603757605079012541561774699628665865662183868374645956694140356716037674688084770628...

output:

7675

result:

ok 1 number(s): "7675"

Test #7:

score: 0
Accepted
time: 184ms
memory: 3916kb

input:

500 11500
85355036663164764459816544518601485185320972076300982726542821424439713703229669576802138231401047471351087455159512255765325323540671792953715169122669767368905391325060775725733157611188832204902997772518104188947349204726490597030311894441123834099315122116302203972018409854605418988681...

output:

1070

result:

ok 1 number(s): "1070"

Test #8:

score: 0
Accepted
time: 1ms
memory: 3760kb

input:

1 11500
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

1

result:

ok 1 number(s): "1"

Extra Test:

score: 0
Extra Test Passed