Lightoj 1235-coin Change (IV) "Two points"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.