You are given a set S={1…n}. It guarantees that n is odd. You have to do the following operations until there is only 1 element in the set:
Firstly, delete the smallest element of S. Then randomly delete another element from S.
For each i∈[1,n], determine the probability of i being left in the S.
It can be shown that the answers can be represented by PQ, where P and Q are coprime integers, and print the value of P×Q−1 mod 998244353.
Input
The first line containing the only integer T(T∈[1,40]) denoting the number of test cases.
For each test case:
The first line contains a integer n .
It guarantees that: ∑n∈[1,5×106].
Output
For each test case, you should output n integers, i-th of them means the probability of i being left in the S.
Sample Input
1
3
Sample Output
0 499122177 499122177
给出一个 n n n,有 [ 1 , n ] [1,n] [1,n]中 n n n个数。
每次有如下操作:先删去最小的元素,再随机删掉一个元素。
求每个数留下来的概率期望。
考虑元素 i i i留下来的方案数,那么他前面有 i − 1 i-1 i−1个数,后面有 n − i n-i n−i个数。
我们易得当 n − i ≥ i − 1 n-i\geq i-1 n−i≥i−1时,i才会被留下来,即 i ≤ n 2 i\leq\frac{n}{2} i≤2n时都是 0 0 0,往后才有值。所以我们可以直接输出前 n 2 \frac{n}{2} 2n个 0 0 0。
然后在处理后面的数字。
我们设每次删除最小元素为操作一,随机删除元素为操作二。
即 i i i后面的 n − i n-i n−i个数一定是操作二删除的,所以第一步,我们让后面 n − i n-i n−i个数与前面 i − 1 i-1 i−1中的 n − i n-i n−i个一一对应删除就行。(在 i − 1 i-1 i−1中选 n − i n-i n−i个数出来在给他排序)
设第 i i i个数被留下来的方案数为 c n t [ i ] cnt[i] cnt[i],则
c n t [ i ] = C i − 1 n − i ∗ A n − i n − i = ( i − 1 ) ! ( 2 i − n − 1 ) ! cnt[i] = C_{i-1}^{n-i}*A_{n-i}^{n-i} = \frac{(i-1)!}{(2i-n-1)!} cnt[i]=Ci−1n−i∗An−in−i=(2i−n−1)!(i−1)!
然后在考虑剩下的 ( i − 1 ) − ( n − i ) = ( 2 i − n − 1 ) (i-1)-(n-i)=(2i-n-1) (i−1)−(n−i)=(2i−n−1)个数,根据前面,在其中选择两两删除。
( 2 i − n − 1 ) (2i-n-1) (2i−n−1)两两选择有 C 2 i − n − 1 2 ∗ C 2 i − n − 3 2 . . . C 4 2 ∗ C 2 2 C_{2i-n-1}^2*C_{2i-n-3}^2...C_4^2*C_2^2 C2i−n−12∗C2i−n−32...C42∗C22
( 2 i − n − 1 ) ! 2 ! ∗ ( 2 i − n − 3 ) ! ∗ ( 2 i − n − 3 ) ! 2 ! ∗ ( 2 i − n − 5 ) ! ∗ . . . ∗ 2 ! 2 ! ∗ 0 ! \frac{(2i-n-1)!}{2!*(2i-n-3)!}*\frac{(2i-n-3)!}{2!*(2i-n-5)!}*...*\frac{2!}{2!*0!} 2!∗(2i−n−3)!(2i−n−1)!∗2!∗(2i−n−5)!(2i−n−3)!∗...∗2!∗0!2!
( 2 i − n − 1 ) ! ( 2 ! ) 2 i − n − 1 2 \frac{(2i-n-1)!}{(2!)^{\frac{2i-n-1}{2}}} (2!)22i−n−1(2i−n−1)!
到这里还没有结束,然后我想了好久,是我太捞了。
比如 [ 1 , 6 ] [1,6] [1,6],我们选择的时候会重复选择多次,比如
( 1 , 2 ) ( 3 , 4 ) ( 5 , 6 ) (1,2)(3,4)(5,6) (1,2)(3,4)(5,6)
( 1 , 2 ) ( 5 , 6 ) ( 3 , 4 ) (1,2)(5,6)(3,4) (1,2)(5,6)(3,4)
( 3 , 4 ) ( 1 , 2 ) ( 5 , 6 ) (3,4)(1,2)(5,6) (3,4)(1,2)(5,6)
( 3 , 4 ) ( 5 , 6 ) ( 1 , 2 ) (3,4)(5,6)(1,2) (3,4)(5,6)(1,2)
( 5 , 6 ) ( 1 , 2 ) ( 3 , 4 ) (5,6)(1,2)(3,4) (5,6)(1,2)(3,4)
( 5 , 6 ) ( 3 , 4 ) ( 1 , 2 ) (5,6)(3,4)(1,2) (5,6)(3,4)(1,2)
这些都是上面包括的情况,因为选的先后次序不同,所以会出现重复的情况,一共出现相同的组合 ( n 2 ) ! (\frac{n}{2})! (2n)!,所以我们要在上面的基础上再除以 ( 2 i − n − 1 2 ) ! (\frac{2i-n-1}{2})! (22i−n−1)!
那么剩下数 ( 2 i − n − 1 ) (2i-n-1) (2i−n−1)中两两选择的方案数为
( 2 i − n − 1 ) ! ( 2 ! ) 2 i − n − 1 2 ∗ ( 2 i − n − 1 2 ) ! \frac{(2i-n-1)!}{(2!)^{\frac{2i-n-1}{2}}*(\frac{2i-n-1}{2})!} (2!)22i−n−1∗(22i−n−1)!(2i−n−1)!
最后整合上述所有情况,第 i i i个数被留下的总方案数为
c n t [ i ] = ( i − 1 ) ! ( 2 i − n − 1 ) ! ∗ ( 2 i − n − 1 ) ! ( 2 ! ) 2 i − n − 1 2 ∗ ( 2 i − n − 1 2 ) ! cnt[i] = \frac{(i-1)!}{(2i-n-1)!}*\frac{(2i-n-1)!}{(2!)^{\frac{2i-n-1}{2}}*(\frac{2i-n-1}{2})!} cnt[i]=(2i−n−1)!(i−1)!∗(2!)22i−n−1∗(22i−n−1)!(2i−n−1)!
化简得
c n t [ i ] = ( i − 1 ) ! ( 2 ! ) 2 i − n − 1 2 ∗ ( 2 i − n − 1 2 ) ! cnt[i] = \frac{(i-1)!}{(2!)^{\frac{2i-n-1}{2}}*(\frac{2i-n-1}{2})!} cnt[i]=(2!)22i−n−1∗(22i−n−1)!(i−1)!
总方案数为 s u m = ∑ i = 1 n c n t [ i ] sum=\sum_{i=1}^ncnt[i] sum=∑i=1ncnt[i]
第 i i i个数被留下的概率期望为 p [ [ i ] = c n t [ i ] s u m p[[i]=\frac{cnt[i]}{sum} p[[i]=sumcnt[i]
时间复杂度为 O ( n ) O(n) O(n)
事先预处理所有的阶乘和阶乘逆元。
参考:https://www.cnblogs.com/luyouqi233/p/13434815.html
dp方法我也不太会,哭了。
#include
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pdd;
#define INF 0x7f7f7f
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)
const ll mod = 998244353;
// const int maxn = 1e5 + 10;
// const double eps = 1e-6;
const int N = 5e6 + 5;
ll F[N];
ll invn[N];
ll invF[N];
ll cnt[N];
ll quick_pow(ll a, ll b)
{
ll ans = 1;
while(b)
{
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans % mod;
}
void Init()
{
F[0] = F[1] = invn[0] = invn[1] = invF[0] = invF[1] = 1;
for(int i = 2;i < N; i++){
F[i] = F[i - 1] * i % mod;
invn[i] = (mod - mod / i) * invn[mod % i] % mod;
invF[i] = invF[i - 1] * invn[i] % mod;
}
}
void solve() {
Init();
int T;
scanf("%d",&T);
ll inv2 = 499122177;
while(T--)
{
int n;
scanf("%d",&n);
if(n == 1) printf("1\n");
else{
printf("0");
for(int i = 2;i <= n / 2; i++)
printf(" 0");
ll ans = 0;
for(int i = n / 2 + 1;i <= n; i++) {
cnt[i] = F[i - 1] * invF[(2 * i - n - 1) / 2] % mod * quick_pow(inv2, (2 * i - n - 1) / 2) % mod;
ans = (ans + cnt[i]) % mod;
}
ll sum = quick_pow(ans, mod - 2);
for(int i = n / 2 + 1;i <= n; i++) {
printf("% lld",sum * cnt[i] % mod);
}
printf("\n");
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}