# Codeforces Global Round 24 D()-其他

## Codeforces Global Round 24 D()

### D. Doremy’s Pegging Game

``````int a[N],b[N],p;
int qmi(int a,int k,int p){
int res=1;
while(k){
if(k&1)res=(res*a)%p;
k>>=1;
a=a*a%p;
}
return res;
}
int C(int x,int y){
if(x<0||y<0||x<y)return 0;
return a[x]*b[y]%p*b[x-y]%p;
}
vector<int>A(N);
void init(){
a[0]=b[0]=1;
for(int i=1;i<=1e5;i++){
a[i]=(a[i-1]*i)%p;
b[i]=b[i-1]*qmi(i,p-2,p)%p;
}
}
void solve(){
int n;cin>>n>>p;
init();
A[0]=1;
for(int i=1;i<=n;i++)A[i]=A[i-1]*i%p;
vector<int>sum(n+10);
for(int x=0;x<=n-3;x++){
for(int j=0;j<=x;j++){
(sum[x]+=C(x,j)*A[n-x-3+j]%p)%=p;
}
}
int ans=0;
for(int i=2;i<=n/2+1;i++){
for(int j=i+1;j<=n;j++){
int x=j-i;
if(x<up(n,2)&&j>up(n,2)){
(ans+=sum[x-1])%=p;
}
}
}
if(n%2==0){
(ans+=A[n-2])%=p;
}
cout<<ans*n%p<<endl;
}
``````
————————

### D. Doremy’s Pegging Game

``````int a[N],b[N],p;
int qmi(int a,int k,int p){
int res=1;
while(k){
if(k&1)res=(res*a)%p;
k>>=1;
a=a*a%p;
}
return res;
}
int C(int x,int y){
if(x<0||y<0||x<y)return 0;
return a[x]*b[y]%p*b[x-y]%p;
}
vector<int>A(N);
void init(){
a[0]=b[0]=1;
for(int i=1;i<=1e5;i++){
a[i]=(a[i-1]*i)%p;
b[i]=b[i-1]*qmi(i,p-2,p)%p;
}
}
void solve(){
int n;cin>>n>>p;
init();
A[0]=1;
for(int i=1;i<=n;i++)A[i]=A[i-1]*i%p;
vector<int>sum(n+10);
for(int x=0;x<=n-3;x++){
for(int j=0;j<=x;j++){
(sum[x]+=C(x,j)*A[n-x-3+j]%p)%=p;
}
}
int ans=0;
for(int i=2;i<=n/2+1;i++){
for(int j=i+1;j<=n;j++){
int x=j-i;
if(x<up(n,2)&&j>up(n,2)){
(ans+=sum[x-1])%=p;
}
}
}
if(n%2==0){
(ans+=A[n-2])%=p;
}
cout<<ans*n%p<<endl;
}
``````