牛客IOI周赛22-普及组 C-照看小猫

在这里插入图片描述
题解:
主要考察容斥,组合数学。
我们可以先预处理出每个长度有多少个方案。因为不能有重复的名字,我们可以先对a数组排个序,那么不难看出,第i只猫前面的i-1只猫能取的名字一定在第i只猫能取的名字里面,因此,第i只猫能取的名字的方案数要减去i-1才是它真正的方案数。最后乘起来即可。

代码:

#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int MAXN=1e4+5;
const int mod=77797;
ll dp[15];
int a[MAXN];
int main()
{
    
    
    dp[1]=26;
    for(int i=2;i<=10;i++)
    {
    
    
        dp[i]=dp[i-1]*26;
    }
    for(int i=1;i<=10;i++)
    {
    
    
        dp[i]+=dp[i-1];
    }
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
        cin>>a[i];
    }
    ll cnt;
    int flag=0;
    sort(a+1,a+1+n);
    ll ans=1;
    for(int i=1;i<=n;i++)
    {
    
    
        cnt=dp[a[i]]-i+1;
        if(cnt<=0)
        {
    
    
            flag=1;
            break;
        }
        else
        {
    
    
            ans=ans*cnt%mod;
        }
    }
    if(flag) cout<<-1<<endl;
    else cout<<ans<<endl;
} 

猜你喜欢

转载自blog.csdn.net/weixin_45755679/article/details/113095726