結果
| 問題 | No.3441 Sort Permutation 2 |
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2026-02-06 21:30:36 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 148 ms / 2,000 ms |
| コード長 | 4,506 bytes |
| 記録 | |
| コンパイル時間 | 4,405 ms |
| コンパイル使用メモリ | 365,704 KB |
| 実行使用メモリ | 39,512 KB |
| 最終ジャッジ日時 | 2026-02-06 21:30:48 |
| 合計ジャッジ時間 | 11,663 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#if __INCLUDE_LEVEL__ == 0
#include __BASE_FILE__
void Solve() {
int n;
IN(n);
vector<int> p(n);
IN(OneBased(p));
basic_string<bool> done(n, false);
vector<int> cyc;
cyc.reserve(n);
vector<int> ans(n);
for (int d : Rep1(1, n)) {
for (int i = d; i <= n; i += d) {
CsrArray<int>::Add(i, d);
}
}
auto ds = CsrArray<int>::Build(n + 1);
for (int s : Rep(0, n)) {
if (done[s]) {
continue;
}
cyc.clear();
{
int i = s;
do {
cyc.push_back(i);
i = p[i];
} while (i != s);
}
int g = 0;
ranges::sort(cyc);
for (int ci : Rep(1, Len(cyc))) {
g = gcd(g, cyc[ci] - cyc[0]);
}
for (int d : ds[g]) {
ans[d] += Len(cyc) - 1;
}
for (int i : cyc) {
done[i] = true;
}
}
ranges::for_each(ans | views::drop(1), LIFT(OUT));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Solve();
}
#elif __INCLUDE_LEVEL__ == 1
#include <bits/stdc++.h>
template <class T, int Id = -1>
class CsrArray {
public:
static void Reserve(int m) {
buf_.reserve(m);
}
static void Add(int i, T x) {
buf_.emplace_back(i, std::move(x));
}
static CsrArray Build(int n) {
CsrArray ret;
ret.pos_.resize(n + 1);
for (int i : buf_ | std::views::keys) {
++ret.pos_[i];
}
std::partial_sum(ret.pos_.begin(), ret.pos_.end(), ret.pos_.begin());
ret.data_.resize(ret.pos_[n]);
for (auto& [i, x] : buf_ | std::views::reverse) {
ret.data_[--ret.pos_[i]] = std::move(x);
}
buf_.clear();
return ret;
}
int size() const { return int(pos_.size()) - 1; }
auto operator[](int i) {
return std::span<T>(data_.data() + pos_[i], data_.data() + pos_[i + 1]);
}
auto operator[](int i) const {
return std::span<const T>(data_.data() + pos_[i], data_.data() + pos_[i + 1]);
}
private:
static thread_local inline std::vector<std::pair<int, T>> buf_;
std::vector<T> data_;
std::vector<int> pos_;
};
template <class T>
concept MyRange =
std::ranges::range<T> &&
!std::convertible_to<T, std::string_view> &&
!std::convertible_to<T, std::filesystem::path>;
template <class T>
concept MyTuple = std::__is_tuple_like<T>::value && !MyRange<T>;
namespace std {
istream& operator>>(istream& is, MyRange auto&& r) {
for (auto&& e : r) {
is >> e;
}
return is;
}
istream& operator>>(istream& is, MyTuple auto&& t) {
apply([&](auto&... xs) { (is >> ... >> xs); }, t);
return is;
}
ostream& operator<<(ostream& os, MyRange auto&& r) {
auto sep = "";
for (auto&& e : r) {
os << exchange(sep, " ") << forward<decltype(e)>(e);
}
return os;
}
ostream& operator<<(ostream& os, MyTuple auto&& t) {
auto sep = "";
apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t);
return os;
}
} // namespace std
template <class T>
class OneBased {
public:
explicit OneBased(T&& x) : ref_(std::forward<T>(x)) {}
template <class... Ts>
requires(sizeof...(Ts) > 1)
OneBased(Ts&&... xs) : ref_(std::forward_as_tuple(std::forward<Ts>(xs)...)) {}
friend std::istream& operator>>(std::istream& is, OneBased x) {
if constexpr (MyRange<T>) {
for (auto&& e : x.ref_) {
is >> ::OneBased(e);
}
} else if constexpr (MyTuple<T>) {
std::apply([&](auto&... xs) { (is >> ... >> ::OneBased(xs)); }, x.ref_);
} else {
is >> x.ref_;
--x.ref_;
}
return is;
}
friend std::ostream& operator<<(std::ostream& os, OneBased x) {
if constexpr (MyRange<T>) {
auto f = [](auto&& e) { return ::OneBased(std::forward<decltype(e)>(e)); };
os << (x.ref_ | std::views::transform(f));
} else if constexpr (MyTuple<T>) {
std::apply([&](auto&... xs) { os << std::tuple(::OneBased(xs)...); }, x.ref_);
} else {
os << ++x.ref_;
--x.ref_;
}
return os;
}
private:
T ref_;
};
template <class T>
OneBased(T&&) -> OneBased<T>;
template <class... Ts>
OneBased(Ts&&...) -> OneBased<std::tuple<Ts...>>;
using namespace std;
#define LIFT(f) ([&](auto&&... xs) -> decltype(auto) { return f(forward<decltype(xs)>(xs)...); })
#define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define Rep1(...) [](int l, int r) { return Rep(l, r + 1); }(__VA_ARGS__)
#define Len(r) int(size(r))
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')
#endif // __INCLUDE_LEVEL__ == 1
risujiroh