hdu6432 Problem G. Cyclic(容斥原理)

题目

给你一个n,问n的圆排列中,

不包含[i,(i+1)%n]的方案数是多少

答案%998244353

思路来源

https://blog.csdn.net/qq_37025443/article/details/82018108(容斥)

https://blog.csdn.net/zero___zero/article/details/81951273(递推)

OEIS

题解

至少一对[i,i+1]或[n,1]的圆排列方案数是C_{n}^{1}*(n-2)!

考虑要选一个数当i,C_{n}^{1},这样(i+1)%n也被确定了,先把这两个数安在圆上

这个圆还有(n-2)个位置,剩下的随便放,所以……

 

线性排列的话,可以理解成把i和i+1合并成一个元素,

线性排列有(n-1)!个排列,

对于某一个线性排列,其任意一个元素当第一个都是一个新的排列,这样一个线性排列就能构造出(n-1)个排列

但在圆排列中这(n-1)个排列是一样的,所以是(n-2)!

 

然后又由于至少0对(就是有没有都可以)的圆排列方案数是(n-1)!

恰好符合C_{n}^{0}*(n-1)!的归纳,这样就可以归纳出……

至少有k对的圆排列方案数是C_{n}^{k}*(n-k-1)!

具体操作过程等价于选k个连续的数(此处n和1视为连续),

然后第k+1个数就定了,然后剩下的随便排搞一搞……

严格证明k的情况我也不会证QAQ

 

另外注意到k=n时,一个严格单增的序列对答案有1的贡献,

然后容斥原理是用总数-(奇加偶减),注意到总数是偶数0的情形

所以答案直接就是奇减偶加。

所以如果补充定义(-1)!=1的话,答案应该是\sum_{k=0}^{n}(-1)^{k}*C_{n}^{k}*(n-k-1)!

写起来方便的写法是(-1)^{n}+\sum_{k=0}^{n-1}(-1)^{k}*C_{n}^{k}*(n-k-1)!

心得

我容斥原理&&组合数学真的差的一比……

算了,无差别补题,补上就好了……

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=998244353; 
const int maxn=1e5;
ll Finv[maxn+5],jc[maxn+5];
ll modpow(ll x,ll n,ll mod)
{
	ll res=1;
	for(;n;x=x*x%mod,n/=2)
	if(n&1)res=res*x%mod;
	return res;
}
void init()
{
	jc[0]=Finv[0]=1;
	for(int i=1;i<=maxn;++i)
	{
	 jc[i]=jc[i-1]*i;
	 if(jc[i]>=mod)jc[i]%=mod;
    }
	Finv[maxn]=modpow(jc[maxn],mod-2,mod);
	for(int i=maxn-1;i>=1;--i)
	{
	 Finv[i]=Finv[i+1]*(i+1);
	 if(Finv[i]>=mod)Finv[i]%=mod;
    }
}
ll C(ll n,ll m)
{
	if(m<0||m>n)return 0;
	return jc[n]*Finv[n-m]%mod*Finv[m]%mod;
}
int t,n;
ll ans;
int main()
{
	init();
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		ans=0;
		for(int k=0;k<n;++k)
		{
			ll tmp=C(n,k)%mod*jc[n-k-1]%mod;
			if(k&1)ans-=tmp;
			else ans+=tmp;
			if(ans>=mod)ans%=mod;
		}
		//(-1)^n 
		if(n&1)ans--;
		else ans++;
		printf("%lld\n",(ans%mod+mod)%mod);
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code92007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值