Codeforces Round #654 (Div.2)E1 Asterism

#include 
#include 
#include 
using namespace std;
const int maxn = 100010;
long long a[maxn];
vector<long long> v;
long long n,p;

long long f(long long x){
	long long ans = 1;
	long long r = n;
	for(long long i = n-1;i >= 0;i--){
		long long y = 0;
		if(a[i]-x>0) y = a[i]-x;
		if((r-y)%p == 0) return p;
		ans *= (r-y);
		ans %= p;//比赛时就差这一句话,取模 
		r--;
	}
	return ans;
}

int main(){
	cin >> n >> p;
	for(long long i = 0;i<n;i++){
		cin >> a[i];
	}
	sort(a,a+n);
	long long ans = 0;
	long long minl = a[0];
	long long maxl = a[n-1];
	long long m = 1;
	for(long long i = n-2;i >= 0;i--){
		if(a[i] == a[n-1]){
			m++;
		}
		else break;
	}
	long long u = max(maxl-n+m,minl);//最小可行x 
	for(long long i = u;i<maxl;i++){
		if(f(i)%p != 0){
			v.push_back(i);
			ans++;
		}
	}
	cout << ans << endl;
	if(!ans) return 0;
	for(long long i = 0;i<v.size() ;i++){
		if(i) cout << " ";
		cout << v[i];
	}
	cout << endl;
	return 0;
}

一定注意要加上**ans %= p;**这句话,我比赛时就因为少写这一句话以至于没有AC。

你可能感兴趣的:(ACM-ICPC算法,codeforces)