1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include<bits/stdc++.h> #include "books.h" using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair <int,int> Pii; #define reg register #define mp make_pair #define pb push_back #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
void solve(int n,int k,ll L,int m) { static const int N=1e6+10; assert(m>=40); if(k>n) return impossible(); static ll a[N]; rep(i,1,n) a[i]=-1; ll s=0; rep(i,1,k-1) s+=a[i]=skim(i); if(s>2*L) return impossible(); int l=k,r=n,res=-1; while(l<=r) { int mid=(l+r)>>1; a[mid]=skim(mid); if(s+a[mid]>=L && s+a[mid]<=2*L) { vector <int> t; rep(i,1,k-1) t.pb(i); t.pb(mid); return answer(t); } if(s+a[mid]>2*L) r=mid-1; else l=mid+1,res=mid; } if(res==-1) return impossible(); if(a[k]==-1) a[k]=skim(k); rep(i,max(k+1,res-k+1),res) if(a[i]==-1) a[i]=skim(i); static int pos[N],c=0; rep(i,1,res) if(~a[i] && (i<=k || i>res-k)) pos[c++]=i; rep(S,0,(1<<c)-1) if(__builtin_popcount(S)==k) { ll v=0; rep(i,0,c-1) if(S&(1<<i)) v+=a[pos[i]]; if(v>=L && v<=2*L) { vector <int> t; rep(i,0,c-1) if(S&(1<<i)) t.pb(pos[i]); return answer(t); } } return impossible(); }
|