CF1371E2 solution
前言
想出了一个很简洁的做法,看了一遍,题解区没有相应的题解。
思路
首先将
考虑对于
- 最小的满足条件的
x 为a_i-i+1 ,这是显然的。 - 最大的满足条件的
x :- 当
i\in [1,p) 时为+\infty 。 - 证明:此时
p\nmid f(+\infty)=i! 。 - 当
i\in [p,n] 时为a_i-i+p-1(i \in [p,n]) 。 - 证明:此时
p \nmid f(a_i-i+p-1)=C_i^{p-1}\times (p-1)! 且p | f(a_i-i+p)=C_i^p\times p! 。
- 当
综上,答案即为
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=100100,inf=0x7fffffffffffffff;
int n,p,a[N],l=0,r=inf;
signed main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>p;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
l=max(l,a[i]-i+1);
if(i>=p) r=min(r,a[i]-i+p);
}
cout<<max(r-l,0ll)<<endl;
for(int i=l;i<r;i++) cout<<i<<' ';
return 0;
}