B. Marin and Anti-coprime Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Marin wants you to count number of permutations that are beautiful. A beautiful permutation of length n is a permutation that has the following property:
gcd ( 1 ⋅ p 1 , 2 ⋅ p 2 , … , n ⋅ p n ) > 1 , \gcd (1 \cdot p_1, \, 2 \cdot p_2, \, \dots, \, n \cdot p_n) > 1, gcd(1⋅p1,2⋅p2,…,n⋅pn)>1,
where$ \gcd $is the greatest common divisor.
A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3, 4] is also not a permutation (n=3 but there is 4 in the array).
Input
The first line contains one integer t ( 1 ≤ t ≤ 1 0 3 ) (1 \le t \le 10^3) (1≤t≤103) — the number of test cases.
Each test case consists of one line containing one integer n ( 1 ≤ n ≤ 1 0 3 ) . n (1 \le n \le 10^3). n(1≤n≤103).
Output
For each test case, print one integer — number of beautiful permutations. Because the answer can be very big, please print the answer modulo$ 998,244,353.$
Example
input
Copy
7
1
2
3
4
5
6
1000
output
Copy
0
1
0
4
0
36
665702330
Note
In first test case, we only have one permutation which is [1] but it is not beautiful because gcd ( 1 ⋅ 1 ) = 1. \gcd(1 \cdot 1) = 1. gcd(1⋅1)=1.
In second test case, we only have one beautiful permutation which is [2, 1] because$ \gcd(1 \cdot 2, 2 \cdot 1) = 2.$
题目分析:
这道题比较巧妙,虽然说只要让gcd不唯一,其实醉酒当的方法就是让gcd为2,也就是全部变成偶数,所以只需要让排列中的奇数乘偶数,偶数乘奇数,这样分配,就能得到数量,如果排列有奇数个数,那么无法让拍列的奇数都成偶数,所以不可能成立,只有排列为偶数个,然后奇数位和偶数位分别求阶乘乘起来就是结果(求阶乘的话,在程序开始前求出来就好,记得取模)
#include<iostream>
using namespace std;
#define mod 998244353
typedef unsigned long long ll;
ll a[504];
int main()
{
int n;
cin>>n;
int tmp;
a[1]=1;
for(int i=2;i<=500;i++){
a[i]=a[i-1]*i%mod;
}
while(n--){
cin>>tmp;
if(tmp%2!=0)cout<<"0"<<endl;
else cout<<a[tmp/2]*a[tmp/2]%mod<<endl;
}
}