HDU 1709 (primary function)

Source: Internet
Author: User

/*

Question: give a certain (n) Number of weights. Each weight is a1, a2, a3... An,

Question: Find [1 ~ Sum (sum is the sum of weights) cannot be weighed;

Key Issue: You can place weights on both sides of the balance,

Put on the same end: a [j + k] + = a [j] different end a [abs (j-k)] + = a [j]

Assuming that all the original weights are placed on the right side, you can place the newly added weights on the left side to obtain the new weight. In this case, a [abs (j-k)] + = a [j].

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1709 click to open the link

*/

# Include <iostream> # include <cstring> # include <cmath> using namespace std; # define maxn 101 int a [maxn * maxn], B [maxn * maxn], c [maxn]; int main () {int n; while (cin> n) {int sum = 0; for (int I = 0; I <n; I ++) {cin> c [I]; sum + = c [I];} memset (a, 0, sizeof (a); memset (B, 0, sizeof (B); for (int I = 0; I <= c [0]; I + = c [0]) a [I] = 1; // int now = c [0]; for (int I = 1; I <n; I ++) // 1. I Represents the I expression {for (int j = 0; j <= sum; j ++) // 2. J indicates the I entry of each expression {for (int k = 0; k <= c [I]; k + = c [I]) // k indicates the index of item I of each expression {B [j + k] + = a [j]; B [abs (j-k)] + = a [j] ;}// now + = c [I]; for (int j = 0; j <= sum; j ++) {a [j] = B [j]; B [j] = 0 ;}} int num = 0; for (int I = 0; I <= sum; I ++) {if (! A [I]) num ++;} cout <num <endl; int p = 1; if (num) {for (int I = 0; I <= sum; I ++) {if (! A [I]) {if (! P) cout <"<I; else {cout <I; p = 0 ;}}} cout <endl ;}}}

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.