逆序对

逆序对

https://ac.nowcoder.com/acm/problem/14731

挺好的一个题目.考虑每组逆序对的贡献,就是,然后有组,然后答案就算它们相乘,注意n==1时输出0.然后注意在做乘法的时候把n%mod.因为n很大.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll qp(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1)    res=res%mod*a%mod;
        a=a%mod*a%mod;
        b>>=1;
    }return res%mod;
}

int main()
{
    ll n;cin>>n;
    if(n==1) cout<<0<<'\n';
    else     cout<<(n%mod)*((n-1)%mod)%mod*qp(2ll,n-2ll)%mod*qp(2ll,mod-2ll)%mod<<'\n';
    return 0;
}
lpt的小屋 文章被收录于专栏

我想要一份甜甜的爱情

全部评论

相关推荐

12-19 15:04
门头沟学院 Java
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务