P5520 [yLOI2019] 青原櫻 (組合數學)

題目傳送門

題意: 給你一個長度爲n的路,m棵互不相同的樹,要求每兩顆數之間必須留空一個位置,問你有多少種方法種樹。

思路: 因爲兩顆樹之間必須有空隔,一共m棵樹也就是m-1個空隔,我們把這些空隔抽出來,於是還剩下n-m+1個位置,我們只需要在n-m+1個位置中選取m個位置,最後把m-1個空隔插入就行了,注意這題是排列就行。

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e7+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-7;
const double PI=acos(-1);
int a(int m,int n,int p)
{
    int res=1;
    for(int i=n-m+1;i<=n;i++)
        res=res*i%p;
    return res;
}
signed main()
{
    int type,n,m,p;
    cin>>type>>n>>m>>p;
    cout<<a(m,n-m+1,p)<<endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章