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 is an array consisting of n distinct integers from 1 1 to n n in arbitrary order. For example, $[2,3,1,5,4] $is a permutation, but [ 1 , 2 , 2 ] [1,2,2] is not a permutation ( 2 ( 2 appears twice in the array ) ) and [ 1 , 3 , 4 ] [1,3,4] is also not a permutation ( n = 3 (n=3 but there is 4 4 in the array ) ) .

Consider a permutation p p of length n n , we build a graph of size n n using it as follows:

For every 1 i n 1≤i≤n , find the largest j j such that 1 j < i 1≤j<i and p j > p i p_j>p_i , and add an undirected edge between node i i and node j j
For every 1 i n 1≤i≤n , find the smallest j j such that i < j n i<j≤n and p j > p i p_j>p_i , and add an undirected edge between node i i and node j j
In cases where no such 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 , and 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) .

A permutation p p is cyclic if the graph built using p p has at least one simple cycle.

Given n n , find the number of cyclic permutations of length n n . Since the number may be very large, output it modulo 1 0 9 + 7 10^9+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) .

Output
Output a single integer 0 x < 1 0 9 + 7 0≤x<10^9+7 , the number of cyclic permutations of length n n modulo 1 0 9 + 7 10^9+7 .
Examples
input
4
output
16
input
583291
output
135712853

Note
There are 16 16 cyclic permutations for n = 4. n=4. [ 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 .

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

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

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

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#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;
}

猜你喜欢

转载自blog.csdn.net/m0_46272108/article/details/107903404