每日一练4/15,逆序对题解
逆序对
http://www.nowcoder.com/questionTerminal/1e3eb598c8ca4631ae2f9ce9016470ec
考虑选取两个位置,前一个位置放1,后一个位置放0,则得到一个逆序对。则逆序对有 个。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll ksm(ll k,ll c)
{
ll ans=1;
while(k)
{
if(k&1)
ans=(ans*c)%mod;
c=(c*c)%mod;
k>>=1;
}
return ans%mod;
}
int main()
{
ll n;
cin >> n;
if(n==1)
printf("0\n");
else
{
ll ans=ksm(n-2,2);
ll nov=ksm(mod-2,2);
ans=((ans*(n%mod)%mod)*((n-1)%mod)%mod)*nov%mod;
cout << ans;
}
return 0;
}
