Niuke IOI Weekly Contest 22-Publication group takes care of kittens (c++)

Babysitting

Topic link

In a sunny afternoon, Shao Zuo arranged a task for Violet Evegarden.
The task is roughly like this. In the next week, Violet needs to take care of N kittens. In order to facilitate the management of these N cats,
Violet prepares to give each cat a unique name. The name must be approved by Ben Mi, the cat.
But these cats are very personal and will never accept long names.
Now give the maximum length of the name that each cat can tolerate.
Please name all the cats, how many different schemes there are.
The name contains only lowercase English letters. The result may be very large, so please take the result modulo 77797.
If there is no solution, please output -1

Problem-solving ideas:

I was really struggling with this question at first. . . Dishes are the original sin.
Suddenly it occurred to us that if we sort the cats to be named in ascending order according to the maximum length of their names, then each time we calculate the following cats, we only need to subtract one of the previous cats! !

Please see the solution for details~

AC code

#include <cstdio>
#include <algorithm>
using namespace std;
int p[15]= {
    
    1};//让数组第一个元素等于1,那么从第二个元素(也就是a[1]开始方案数*26)
int a[10005];
int main() {
    
    
	int n;
	scanf("%d",&n);//记录猫咪数量
	for(int i=1; i<=n; i++)
		scanf("%d",&a[i]);//记录每只猫咪的最长容忍名字长度上限
	sort(a+1,a+1+n);
	for(int i=1; i<=10; i++) {
    
    
		p[i]=p[i-1]*26%77797;
	}
	for(int i=2; i<=10; i++)
		p[i]=(p[i-1]+p[i])%77797;
		//这是最重要的一点!!对于10个字以内的名字方案进行记录
	long long int ans=1;
	for(int i=1; i<=n; i++) {
    
    
	ans=(p[a[i]]-(i-1))*ans%77797;//第二只猫咪开始,减去上一只猫咪用掉的一个方案(叠加)
	}
	if(ans<=0) {
    
    
		printf("-1");//如果答案小于等于0,要输出-1;
		return 0;
	}
	printf("%lld",ans);//输出答案
}

Attachment: If you are a newcomer, if you have any shortcomings, please correct me. If there are errors in the solution or not clearly written, you are welcome to ask questions in the comment area~

Guess you like

Origin blog.csdn.net/qq_34832548/article/details/113095721
Recommended