看到出题人题解的时间复杂度是 ,发现我赛时的思路更优(?),所以来写一篇题解
注意到如果前面的所有位都选好了数,这一位只有可能选可以选的最大的和次大的(前提是有),因为如果选了次大的,下面肯定是从大到小的选,选了第三大的也是从大到小选,明显不如选次大的优。所以遍历每一位,考虑最大和次大的情况,选了次大的下面的位就从大到小选,因为 的时候无论 多大答案只与最后 位有关。时间复杂度 。
我的代码(赛时没调出来,我的码风非常丑)
//Do not hack it
#include<bits/stdc++.h>
#define endl cerr<<"------------------I Love Sqrt Decomposition------------------\n";
#define int __int128
using namespace std;
#ifdef __linux__
#define Gc getchar_unlocked
#define Pc putchar_unlocked
#endif
#ifndef __linux__
#define Gc getchar
#define Pc putchar
#endif
#define ds(x) (x=='\r'||x=='\n'||x==' ')
namespace o {
template<typename T> inline void r(T& a) { a = 0; char ch = Gc(); bool ok = 0; for (; ch < '0' || ch>'9';) { if (ch == '-')ok ^= 1; ch = Gc(); }for (; ch >= '0' && ch <= '9';) { a = (a << 1) + (a << 3) + (ch ^ 48); ch = Gc(); }if (ok)a = -a; }
template<typename T> inline void w(T a) { if (a == 0) { Pc('0'); return; }static char ch[20]; int till = 0; if (a < 0) { Pc('-'); for (; a;) { ch[till++] = -(a % 10); a /= 10; } } else { for (; a;) { ch[till++] = a % 10; a /= 10; } }for (; till;)Pc(ch[--till] ^ 48); }
struct Srr {
Srr operator>>(int& a) { r(a); return{}; }
Srr operator>>(char& ch) { ch = Gc(); for (; ds(ch);) { ch = Gc(); }return{}; }
Srr operator>>(string& s) { s = ""; char ch = Gc(); for (; ds(ch);) { ch = Gc(); }for (; !(ds(ch) || ch == EOF);) { s.push_back(ch); ch = Gc(); }return {}; }
template<typename T> Srr operator<<(T& a) { r(a); return{}; }
void is(string& s, int T) { s = ""; char ch = Gc(); for (; ds(ch);) { ch = Gc(); }for (; T--;) { s.push_back(ch); ch = Gc(); } }
}in;
struct Sww {
Sww operator<<(const int a) { w(a); return{}; }
Sww operator<<(const char ch) { Pc(ch); return{}; }
Sww operator<<(const string s) { for (int i = 0; i < s.size(); i++) { Pc(s[i]); }return{}; }
template<typename T> Sww operator>>(const T a) { w(a); return{}; }
}out;
namespace __STL {
const bool __is_P[] = { 0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1 };
inline bool is_p(const int a) { if (a <= 29) { return __is_P[a]; }if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) { return 0; }for (int i = 6;; i += 6) { if (((i + 1) * (i + 1)) > a) { return 1; }if (a % (i + 1) == 0) { return 0; }if (((i + 5) * (i + 5)) > a) { return 1; }if (a % (i + 5) == 0) { return 0; } } }
inline int power(int a, int b, const int mod = -1) { int ans = 1; if (mod == -1) { for (; b;) { if (b & 1) { ans *= a; }b >>= 1; a *= a; }return ans; }for (; b;) { if (b & 1) { ans = ans * a % mod; }b >>= 1; a = a * a % mod; }return ans; }
}
}using o::in; using o::out;
#undef ds
#undef Gc
#undef Pc
using namespace o::__STL;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) ((a)<0?-(a):(a))
#define eout cerr
int a, p;
int ans = 0;
bool vis[105];
int f[1005];
int d[105];
void dfs(int n) {//考虑到第n位(0开始)
if (n == a) {
//out << p << '\n';
int x = 0;
for (int i = 0; i < a; i++) {
x += (a - i) * d[i];
}
ans = max(ans, x);/*
out << x << "\n";
for (int i = 0; i < a; i++) {
out << d[i] << ' ';
}
out << '\n';*/
return;
}
int y = 0, c = 0;
int k = 0;
int last = 0;
for (int i = 1; i <= a; i++) {
if (vis[i]) {
continue;
}
k++;
//sum选i最少增加多少排名
int sum = f[a - n - 1] * (k - 1);
if (sum > p) {
break;
}
last = y;
y = i;
c = sum;
}
if (k == 2) {
d[n] = y;
vis[y] = 1;
p -= c;
dfs(n + 1);
p += c;
vis[y] = 0;
return;
}
d[n] = y;
vis[y] = 1;
p -= c;
dfs(n + 1);
p += c;
vis[y] = 0;
vis[last] = 1;
d[n] = last;
int till = n + 1;
for (int i = a; i >= 1; i--) {
if (!vis[i]) {
d[till++] = i;
}
}
vis[last] = 0;
dfs(a);
}
int e = 0;
#define mod 998244353
void bdfs(int n) {//考虑到第n位(0开始)
if (n == a) {
//out << p << '\n';
int x = 0;
for (int i = 0; i < a; i++) {
x += (a - i) * (d[i] + e);
}
ans = max(ans, x);/*
for (int i = 0; i < a; i++) {
out << d[i] << ' ';
}
out << '\n';*/
return;
}
int y = 0, c = 0;
int k = 0;
int last = 0;
for (int i = 1; i <= a; i++) {
if (vis[i]) {
continue;
}
k++;
//sum选i最少增加多少排名
int sum = f[a - n - 1] * (k - 1);
if (sum > p) {
break;
}
last = y;
y = i;
c = sum;
}
if (k == 2) {
d[n] = y;
vis[y] = 1;
p -= c;
bdfs(n + 1);
p += c;
vis[y] = 0;
return;
}
d[n] = y;
vis[y] = 1;
p -= c;
bdfs(n + 1);
p += c;
vis[y] = 0;
vis[last] = 1;
d[n] = last;
int till = n + 1;
for (int i = a; i >= 1; i--) {
if (!vis[i]) {
d[till++] = i;
}
}
vis[last] = 0;
bdfs(a);
}
int ni;
int sum(int x) {
return (x + 1) % mod * ((x + 2) % mod) % mod * x % mod * ni % mod;
}
signed main() {
f[1] = 1;
ni = power(6, mod - 2, mod);
for (int i = 2; i <= 50; i++) {
f[i] = f[i - 1] * i;
}
int T;
in >> T;
for (; T--;) {
memset(vis, 0, sizeof(vis));
in >> a >> p;
ans = 0;
p--;
if (a <= 20) {
dfs(0);
out << ans << '\n';
continue;
}
else {
e = a - 20;
int ansa = sum(a);
for (int i = 0; i < 20; i++) {
ansa -= (a - i) * (i + 1);
}
ansa %= mod;
a -= e;
bdfs(0);
ansa += ans;
ansa %= mod;
ansa = (ansa + mod) % mod;
out << ansa << '\n';
}
}
return 0;
}