题目链接:http://codeforces.com/contest/799/problem/D
因为${a_i>=2}$那么一个数字至多操作${log_{2}^{max(a,b)/min(h,w)}}$之后就会超过给定的${a,b}$,所以可以搜索,考虑复杂度问题我们就直接随机化,显然按照a_i的大小从大往小选。
辣鸡出题人没有把$h,w$旋转$90$度的情况放在PP里面,我的rating啊...
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<cstdlib> 6 #include<ctime> 7 #include<cmath> 8 #include<cstring> 9 using namespace std; 10 #define maxn 1001000 11 #define llg long long 12 #define inf 0x7fffffff 13 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); 14 llg n,m,a[maxn],c,d,w,h,ans=inf,f[maxn]; 15 16 bool cmp(llg a,llg b) {return a>b;} 17 18 inline int getint() 19 { 20 int w=0,q=0; char c=getchar(); 21 while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 22 while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w; 23 } 24 25 void work() 26 { 27 llg cs=0; 28 llg x=c,y=d; 29 while (x<h || y<w) 30 { 31 cs++; 32 if (cs>n) return ; 33 if (x>=h) {y*=a[cs]; continue;} 34 if (y>=w) {x*=a[cs]; continue;} 35 if (rand()%2) x*=a[cs];else y*=a[cs]; 36 } 37 ans=min(ans,cs); 38 } 39 40 int main() 41 { 42 yyj("D"); 43 cin>>h>>w>>c>>d>>n; 44 for (llg i=1;i<=n;i++) a[i]=getint(),f[a[i]]++; 45 sort(a+1,a+n+1,cmp); 46 // n=min(n,(llg)40); 47 while ((double)clock()/CLOCKS_PER_SEC<=0.91) work(),swap(h,w),work(),swap(h,w); 48 if (ans==inf) ans=-1; 49 cout<<ans; 50 51 return 0; 52 }