列表

详情


NC51056. Counting Swaps

描述

背景

https://ipsc.ksp.sk/2016/real/problems/c.html

Just like yesterday (in problem U of the practice session), Bob is busy, so Alice keeps on playing some single-player games and puzzles. In her newest puzzle she has a permutation of numbers from 1 to n. The goal of the puzzle is to sort the permutation using the smallest possible number of swaps.

Instead of simply solving the puzzle, Alice is wondering about the probability of winning it just by playing at random. In order to answer this question, she needs to know the number of optimal solutions to her puzzle.

描述

给定一个 1~n 的排列 ,可进行若干次操作,每次选择两个整数 x,y,交换 p_x,p_y。设把 变成单调递增的排列 1,2,…,n 至少需要 m 次交换。求有多少种操作方法可以只用 m 次交换达到上述目标。因为结果可能很大,你只需要输出对 取模之后的值。
例如排列 2,3,1 至少需要2次交换才能变为 1,2,3。操作方法共有3种,分别是:
先交换数字2,3,变成 3,2,1,再交换数字3,1,变成 1,2,3。
先交换数字2,1,变成 1,3,2,再交换数字3,2,变成 1,2,3。
先交换数字3,1,变成 2,1,3,再交换数字2,1,变成 1,2,3。

You are given a permutation   of the numbers 1 through n. In each step you can choose two numbers and swap p_x with p_y.

Let m be the minimum number of such swaps needed to sort the given permutation. Compute the number of different sequences of exactly mswaps that sort the given permutation. Since this number may be large, compute it modulo .



输入描述

The first line of the input file contains an integer t specifying the number of test cases. Each test case is preceded by a blank line.

Each test case consists of two lines. The first line contains the integer n. The second line contains the sequence : a permutation of 1, …,n.

In the easy subproblem C1.

In the hard subproblem C2.

输出描述

For each test case, output a single line with a single integer:, where x is the number of ways to sort the given sequence using as few swaps as possible.

示例1

输入:

3

3
2 3 1

4
2 1 4 3

2
1 2

输出:

3
2
1

说明:

In the first test case, we can sort the permutation in two swaps. We can make the first swap arbitrarily; for each of them, there’s exactly one optimal second swap. For example, one of the three shortest solutions is “swap p1 with p2 and then swap p1 with p3”.

In the second test case, the optimal solution involves swapping p1 with p2 and swapping p3 with p4. We can do these two swaps in either order.

The third sequence is already sorted. The optimal number of swaps is 0, and thus the only optimal solution is an empty sequence of swaps.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 379ms, 内存消耗: 1628K, 提交时间: 2020-08-19 17:14:43

#include<cstdio>
#include<cstring>
#define ll long long
const int M=100010,p=1e9+9;
int a[M];
ll jc[M];
bool v[M];
ll ksm(ll x,int c)
{
	ll ans=1ll;
	while(c)
	{
		if(c&1)ans=ans*x%p;
		x=x*x%p;
		c>>=1;
	}
	return ans;
}
int main()
{
	jc[0]=jc[1]=1;
	for(int i=2;i<=M-10;i++)jc[i]=(jc[i-1]*i)%p;
	int t;scanf("%d",&t);
	while(t--)
	{
		int n;scanf("%d",&n);
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		memset(v,0,sizeof(v));
		int tot=0;
		ll ans=1ll;
		for(int i=1;i<=n;i++)
		{
			if(v[i])continue;
			int len;
			v[i]=len=1;
			for(int j=a[i];j!=i;j=a[j])len++,v[j]=1;
			tot++;
			ans=ans*(len==1?1:ksm((ll)len,len-2))%p;
			ans=ans*ksm(jc[len-1],p-2)%p;
		}
		ans=ans*jc[n-tot]%p;
		printf("%lld\n",ans);
	}
	return 0;
}

上一题