C. New Year and Permutation

参考:Codeforces Round Hello 2020 A~E 题解

发现了一个网站OEIS,如果打表找规律的话会很方便,虽然这道题没有用上....

具体思路可看参考视频。

一般数学题都是打表找规律,如果找不出规律,例如像这种排列组合的问题,就可以考虑分情况讨论,最后进行累加即可。

代码:

// Created by CAD on 2020/1/6.
#include <bits/stdc++.h>
#define mst(name, value) memset(name,value,sizeof(name))
#define ll long long
using namespace std;

const int maxn=250005;
ll c[maxn];
int mod;
ll jc(ll x)
{
    if(~c[x]) return c[x];
    else return c[x]=x*jc(x-1)%mod;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;   cin>>n>>mod;
    mst(c,-1);
    c[0]=c[1]=1;
    ll ans=0;
    for(int i=1;i<=n;++i)
        ans=(ans+(n-i+1)*jc(i)%mod*jc(n-i+1)%mod+mod)%mod;
    cout<<ans<<endl;
    return 0;
}