結果
| 問題 |
No.271 next_permutation (2)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-05-24 10:21:10 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,776 bytes |
| コンパイル時間 | 3,576 ms |
| コンパイル使用メモリ | 280,304 KB |
| 実行使用メモリ | 6,272 KB |
| 最終ジャッジ日時 | 2025-05-24 10:21:15 |
| 合計ジャッジ時間 | 4,876 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 21 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:98:10: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
98 | freopen("inversion.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:99:10: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
99 | freopen("inversion.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:100:10: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
100 | freopen("inversion.err", "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
/*
* _|_|_|_|_| _|_|_|_| _|_|_| _|_|_|_|_| _|_|_| _|_|
* _| _| _| _| _| _| _|
* _| _|_|_| _|_| _| _|_| _|
* _| _| _| _| _| _|
* _|_|_|_|_| _| _|_|_| _| _|_|_| _|_|_|_|
*/
#include <bits/stdc++.h>
constexpr int pMod = 1E9 + 7;
constexpr int maxN = 1E5 + 1;
constexpr int inv2 = (pMod + 1) / 2;
constexpr __int128 INF = 2E18;
template<class Tp>
Tp Add(Tp x) {
return x;
}
template<class Tp>
Tp Mul(Tp x) {
return x;
}
template<class Tp, class... Tps>
int Add(Tp x, Tps... y) {
return (x + Add(y...)) % pMod;
}
template<class Tp, class... Tps>
int Mul(Tp x, Tps... y) {
return int(1LL * x * Mul(y...) % pMod);
}
template<class Tp, class... Tps>
void AddC(Tp &x, Tps... y) {
x = Add(x, y...);
}
template<class Tp, class... Tps>
void MulC(Tp &x, Tps... y) {
x = Mul(x, y...);
}
int Power(int x, int k) {
int res = 1;
while (k) {
if (k & 1) MulC(res, x);
MulC(x, x), k >>= 1;
}
return res;
}
int ExGcd(int a, int b, int &x, int &y) {
if (b == 0) return x = 1, y = 0, a;
int d = ExGcd(b, a % b, y, x);
return y -= a / b * x, d;
}
int Inv(int x) {
int ret, t, d = ExGcd(x, pMod, ret, t);
return d * Add(ret, pMod);
}
int fac[maxN], invFac[maxN];
void InitFac(int n = maxN - 1) {
fac[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = Mul(fac[i - 1], i);
invFac[n] = Inv(fac[n]);
for (int i = n; i >= 1; i--) invFac[i - 1] = Mul(invFac[i], i);
}
int Combination(int n, int m) {
return m > n || m < 0 ? 0 : Mul(fac[n], invFac[n - m], invFac[m]);
}
class FenwickTree {
int N;
std::vector<int> tree;
public:
explicit FenwickTree(int N) : N(N), tree(N) {}
void update(int x, int v = 1) {
for (; x < N; x |= x + 1) { tree[x] += v; }
}
int query(int x) const {
int res = 0;
for (x++; --x >= 0; x &= x + 1) { res += tree[x]; }
return res;
}
};
int main() {
freopen("inversion.in", "r", stdin);
freopen("inversion.out", "w", stdout);
freopen("inversion.err", "w", stderr);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
InitFac();
int N, ans = 0;
long long K;
std::cin >> N >> K;
std::vector<long long> facl(N + 1);
facl[0] = 1;
for (int i = 1; i <= N; i++) {
facl[i] = (long long) std::min(INF, (__int128) facl[i - 1] * i);
}
std::vector<int> A(N), S(N), R(N), F(N + 1);
for (auto &v: A) { std::cin >> v, v--; }
for (int i = 0; i <= N; i++) { F[i] = Mul(fac[i], i, i - 1, inv2, inv2); }
FenwickTree ft(N);
for (int i = N - 1, sum = 0; ~i; i--) {
R[i] = ft.query(A[i]), ft.update(A[i], 1);
AddC(sum, R[i]), AddC(ans, R[i]);
}
for (int i = 1; i < N; i++) { S[i] = Add(S[i - 1], R[i - 1]); }
auto Get = [&](int l, int r) { return Mul(l + r, r - l + 1, inv2); };
auto Work = [&](int M) {
assert(K < facl[M]);
for (int i = 0, sr = 0; i < M; i++) {
int vR = int(K / facl[M - i - 1]);
AddC(ans, Mul(vR, Add(F[M - i - 1], Mul(fac[M - i - 1], sr))));
AddC(ans, Mul(Get(0, vR - 1), fac[M - i - 1]));
K -= vR * facl[M - i - 1], AddC(sr, vR);
}
};
K--;
bool flg = false;
for (int i = N - 1; ~i; i--) {
int G = (int) std::min(N - i - 1LL - R[i], K / facl[N - i - 1]);
K -= facl[N - i - 1] * G;
AddC(ans, Mul(G, Add(F[N - i - 1], Mul(fac[N - i - 1], S[i]))));
AddC(ans, Mul(Get(R[i] + 1, R[i] + G), fac[N - i - 1])), R[i] += G;
if (R[i] < N - i - 1) {
AddC(ans, Mul(K % pMod, Add(S[i], R[i] + 1)));
Work(N - i - 1), flg = true;
break;
}
}
if (!flg) {
long long G = K / facl[N];
AddC(ans, Mul(G % pMod, F[N]));
K %= facl[N], Work(N);
}
std::cout << ans << '\n';
return 0;
}