C. Cyclic Permutations
原题链接:https://codeforces.com/contest/1391/problem/C
A permutation of length is an array consisting of n distinct integers from to in arbitrary order. For example, $[2,3,1,5,4] $is a permutation, but is not a permutation appears twice in the array and is also not a permutation but there is in the array .
Consider a permutation of length , we build a graph of size using it as follows:
For every
, find the largest
such that
and
, and add an undirected edge between node
and node
For every
, find the smallest
such that
and
, and add an undirected edge between node
and node
In cases where no such
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 , and ; here, the edges of the graph are .
A permutation is cyclic if the graph built using has at least one simple cycle.
Given , find the number of cyclic permutations of length . Since the number may be very large, output it modulo .
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer
.
Output
Output a single integer
, the number of cyclic permutations of length
modulo
.
Examples
input
4
output
16
input
583291
output
135712853
Note
There are
cyclic permutations for
is one such permutation, having a cycle of length four
.
Nodes form a simple cycle if the following conditions hold:
- k≥3.
- for any pair of indices and .
- and share an edge for all , and and 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;
}