C. Cyclic Permutations
http://codeforces.com/contest/1391/problem/C
题面:
A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array) and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).
Consider a permutation p p p of length n n n, we build a graph of size n n n using it as follows:
- For every 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n, find the largest j j j such that 1 ≤ j < i 1 \leq j < i 1≤j<i and p j > p i p_j > p_i pj>pi, and add an undirected edge between node i i i and node j j j
- For every 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n, find the smallest j j j such that i < j ≤ n i < j \leq n i<j≤n and p j > p i p_j > p_i pj>pi, and add an undirected edge between node i i i and node j j j
In cases where no such j j j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4 n = 4 n=4, and p = [ 3 , 1 , 4 , 2 ] p = [3,1,4,2] p=[3,1,4,2]; here, the edges of the graph are ( 1 , 3 ) , ( 2 , 1 ) , ( 2 , 3 ) , ( 4 , 3 ) (1,3),(2,1),(2,3),(4,3) (1,3),(2,1),(2,3),(4,3).
A permutation p p p is cyclic if the graph built using p p p has at least one simple cycle.
Given n n n, find the number of cyclic permutations of length n n n. Since the number may be very large, output it modulo 1 0 9 + 7 10^9+7 109+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n n n ( 3 ≤ n ≤ 1 0 6 3 \le n \le 10^6 3≤n≤106).
Output
Output a single integer 0 ≤ x < 1 0 9 + 7 0 \leq x < 10^9+7 0≤x<109+7, the number of cyclic permutations of length n n n modulo 1 0 9 + 7 10^9+7 109+7.
Examples
input
4
output
16
input
583291
output
135712853
Note
There are 16 16 16 cyclic permutations for n = 4 n = 4 n=4. [ 4 , 2 , 1 , 3 ] [4,2,1,3] [4,2,1,3] is one such permutation, having a cycle of length four: 4 → 3 → 2 → 1 → 4 4 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 4 4→3→2→1→4.
Nodes v 1 v_1 v1, v 2 v_2 v2, … \ldots …, v k v_k vk form a simple cycle if the following conditions hold:
- k ≥ 3 k \geq 3 k≥3.
- v i ≠ v j v_i \neq v_j vi=vj for any pair of indices i i i and j j j. ( 1 ≤ i < j ≤ k 1 \leq i < j \leq k 1≤i<j≤k)
- v i v_i vi and v i + 1 v_{i+1} vi+1 share an edge for all i i i ( 1 ≤ i < k 1 \leq i < k 1≤i<k), and v 1 v_1 v1 and v k v_k vk share an edge.
翻译:
一个排列 长度为 n ,是一个数组包括 n 个不同的distinct 整数从 1 到 n 任意规则。例如, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] 是一个排序,但是就不是排列(2 出现了两次在数组里),并且 [ 1 , 3 , 4 ] [1,3,4] [1,3,4] 也不是排列(n = 3 但是4 出现了在数组里)。
考虑Consider 一个数组 长度为 n ,我们建立 一个学图形 面积是 n 使用他的方法如下:
- 对每一个 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n ,找到最大的 j j j 使得 1 ≤ j < i 1 \leq j < i 1≤j<i and p j > p i p_j > p_i pj>pi, 并且添加无向边undirected edge 在点 i i i and j j j 之间。
- 对每一个 $ 1 \leq i \leq n $ ,找到最小的 j j j 使得 i < j ≤ n i < j \leq n i<j≤n and p j > p i p_j > p_i pj>pi,并且添加无向边在点 i and j i \text{ and } j i and j之间。
在情况 如果这样的不存在,我们做不 边。另外also,注意,我们做边 之间 在对应的corresponding 指标indices,不是值 这些指标的。
为了清楚clarity,思考一个 n = 4 n = 4 n=4, and p = [ 3 , 1 , 4 , 2 ] p = [3,1,4,2] p=[3,1,4,2]; 在这里,图的边是 ( 1 , 3 ) , ( 2 , 1 ) , ( 2 , 3 ) , ( 4 , 3 ) (1,3),(2,1),(2,3),(4,3) (1,3),(2,1),(2,3),(4,3)
一个排序 P 是循环的,如果这个图形被建立 P 有至少一个简单的环。
给定 n ,找到一个数量 循环的排列的 长度为 n。
题意:
给定 n n n ,从1 到 n 的数组,在满足上面两个条件的情况下建图。
求全排列中带环的数列的个数。
题解:
反着找,找不成环的个数。
多画几组情况可以发现,当 两侧有大于 q [ i ] q[i] q[i] 的情况时,会成环,那么当所有大于 q [ i ] q[i] q[i] 的在同一侧就不能成环,最容易想到就是递增序列,同时你会发现,最大值是可以随意放的,而最小值只能放两侧,那么只要从最小值到最大值和次小值和最大值之间的递增递减的顺序是相反的就行。也就是没有波谷。即,每次将最小的两个值随意放两侧。可以是 1 2 XXX,或者是 1 XXX 2
剩下就是组合数学,假设 n = 4, 1就有两个位置可以放(两个边界),2有两个位置可以放,3有两位置可以放,最后4有一个位置可以放。
那么同理就是, 2 n − 1 2^{n-1} 2n−1个不成环的数列。
全排列是 n ! n! n! ,相减就可以求出成环的值。
代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int MAX = 2e6 + 100;
const int mod = 1e9 + 7;
int main(void)
{
IOS;
ll n;
cin>>n;
ll ans = 1, res = 1;
for(int i=2; i<=n; i++)
{
ans = ans * i % mod;
res = res * 2 % mod;
}
cout<<(ans - res + mod) % mod<<endl;
return 0;
}