Code [VS] 1297 coins, codevs1297 coins

Source: Internet
Author: User

Code [VS] 1297 coins, codevs1297 coins
DescriptionDescription

We know that even coins with the same nominal value may have different weights because they are affected by many factors, including manufacturing processes and processes. However, the weight of any coin with a nominal value is always within a specific range. We now know the weight range of all nominal coins. Given the total weight of a pile of coins, ask how many different possibilities are there for the total value of the pile of coins. For example, it is known that the weight of a coin is between 19 and 21, and that the weight of a coin is between 40 and 43. The total weight of a pile of coins is 99. Then it can be composed of four 20 and one 19-cent coins with a total value of 5 cents, it can also be made up of one cent coin weighing 42 and three cent coins weighing 19. Its total value is 8 cents, alternatively, it may consist of two fifty-cent coins with a weight of 40 and one cent coin with a weight of 19. The total value is one cent and one cent. Therefore, there are three different possibilities for the total value of these coins.

Input description Input Description

The first line is an integer w (10 <= w <= 100) indicating the total weight of all coins. The second row is an integer n (1 <=n <= 7), indicating the total number of coins with different denominations. Each row in the next n rows contains three integers, indicating the nominal value of the coin in sequence, minimum possible weight and maximum possible weight. The nominal value of a coin shall not exceed 50, the minimum weight shall not be less than 2, and the maximum weight shall not exceed 100. The gap between the maximum weight and the minimum weight cannot exceed 30.

Output description Output Description

Only one line indicates the total value of the coin.

Sample Input Sample Input

99

2

1 19 21

5 40 43

Sample output Sample Output

3

DP + recursion, this question falls in the memory of the search, do not know to use two-dimensional array records, but use one-dimensional in the dead push. This question also makes me realize how many parameters the dp function has, so we should use several-dimensional arrays for memory.

# Include <bits/stdc ++. h> using namespace std; set <int> box; int countt = 0; int w [300]; int v [300]; int sum, n; int number = 1; int vis [102] [2555]; void dp (int money, int sum) {// I did not know how to use two-dimensional, one-dimensional vis [money] is not supported in many sum scenarios. If (vis [money] [sum]) return; // at this time, sum is the nominal value, put it in the set if (money = 0) {box. insert (sum); return;} vis [money] [sum] = 1; for (int I = 1; I <number; I ++) {if (money> = w [I]) {dp (money-w [I], sum + v [I]) ;}} int main () {scanf ("% d", & sum); scanf ("% d", & n); int value, l, r; for (int I = 1; I <= n; I ++) {scanf ("% d", & value, & l, & r); for (int j = l; j <= r; j ++) {w [number] = j; v [number ++] = value ;}} dp (sum, 0 ); // set size is the answer printf ("% d \ n", box. size ());}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.