A11434 | Maximum Element
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
One day Petya was 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 his program and found out that his 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 following code:
Now Petya is interested in fault rate of his function. He asked you to find the number of permutations of integers from $1$ to $n$ such that the return value of his function on those permutations is not equal to $n$ . Since this number could be very big, output the answer modulo $10^{9}+7$ .
<br></br>int fast_max(int n, int a[]) { <br></br> int ans = 0;<br></br> int offset = 0;<br></br> for (int i = 0; i < n; ++i)<br></br> if (ans < a[i]) {<br></br> ans = a[i];<br></br> offset = 0;<br></br> } else {<br></br> offset = offset + 1;<br></br> if (offset == k)<br></br> return ans;<br></br> }<br></br> return ans;<br></br>}<br></br>That way the function iteratively checks array elements, storing the intermediate maximum, and if after $k$ consecutive iterations that maximum has not changed, it is returned as the answer.Now Petya is interested in fault rate of his function. He asked you to find the number of permutations of integers from $1$ to $n$ such that the return value of his function on those permutations is not equal to $n$ . Since this number could be very big, output the answer modulo $10^{9}+7$ .
输入格式
The only line contains two integers $n$ and $k$ ( $1<=n,k<=10^{6}$ ), separated by a space — the length of the permutations and the parameter $k$ .
输出格式
Output the answer to the problem modulo $10^{9}+7$ .
输入输出样例
输入 #1
5 2
输出 #1
22
输入 #2
5 3
输出 #2
6
输入 #3
6 3
输出 #3
84
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]$ .
$[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]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted