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;
}

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