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⋅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≤103) — the number of test cases.
Each test case consists of one line containing one integer 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 998244353.
输入 | 输出 |
---|---|
7 | |
1 | 0 |
2 | 1 |
3 | 0 |
4 | 4 |
5 | 0 |
6 | 36 |
1000 | 665702330 |
分析:当n为奇数时,permutations的数量为0
当n为偶数时,permutations的数量为pow((n/2)!,2)
感叹号为阶层
例子:n=6 A(3,3)*A(3,3)=36
即排列组合n/2的偶数位3个与n/2的奇数位3个排列组合
原因:当n为偶数时gcd(1 * p1, 2 * p2…n * pn)
p1,p3,p5…只能放偶数
p2,p4,p6…只能放奇数
此时 p11,p22,p3*3 全为偶数
最大公约数为2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for (int i=0;i<t;i++)
{
int n=in.nextInt();
if ((n&1)==1)//为奇数
{
System.out.println(0);
}
else
{
//n除以2
n=n/2;
long sum=1l;
for (int j=2;j<=n;j++)//n/2
{
sum=sum*j*j%998244353;
}
System.out.println(sum);
}
}
}
}
求赞