poj 3761 Bubble Sort_快速幂

时间:2021-02-15 06:41:53

题意:问你冒泡排序第i次排序,一共排了多少次

套公式K!((K + 1) ^ (N - K) - K ^ (N - K))

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
#define N 1000010
#define M 20100713
LL a[N];
int _pow(LL v,int k){
LL res=1;
while(k){
if(k&1){
res=res*v;
res%=M;
}
v=v*v;
v%=M;
k>>=1;
}
return res;
}
int main(int argc, char** argv) {
int t,i,n,k;
a[0]=a[1]=1;
for(i=2;i<N;i++)
a[i]=a[i-1]*i%M;
scanf("%d",&t);
while(t--){
LL sum=0;
scanf("%d%d",&n,&k);
sum=(_pow(k+1,n-k)-_pow(k,n-k))%M;
if(sum<0)
sum+=M;
printf("%lld\n",sum*a[k]%M);
}
return 0;
}