1.快速幂
ll po(ll a,ll b) //快速幂算法
{
ll ans = 1;
while(b)
{
if(b & 1)
{
ans = ans * a % mod;
}
b >>= 1;
a = a * a % mod;
}
return ans;
}
2.组合数
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e5+5;
const int mod=998244353;
ll inv[maxn], fac[maxn];
ll quickPow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%mod;
b>>=1;
a=(a*a)%mod;
}
return ans;
}
void init()
{
//求阶乘
fac[0]=1;
for(int i=1;i<maxn;i++)
{
fac[i]=fac[i-1]*i%mod;
}
//求逆元
inv[maxn-1]=quickPow(fac[maxn-1],mod-2);
for(int i=maxn-2;i>=0;i--)
{
inv[i]=inv[i+1]*(i+1)%mod;
}
}
ll C(int n,int m)
{
if(m>n)
return 0;
if(m==0)
return 1;
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
3.求逆序数
查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 600050;
int num[N],n,m;
deque<int>q;
struct node{
int val;
int id;
}a[600010];
int lowbit(int x){
return x&(-x);
}
void add(int x){
while(x<=n){
num[x]++;
x+=lowbit(x);
}
}
int sum(int x){
int ans=0;
while(x>0){
ans+=num[x];
x-=lowbit(x);
}
return ans;
}
int cmp(node x,node y){
if(x.val==y.val) return x.id<y.id;
return x.val<y.val;
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> a[i].val, a[i].id = i;
for (int i = 1; i <= n; i++)q.push_back(a[i].val);
sort(a + 1, a + n + 1, cmp);
int cnt = 0;
for (int i = 1; i <= n; i++) {
add(a[i].id);
cnt += i - sum(a[i].id);
}
cout << cnt << endl;
return 0;
}