E1. Asterism (Easy Version)
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
This is the easy version of the problem. The difference between versions is the constraints on n and ai. You can make hacks only if all versions of the problem are solved.
First, Aoi came up with the following idea for the competitive programming problem:
Yuzu is a girl who collecting candies. Originally, she has x candies. There are also n enemies numbered with integers from 1 to n. Enemy i has ai candies.
Yuzu is going to determine a permutation P. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, {2,3,1,5,4} is a permutation, but {1,2,2} is not a permutation (2 appears twice in the array) and {1,3,4} is also not a permutation (because n=3 but there is the number 4 in the array).
After that, she will do n duels with the enemies with the following rules:
If Yuzu has equal or more number of candies than enemy Pi, she wins the duel and gets 1 candy. Otherwise, she loses the duel and gets nothing.
The candy which Yuzu gets will be used in the next duels.
Yuzu wants to win all duels. How many valid permutations P exist?
This problem was easy and wasn’t interesting for Akari, who is a friend of Aoi. And Akari made the following problem from the above idea:
Let’s define f(x) as the number of valid permutations for the integer x.
You are given n, a and a prime number p≤n. Let’s call a positive integer x good, if the value f(x) is not divisible by p. Find all good integers x.
Your task is to solve this problem made by Akari.
Input
The first line contains two integers n, p (2≤p≤n≤2000). It is guaranteed, that the number p is prime (it has exactly two divisors 1 and p).
The second line contains n integers a1,a2,…,an (1≤ai≤2000).
Output
In the first line, print the number of good integers x.
In the second line, output all good integers x in the ascending order.
It is guaranteed that the number of good integers x does not exceed 105.
Examples
inputCopy
3 2
3 4 5
outputCopy
1
3
inputCopy
4 3
2 3 5 6
outputCopy
2
3 4
inputCopy
4 3
9 1 1 1
outputCopy
0
Note
In the first test, p=2.
If x≤2, there are no valid permutations for Yuzu. So f(x)=0 for all x≤2. The number 0 is divisible by 2, so all integers x≤2 are not good.
If x=3, {1,2,3} is the only valid permutation for Yuzu. So f(3)=1, so the number 3 is good.
If x=4, {1,2,3},{1,3,2},{2,1,3},{2,3,1} are all valid permutations for Yuzu. So f(4)=4, so the number 4 is not good.
If x≥5, all 6 permutations are valid for Yuzu. So f(x)=6 for all x≥5, so all integers x≥5 are not good.
So, the only good number is 3.
In the third test, for all positive integers x the value f(x) is divisible by p=3.
题目大意:
给出n和p,一个初始值x 进行n次比较,如果x > a[i],则x++,要让x每次比较都大于ai 求ai的排列方式有多少种,并且f(x) % p != 0 其中f(x)表示初始值为x时的ai数组排列方式。
题目分析:
- 设a数组最大值为m,当x大于m时 无论怎么排列a数组 都是可行的 答案数为n! 但此时n > p 所以n! % p == 0 不计入答案
- 当x + n - 1 < m 时,对于最大的m肯定不能满足 此时也是不符合答案的情况
- 所以做法是 枚举 x-n-1 到 m 求出 每个 f(x),看是否满足题意。复杂度 (n*n);
- 求 f(x) ,总共有 n 个位置 从 m-n+1 到 m ,对应的值分别为 x到 m 。所以贪心的从大的开始安排,每个 a[i]只能安排到对应值比a[i]大或者等于的位置 。把最大的安排到可以满足的位置 一共 x+n-a[i] 个 第二大的数有 x+n-1-a[i]个位置。当 a[i]<=x 可以随便放。最后看所有可能 是否可以整除 p。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define Maxn 2005
using namespace std;
// reverse(a+1,a+n+1); reverse 函数功能: 翻转数组区间
int a[Maxn],n,ModP;
inline bool check(int x) {
int Ans = 1,sum = n;// 此处的函数注意倒序检查满足答案的数字 然后sum其实可以用i代替·
for(int i=n; i>=1; i--) {
if(a[i] <= x) Ans = (Ans * sum) % ModP;
else if(x + sum <= a[i]) return 0;
else Ans = (Ans * (x + sum - a[i])) % ModP;
sum--;
}
if(Ans == 0) return 0;
return 1;
}
int main() {
scanf("%d %d",&n,&ModP);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
sort(a + 1, a + n + 1);
vector<int> ans;
for(int i=max(a[n] - n + 1,1); i<a[n]; i++)
if(check(i)) ans.push_back(i);
cout << ans.size() << endl;
for(auto i: ans) cout << i << ' ';
return 0;
}