codeforces-886-E. Maximum Element(Технокубок 2018 - Отборочный Раунд 3)

 

E. Maximum Element

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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:

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 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 109 + 7.

Input

The only line contains two integers n and k (1 ≤ n, k ≤ 106), separated by a space — the length of the permutations and the 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].

 

题意:

给你一段程序,让你输出有多少数组的排列情况使得该程序输出的结果不为最大值n。

 

思路:

有点容斥原理的感觉,我们首先可以确定的就是n必定不能出现在开头,所以我们对此将第一位一次填入0~n-1,计算出不满足条件的方案数,

再求出所有不以n开头排列的方案数,我们用s数组记录不成立方案数的前缀和,最后只要用总的方案数减去不满足条件的数目就是答案。

 

 

 

AC code

 

#include<bits/stdc++.h>
#define mod 1000000007
#define maxn 2000020
using namespace std;
typedef long long ll;

ll a[maxn],s[maxn],n,k,ans;

ll pow_(ll x,ll y)
{
    if (y==0)
        return 1;
    ll tmp=pow_(x,y/2);
    tmp=tmp*tmp%mod;
    if (y%2)
        tmp=tmp*x%mod;
    return tmp;
}

ll rev(ll x)
{
    return pow_(x,mod-2);
}

int main()
{
    scanf("%d%d",&n,&k);
    ans=1,s[0]=0;

    for (int i=2;i<n;i++)
        ans=ans*i%mod;

    for (int i=1;i<=k;i++)
    {
        a[i]=ans;
        s[i]=(s[i-1]+a[i])%mod;
    }

    for (int i=k+1;i<n;i++)
    {
        a[i]=(s[i-1]-s[i-1-k])*rev(i)%mod;	//以a开头不成立的个数
        s[i]=(s[i-1]+a[i])%mod;			//依次记录总的不行方案数
    }

    ans=ans*(n-1)%mod;
    ans=(ans-s[n-1]+mod)%mod;			//所有情况减去不成立的情况
    printf("%d\n",ans);

    return 0;
}
	//以a开头不成立的个数
        s[i]=(s[i-1]+a[i])%mod;			//依次记录总的不行方案数
    }

    ans=ans*(n-1)%mod;
    ans=(ans-s[n-1]+mod)%mod;			//所有情况减去不成立的情况
    printf("%d\n",ans);

    return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值