根據打表可得,對於n的情況

任意一個首位!=1的排列時,則其答案-1可以與首位為1的情況對應

當n=4時

排列 答案

1 2 3 4 ------ 0

1 2 4 3 ------ 1

1 3 2 4 ------ 1

1 3 4 2 ------ 2

1 4 2 3 ------ 2

1 4 3 2 ------ 1

2 1 3 4 ------ 1

2 1 4 3 ------ 2

2 3 1 4 ------ 2

2 3 4 1 ------ 3

2 4 1 3 ------ 3

2 4 3 1 ------ 2

3 1 2 4 ------ 2

3 1 4 2 ------ 3

3 2 1 4 ------ 1

3 2 4 1 ------ 2

3 4 1 2 ------ 2

3 4 2 1 ------ 3

4 1 2 3 ------ 3

4 1 3 2 ------ 2

4 2 1 3 ------ 2

4 2 3 1 ------ 1

4 3 1 2 ------ 3

4 3 2 1 ------ 2

記f[i]表示i個數,所有i個數可以組成的排列的答案總和

則我們發現,有(n-1)(n-1)!項首位不等於1的排列,對答案有1的貢獻

除此之外,我們會發現,首項為1的數由於有n個數(包括自己)與其對應

所以對答案有n
f[i-1]的貢獻

所以f[i]=if[i-1]+(i-1)(i-1)!

注意要逆元

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mo 998244353ll
ll t,f[10000010],n,jc[10000010];
ll qp(ll x,ll y){
	ll r=1;
	x%=mo;
	while(y){
		if(y&1)r=r*x%mo;
		y>>=1;
		x=(x*x)%mo;
	}
	return r;
}
int main(){
	freopen("inverse.in","r",stdin);
	freopen("inverse.out","w",stdout);
	jc[0]=1;
	for(ll i=1;i<=10000010;i++)
		jc[i]=jc[i-1ll]*i%mo;
	for(ll i=1;i<=10000010;i++)
		f[i]=(i*f[i-1ll]%mo+(i-1ll)*jc[i-1ll]%mo)%mo;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld",&n);
		printf("%lld\n",f[n]*qp(jc[n],mo-2ll)%mo);
	}
}

jzoj5931的更多相关文章

随机推荐

  1. net下载文件方法汇总

    //TransmitFile实现下载 protected void Button1_Click1(object sender, EventArgs e) { /* 微软为Response对象提供了一个 ...

  2. iOS.Compiler

    1. 在Xcode4.6下创建的工程, 在Xcode5下build&run, 然后提示以下error. 难不成要在Xcode5下重新创建工程? Xcode cannot run using t ...

  3. Mint UI 之 Swipe 组件

    #为什么不显示内容? 一定要指定 mt-swipe 元素的宽和高. <mt-swipe :auto="4000" class="swipe"> &l ...

  4. Spring官方文档翻译(1~6章)

    Spring官方文档翻译(1~6章) 转载至 http://blog.csdn.net/tangtong1/article/details/51326887 Spring官方文档.参考中文文档 一.S ...

  5. windows 安装配置jdk7

    1.安装jdk这里不在介绍 2.配置新建用户变量:JAVA_HOME 值为(就是你自己jdk的安装路径):C:\Program Files\Java\jdk1.7.0_75\ 3.配置系统变量:Pat ...

  6. Ckeditor上传图片返回的JS直接显示出来,未执行!!!

    Ckeditor上传图片网上有很多教程. 下面是我今天下午遇到的一个坑...自己挖的坑. 在conotroller里 我开始习惯性的 response.setContentType("app ...

  7. const变量指针赋值给非const类型的指针运行结果

    在c++可以定义一个const变量,然后把变量的值赋给一个非const指针,可以通过指针来改变const变量的值吗?下面的截图给出了答案

  8. Guava学习笔记:Preconditions优雅的检验参数(java)

    http://www.cnblogs.com/peida/p/guava_preconditions.html 在日常开发中,我们经常会对方法的输入参数做一些数据格式上的验证,以便保证方法能够按照正常 ...

  9. Linux IPC之共享内存

    System V共享内存机制: shmget  shmat  shmdt  shmctl 原理及实现: system V IPC机制下的共享内存本质是一段特殊的内存区域,进程间需要共享的数据被放在该共 ...

  10. html5获取当前的位置..在地图中

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...