HDU 1258 Sum It Up (POJ 1564)

Source: Internet
Author: User

I have done it before, and I happen to see it. I copied it. Very strange ...... Send the POJ program to HDU and then WA.

Then rewrite it. The HDU program is handed in AC and then the POJ is handed in. Just ......


Simple DFS, just fine.


HDU:

# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <vector> # include <cmath> # define INF 0x7fffffff # define eps 1e-8 # define LL long # define PI 3.141592654 # define CLR (, b) memset (a, B, sizeof (a) # define FOR (I, a, B) for (int I = a; I <B; I ++) # define FOR _ (I, a, B) for (int I = a; I> = B; I --) # define rep (I,, B) for (int I = a; B>? I <B: I> = B; B>? I ++: I --) # define pb push_back # define mp make_pair # define ft first # define sd second # define sf scanf # define pf printf # define sz (v) (int) (v ). size () # define all (v ). begin (), (v ). end () # define acfun std: ios: sync_with_stdio (false) # define SIZE 100000000 + 1 using namespace std; int n, m, a [12], B [12]; bool vis [12]; bool cmp (int a, int B) {return a> B;} vector <string> ha; void dfs (int deep, int sum) {if (sum> m) return; If (sum = m) {// pf ("(% d = % d) \ n", sum, m, deep); int out [12]; memcpy (out, B, sizeof (B); sort (out, out + deep, cmp); string ch; ch. clear (); FOR (I, 0, deep) {char tmp [11]; sprintf (tmp, "% d", out [I]); ch. append (tmp);} FOR (I, 0, sz (ha) if (ha [I] = ch) return; ha. pb (ch); FOR (I, 0, deep-1) pf ("% d +", out [I]); pf ("% d \ n ", out [deep-1]);} FOR (I, 0, n) {if (vis [I]) continue; vis [I] = 1; B [deep] = a [I]; dfs (deep + 1, sum + a [I]); vis [I] = 0 ;}} int main () {while (~ Sf ("% d", & m, & n), n | m) {FOR (I, 0, n) sf ("% d ", & a [I]); CLR (vis, 0); ha. clear (); pf ("Sums of % d: \ n", m); dfs (0, 0); if (ha. empty () puts ("NONE ");}}

POJ. Previously renewed.


# Include <cstdio> # include <cstring> int sum, n, a [10001]; int vis [10001], OK; int ck [10001], top = 0; int check (int ans) {int I; for (I = 0; I <top; I ++) if (ck [I] = ans) return 0; ck [top ++] = ans; return 1;} void dfs (int s, int j) {int I; if (s = sum) {int o = 0; OK = 1; int num = 0, j = 1; for (I = 0; I <n; I ++) {if (vis [I]) num = num + a [I] * j, j ++;} if (! Check (num) return; for (I = 0; I <n; I ++) {if (vis [I] &! O) printf ("% d", a [I]), o = 1; else if (vis [I]) printf ("+ % d ", a [I]);} printf ("\ n");} else {for (I = j; I <n; I ++) if (s + a [I] <= sum &&! Vis [I]) vis [I] = 1, dfs (s + a [I], I + 1), vis [I] = 0 ;}} int main () {int I; while (scanf ("% d", & sum, & n), sum! = 0, n! = 0) {memset (vis, 0, sizeof (vis); for (I = 0; I <n; I ++) scanf ("% d ", & a [I]); printf ("Sums of % d: \ n", sum); OK = 0; top = 0; dfs (0, 0); if (! OK) printf ("NONE \ n ");}}



HDU 1258 Sum It Up (POJ 1564)

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.