- Say this is our school summer camp finished a problem, just see thought is to water problem, later found the complexity is too big, can not stand, the game finished also did not get out, then said this is binary enumeration. Then the touch of the study, and then wrote the word,
- The so-called binary enumeration is to calculate the first half, then half, and then use the two-point search to see if we can get this state, if it is possible,
- Test instructions: Give you two number n,k, the following gives you the number of N, indicating that you now have the face value of the coin, each coin face value of two, see if you can pay K
- Solution ideas: First thought that only three states, the direct Dfs is good, and later found that the complexity is too big, and then the dead is the above, the details of the test code
- Source code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int a[28];long long a1[20 005],a2[20005];int s,e;int t;int t1,t2;int n,k;void dfs (int x,long long v) {if (x==e) {if (e==n) a1[t1++]=v; else if (E==N/2) a2[t2++]=v; return; } for (int i=0; i<=2; i++) dfs (x+1,v+i*a[x]);} int main () {while (scanf ("%d", &t) ==1) {int cases=1; while (t--) {memset (a1,0,sizeof (A1)); Memset (a2,0,sizeof (A2)); scanf ("%d%d", &n,&k); for (int i=0; i<n; i++) scanf ("%d", &a[i]); t1=t2=0; E=N/2; DFS (0,0); E=n; DFS (n/2,0); Sort (A2,A2+T2); int j,l,mid,r; for (j=0; j<t1; J + +) {long long h=k-a1[j]; l=0; r=t2-1; while (l<=r) {mid = (l+r) >>1; if (a2[mid]==h)Break if (a2[mid]>h) r=mid-1; else l=mid+1; } if (l<=r) break; } if (J<T1) printf ("Case%d:yes\n", cases++); else printf ("Case%d:no\n", cases++); } }}
Lightoj 1235 Coin Change (IV) (binary enumeration)