Asterism (Easy Version)
来源:CodeForces - 1371E1
题目描述:
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.
输入格式:
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).
输出格式:
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.
输入样例:
4 3
2 3 5 6
输出样例:
2
3 4
思路:
根据题目要求,先对每个敌人的糖果数进行升序排序。要想打败所有敌人,则初始糖果数x必须满足:x + i >= a[i],其中i为位置下标(0 <= i < n),a[i]为第i个敌人的糖果数。
一共有n个位置,首先求出第一个位置所需的最少糖果数量。如果初始糖果数量x大于等于a[n -1],那么f(x) = n!,又2 <= p <= n,则f(x)必是p的倍数,不符合题意,故x < a[n - 1],由此确定了初始糖果数量x的上界与下界,在此范围内枚举x即可,然后判断x是否满足条件。
判断给定的x是否满足条件:遍历n个位置,对于每一个位置,求出当前位置上满足小于等于x + i的a数组中元素的个数cnt,cnt再减去i,最后得到的就是当前位置上满足题意的a数组中元素个数(由乘法原理可得)。如果该数量是p的倍数,则f(x)一定是p的倍数,可中途退出。最后把满足条件的x存储起来,在最后输出即可。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
void init ()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
vector<int> a;
set<int> st;
int main ()
{
init();
int n, p;
cin >> n >> p;
a.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
int start = -1;
for (int i = 0; i < n; i++) start = max(start, a[i] - i);
for (int x = start; x < a[n - 1]; x++)
{
bool ok = true;
for (int i = 0; i < n; i++)
{
int cnt = upper_bound(a.begin(), a.end(), x + i) - a.begin();
cnt -= i;
if (cnt % p == 0)
{
ok = false;
break;
}
}
if (ok) st.insert(x);
}
cout << st.size() << endl;
for (const auto& e : st) cout << e << " ";
return 0;
}