Poj--3181--dollar dayz--backpack/High precision

Source: Internet
Author: User
Tags dayz

Dollar Dayz
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4220 Accepted: 1642

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: The input n,k denotes k coins, the value is 1~k, and the number of programs that use these coins to combine value n

Resolution: The value is very large, 64 bits can not fit, so with two 64-bit respectively as high and low to solve, then the complete knapsack problem

Here's a point I want to say, although not wrong, but this is the problem of the loopholes with the thinking of rigorous, that is, when there is a high need to output a bit to fill up, is generally 1E18 for the touch, so generally are high on the output high, and then the low output, I said if the low level is not 18 full?

Take 100来 said, High is 1, low is 99, this good, output to direct is 199, if the low is 9? Output is 19, low-leading 0 did not handle ah, some of the correct code is based on the data is not rigorous AH.


#include <iostream>#include <cstdio>#include <cstring>#define Max(A,B)A>B?A:Busing namespaceStd;__int64MoD=1000000000000000000;int Main(void){    intN,M,I,J,K,L;    __int64Dp[2][1111];     while(scanf("%d%d",&N,&M)!=Eof)    {        memset(Dp,0,sizeof(Dp));Dp[0][0]=1;        if(M>N)M=N;         for(I=1;I<=M;I++)         for(J=I;J<=N;J++)        {Dp[1][J]=Dp[1][J-I]+Dp[1][J]+(Dp[0][J-I]+Dp[0][J])/MoD; /  /HighDp[0][J]=(Dp[0][J-I]+Dp[0][J])%MoD; /  /Low        }        if(Dp[1][N])        {            printf("%i64d",Dp[1][N]);            printf("%018i64d\ n",Dp[0][N]); /  /supplementary preamble 0        }Else        {            printf("%i64d\ n",Dp[0][N]); /  /This does not add leading 0        }    }    return 0;}

Poj--3181--dollar dayz--backpack/High precision

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.