poj3181 (Dollar Dayz)

Source: Internet
Author: User
Tags dayz

Description

Farmer John goes to Dollar days at the Cow Store and discovers a unlimited number of tools on sale. During his first visit, the tools is selling variously for $, $, and $. Farmer John has exactly $ to spend. He can buy 5 tools at $1 tool at $ and a additional 1 tool at $. Of course, there is other combinations for a total of 5 different ways FJ can spend all he money on tools. Here they is:

1 @ us$3 + 1 @ us$2  1 @ us$3 + 2 @ us$1  1 @ us$2 + 3 @ us$1  2 @ us$2 + 1 @ us$1  5 @ us$1
Write a program than would compute the number of ways FJ can spend N dollars (1 <= n <=) at the Cow Store for OLS on sale with a cost of $ $K (1 <= K <= 100).

Input

A single line with the space-separated integers:n and K.

Output

A single, with a single integer, is the number of the unique ways FJ can spend.

Sample Input

5 3

Sample Output

5

Test instructions: How many ways to make an n in an integer in 1~m.

Method 1: Complete knapsack problem, pay attention to high precision.

Method 2: I'm using dynamic programming to split integers, and it's easier to think of them. Set Dp[n][m] Indicates the number of methods of n with an integer of not more than M, discussed in the following case:

M=0, dp[n][0]=0;

M>n, Dp[n][m]=dp[n][n];

0<m<=n, the number of methods is dp[n-m][m] If the largest integer is M, and if the maximum integer is less than M, the method number is dp[n][m-1]. So the total is dp[n][m]=dp[n-m][m]+dp[n][m-1].

The space can also be further optimized by using a scrolling array minus the second dimension.

The last is to pay attention to high precision, the large number is divided into two parts, the right half belongs to a long long range, the left half divided by 1e18 after another save, see Code.

#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using Namespace Std;typedef Long Long ll;const LL MOD = 1e18;    LL a[1005], B[1005];int main () {int n, m;    CIN >> n >> m;    A[0] = 1; for (int j = 1, J <= m; ++j) for (int i = j; i <= N; ++i) {b[i] = B[i-j] + b[i] + (A[I-J)            + a[i])/MOD;        A[i] = (A[i-j] + a[i])% MOD;    } if (B[n]) printf ("%lld", B[n]);    printf ("%lld\n", A[n]); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

poj3181 (Dollar Dayz)

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.