Title Link: http://www.lightoj.com/volume_showproblem.php?problem=1235
Test instructions: There are N coins (n<=18), asked whether the use of each coin is not more than two times the case to pay the nominal value of just K.
Idea: DFS constructs all the expenses that the first half of these coins can pay and all the expenses that the last half can pay. After the sort, enumerate the first half of each face value in the second inside two points to look for. (or save with set).
Code: (SET)
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace STD;intNintNUM1, num2;Long LongWLong Longa[1000000]; Set <long long>S1; Set <long long>S2;voidDFS1 (Long LongSumintXintNUM) {if(x = = NUM1) {S1.insert (sum);return; } for(inti =0; I <=2; i++) DFS1 (sum + a[x] * i,x +1, Num +1);}voidDFS2 (Long LongSumintXintNUM) {if(x = = N) {S2.insert (sum);return; } for(inti =0; I <=2; i++) dfs2 (sum + a[x] * I, X +1, Num +1);}intMain () {intTscanf("%d", &t);intCases =1; while(t--) {s1.clear (); S2.clear ();memsetA0,sizeof(a));scanf("%d%lld", &n, &w); for(inti =0; I < n; i++)scanf("%lld", &a[i]); NUM1 = n >>1; num2 = N-NUM1; DFS1 (0,0, NUM1); DFS2 (0, NUM1, N);intOK =0; Set <long long>:: Iterator it; for(it = S1.begin (); It! = S1.end (); it++) {intTMP = *it;//cout << tmp << Endl; if(S2.count (w-tmp)! =0) {OK =1; Break; } }if(OK)printf("Case%d:yes\n", cases++);Else printf("Case%d:no\n", cases++); }return 0;}
Copyright NOTICE: Reprint please indicate the source.
Lightoj 1235-coin Change (IV) "Two points"