P8474 「GLR-R3」立春 题解
思路
我们先枚举一下 \(n\le4\) 的情况:
当 \(n=1\) 时,逆序对数量的情况:
\(\tau(\sigma)=0\) 有 \(1\) 种情况。
当 \(n=2\) 时:
\(\tau(\sigma)=0\) 有 \(1\) 种情况。
\(\tau(\sigma)=1\) 有 \(1\) 种情况。
当 \(n=3\) 时:
\(\tau(\sigma)=0\) 有 \(1\) 种情况。
\(\tau(\sigma)=1\) 有 \(2\) 种情况。
\(\tau(\sigma)=2\) 有 \(2\) 种情况。
\(\tau(\sigma)=3\) 有 \(1\) 种情况。
当 \(n=4\) 时:
\(\tau(\sigma)=0\) 有 \(1\) 种情况。
\(\tau(\sigma)=1\) 有 \(3\) 种情况。
\(\tau(\sigma)=2\) 有 \(5\) 种情况。
\(\tau(\sigma)=3\) 有 \(6\) 种情况。
\(\tau(\sigma)=4\) 有 \(5\) 种情况。
\(\tau(\sigma)=5\) 有 \(3\) 种情况。
\(\tau(\sigma)=6\) 有 \(1\) 种情况。
根据上面的列表,可以算出
当 \(n=1\) 时,答案为 \(1\)。
当 \(n=2\) 时,答案为 \(3\)。
当 \(n=3\) 时,答案为 \(21\)。
当 \(n=4\) 时,答案为 \(315\)。
这时,再经过简单的观察,不难发现:
\(1\times3=3\),\(3\times7=21\),\(21\times15=315\)。
\(3\times2+1=7\),\(7\times2+1=15\)。
这时即可得出规律,即 \(ans_n=ans_{n-1}\times b_{i-1}\)。
其中 \(b_i=b_{i-1}\times2+1\),\(b_1=3\),\(ans_1=1\)。递推即可。
时间复杂度 \(\mathcal O(n)\)。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
int n,now,tmp;
signed main() {
cin>>n;
now=1,tmp=3;
for(int i=2;i<=n;i++){
now*=tmp;
now%=mod;
tmp=tmp*2+1;
tmp%=mod;
}
cout<<now;
return 0;
}

浙公网安备 33010602011771号