这道题对前三个进行特判,然后 > 3 的部分就是这样找规律找出来的。
下面是打表代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define int long long
using namespace std;
constexpr int mod = 998244353;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = 4;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
a[i] = i + 1;
int ans = 0;
bool flag = true;
do
{
flag = true;
int idx = 1;
while (idx <= n)
{
int sum = 0;
for (int i = 0; i < idx; i++)
{
sum += a[i] * 2;
}
if (sum % idx != 0)
flag = false;
idx++;
}
if (flag)
{
for (int i = 0; i < n; i++)
cout << a[i] << " \n"[i == n - 1];
ans++;
}
} while (next_permutation(a.begin(), a.end()));
cout << ans << "\n";
return 0;
}
下面是 ac 代码
小于 4 的时候就按个特判就行
规律就是 > 4 的时候是 6 * (2 ^ (n - 3) )
#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
constexpr int mod = 998244353;
int q_m(int a, int q, int mod)
{
int ans = 1;
while (q > 0)
{
if (q & 1)
ans = a * ans % mod;
q >>= 1;
a = a * a % mod;
}
return ans;
}
void solve()
{
int n;
cin >> n;
if (n == 1 || n == 2) cout << n << "\n";
else cout << 6 * q_m(2, n - 3, mod) % mod << "\n";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}