E. Maximum Element time limit per test 2 seconds memory limit per test megabytes input standard input output standard Output
One day Petya is solving a very interesting problem. But although he used many optimization techniques, his solution still got time limit exceeded verdict. Petya conducted a thorough analysis of the his program and found out that he function for finding maximum element in an array of n positive integers was too slow. Desperate, Petya decided to use a somewhat unexpected optimization using parameter K, so now his function contains the fol lowing Code:
int Fast_max (int n, int a[]) {
int ans = 0;
int offset = 0;
for (int i = 0; i < n; ++i)
if (ans < a[i]) {
ans = a[i];
offset = 0;
} else {
offset = offset + 1;
if (offset = = k)
return ans;
}
return ans;
}
That's the function iteratively checks array elements, storing the intermediate maximum, and if after kconsecutive Itera tions that maximum have not changed, it's returned as the answer.
Now Petya are interested in fault rate of his function. He asked you to find the number of permutations of integers from 1 to n such, the return value of the his function on Thos e permutations is not equal to N. Since This number could is very big, output the answer modulo 109 + 7. Input
The only line contains-integers n and K (1≤n, k≤106), separated by a space-the length of the permutations and th e parameter K. Output
Output the answer to the problem modulo 109 + 7. Examples input
5 2
Output
22
Input
5 3
Output
6
Input
6 3
Output
84
Note
Permutations from second example:
[4, 1, 2, 3, 5], [4, 1, 3, 2, 5], [4, 2, 1, 3, 5], [4, 2, 3, 1, 5], [4, 3, 1, 2, 5], [4, 3, 2, 1, 5].
Set Dp[i] Indicates the 1~i arrangement that meets the requirements of the topic at the end of I. Obviously if i<=k+1, then dp[i]=0.
We consider the position of i-1 in this arrangement. When the number between i-1 and I is more than K, it is obvious that there is a total of (i-k-1) * (i-2)! sequence; otherwise, the i-1 subscript J >= I-k, the first J number of the sequence is discretized into 1 to J, the permutations of these numbers must end with J, to meet the sequence of the topic requirements, A total of dp[j], because the following number is less than k, it is impossible to meet the requirements. DP[J] is the result of discretization, before discretization results in a total of dp[j]*c (i-2,j-1) * (i-j-1)!=dp[j]* (i-2)!/(j-1)! (first in the remaining i-2 the number of the J-1 row in the subscript 1~j-1 position, Subscript j After the last element is placed in a random order). Such
Extract (I-2)!, the following can be used to prefix and find out. The multiplication of factorial can be obtained by using inverse element.
Dp[n] is the number of sequences ending in N. We put N in a different position H, the number before the n subscript is discretized to 1~h-1, as above, we get the answer to
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map > #include <queue> #include <deque> #include <vector> #include <set> #include <algorithm&
Gt #include <math.h> #include <cmath> #include <stack> #include <iomanip> #define MEM0 (a) memset (a)
0,sizeof (a)) #define Meminf (a) memset (A,0x3f,sizeof (a)) using namespace Std;
typedef long Long LL;
typedef long double LD;
typedef double DB;
const int maxn=1000005,inf=0x3f3f3f3f;
const LL LLINF=0X3F3F3F3F3F3F3F3F,MOD=1E9+7;
Const LD Pi=acos ( -1.0L);
ll INV[MAXN],FAC[MAXN],DP[MAXN],SUM[MAXN];
ll Fastpow (ll Base,ll index) {ll sum=base,ans=1;
ll I=index;
while (i) {if (i%2) ans= (ans*sum)%mod;
Sum*=sum;
Sum=sum%mod;
i/=2;
} return ans;
} int main () {ll n,k,ans=0,i;
CIN >> N >> K;
if (n<=k+1) {printf ("0\n"); return 0;
} fac[0]=1; for (i=1;i<=n;i++) {fac[i]= (fac[i-1]*i)%mod;
} inv[n]=fastpow (Fac[n],mod-2);
for (i=n-1;i>=0;i--) {inv[i]=inv[i+1]* (i+1);
Inv[i]%=mod;
} MEM0 (DP); mem0 (sum);
for (i=k+2;i<=n;i++) {dp[i]= (i-k-1+ (sum[i-1]-sum[i-k-1]+mod)%mod)%mod;
Dp[i]= (dp[i]*fac[i-2])%mod;
sum[i]=sum[i-1]+ (dp[i]*inv[i-1])%mod;
Sum[i]%=mod;
ans+= (((dp[i]*fac[n-1)%mod) *inv[i-1])%mod;
Ans%=mod;
} cout << ans;
return 0; }