counting swaps

3602 Counting Swaps 0x30「数学知识」例题

背景

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 的排列 p_1,p_2,…,p_n,可进行若干次操作,每次选择两个整数 x,y,交换 p_x,p_y。设把 p_1,p_2,…,p_n 变成单调递增的排列 1,2,…,n 至少需要 m 次交换。求有多少种操作方法可以只用 m 次交换达到上述目标。因为结果可能很大,你只需要输出对 10^9+9 取模之后的值。1≤n≤10^5。

例如排列 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 p1,?…,?pn of the numbers 1 through n. In each step you can choose two numbers x?<?y and swap px with py.

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

输入格式

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 p1,?…,?pn: a permutation of 1,?…,?n.

In the easy subproblem C1, 1?≤?n?≤?10.

In the hard subproblem C2, 1?≤?n?≤?105.

输出格式

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

样例输入

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.

求长度为n的环变成自环的公式

f[n]=n^(n-2)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
const long long mod=1000000009;

long long a[maxn];
long long num[maxn];
long long l[maxn];
bool v[maxn];

long long pow(long long x,long long k){
    long long ans=1%mod;
    for (;k;k>>=1){
        if(k&1) ans=ans*x%mod;
        x=(long long)x*x%mod;
    }
    return ans%mod;
}

int main(){
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    a[0]=1;
    for(int i=1;i<=100000;i++) a[i]=a[i-1]*i%mod;
    int t;
    scanf("%d",&t);
    while(t--){
        memset(v,0,sizeof(v));
        int n;
        scanf("%d",&n);
        for (int i=1;i<=n;i++) scanf("%lld",&num[i]);
        int top=0;
        for (int i=1;i<=n;i++){
            if(!v[i]){
                int pp=1;
                v[i]=true;
                for (int j=num[i];j!=i;j=num[j]){
                    ++pp;
                    v[j]=true;
                }
                l[++top]=pp;
            }
        }
        long long ans=1;
        for (int i=1;i<=top;i++){
            ans=ans*(l[i]==1 ? 1 : pow(l[i],l[i]-2))%mod;
            ans=ans*pow(a[l[i]-1],mod-2)%mod;
        }
        ans=ans*a[n-top]%mod;
        printf("%lld\n",ans%mod);
    }
return 0;
}

原文地址:https://www.cnblogs.com/lmjer/p/9191341.html

时间: 2024-10-09 23:42:54

counting swaps的相关文章

lfyzoj104 Counting Swaps

问题描述 给定你一个 \(1 \sim n\) 的排列 \(\{p_i\}\),可进行若干次操作,每次选择两个整数 \(x,y\),交换 \(p_x,p_y\). 请你告诉穰子,用最少的操作次数将给定排列变成单调上升的序列 \(1,2,\ldots,n\),有多少种方式呢?请输出方式数对 \(10^9+9\) 取模的结果. 输入格式 第一行一个整数 \(T\) 代表数据组数. 每一组测试数据,第一行是一个整数 \(n\) 代表排列中的元素个数,第二行 \(n\) 个整数,是这个排列. 输入数据中

luogu P4778 Counting swaps

计数套路题?但是我连套路都不会,,, 拿到这道题我一脸蒙彼,,,感谢@poorpool 大佬的博客的指点 先将第\(i\)位上的数字\(p_i\)向\(i\)连无向边,然后构成了一个有若干环组成的无向图,可以知道某个点包含它的有且仅有一个环,因为所有点度数都为2(自环的点度数也是2吧qwq) 那么我们的最终目标就是把这个图转换成有\(n\)个自环的图,相当于把排列排好顺序 考虑对于一个\(m\)个点的环,把它变成\(m\)个自环至少需要\(m-1\)步(相当于排列\((2,3...m,1)\)变

P4778 Counting Swaps 题解

第一道 A 掉的严格意义上的组合计数题,特来纪念一发. 第一次真正接触到这种类型的题,给人感觉好像思维得很发散才行-- 对于一个排列 \(p_1,p_2,\dots,p_n\),对于每个 \(i\) 向 \(p_i\) 连一条边,可以发现整个构成了一个由若干环组成的图,目标是将这些环变为自环. 引理:把长度为 \(n\) 的环变为 \(n\) 个自环,最少交换次数为 \(n-1\). 用归纳法证,对于当前情况,任意一次交换都将其拆为两个环,由淘汰赛法则可知引理成立. 记 \(F_n\) 表示在最

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

Counting Divisors HDU - 6069

Counting Divisors HDU - 6069 题意:给定区间[a,b]和k,求xk有多少因子(x属于[a,b]),求和. 题解:http://blog.csdn.net/zlh_hhhh/article/details/76680641 a.b最大可达到1e12,但是b-a<1e6. 一开始愚蠢的一个一个分解然后去求有多少因子然后求和,范围那么大裸裸的超时啊! 可以枚举素数,对每一个素数,把区间内所有可以分解的进行分解. 最后再求和. 1 #include <bits/stdc++

LightOJ - 1148 Mad Counting(坑)

Mad Counting Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive a

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up