Codeforces #663 (Div. 2) C. Cyclic Permutations

C. Cyclic Permutations

原题链接:https://codeforces.com/contest/1391/problem/C

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

A permutation of length n n n is an array consisting of n distinct integers from 1 1 1 to n n n in arbitrary order. For example, $[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≤i≤n 1in, find the largest j j j such that 1 ≤ j < i 1≤j1j<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≤i≤n 1in, find the smallest j j j such that i < j ≤ n ii<jn 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 ( 3 ≤ n ≤ 1 0 6 ) n (3≤n≤10^6) n(3n106).

Output
Output a single integer 0 ≤ x < 1 0 9 + 7 0≤x<10^9+7 0x<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→3→2→1→4 :43214.

Nodes v 1 , v 2 , … , v k v_1, v_2, …, v_k v1,v2,,vk form a simple cycle if the following conditions hold:

  • k≥3.
  • v i ≠ v j v_i≠v_j vi=vj for any pair of indices i i i and j j j. ( 1 ≤ i < j ≤ k ) (1≤i(1i<jk)
  • v i v_i vi and v i + 1 v_{i+1} vi+1 share an edge for all i ( 1 ≤ i < k ) i (1≤ii(1i<k), and v 1 v_1 v1 and v k v_k vk share an edge.

(先上代码,题解稍后送上)

#include
#include
#include
#include
#include
#include
#define ll long long
const ll mod = 1e9 + 7;
using namespace std;
ll fun(ll n)// n!
{
	ll ans = 1;
	for (ll i = 1; i <= n; i++)
		ans = ans * i % mod;
	return ans;
}
ll quick_pow(ll a, ll b, ll c) //2^(n-1)快速幂
{
	ll ans = 1;
	a = a % c;
	while (b)
	{
		if (b & 1)
			ans = (ans * a) % c;
		a = (a * a) % c;
		b = b / 2;
	}
	return ans;
}

int main()
{
	ll n;
	cin >> n;
	ll res;
	res = fun(n) - pow2(2, n - 1, mod) + mod;
	printf("%d\n", res % mod);
	return 0;
}

你可能感兴趣的:(codeforces)