Question: Here is a piece of rope that allows you to cut the rope into any segment consisting of A, B, and C to obtain the maximum number of segments;
Solution: Infinite backpacks
Solution code:
1 // file name: 189a. CPP 2 // Author: darkdream 3 // created time: thursday, July 24, 2014, 26 seconds, 4 5 # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # include <cmath> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 25 using namespace STD; 26 int ans [4] [1, 4005]; 27 int main () {28 int n, a [4]; 29 memset (ANS, 0, sizeof (ANS )); 30 scanf ("% d", & N, & A [1], & A [2], & A [3]); 31 ans [0] [0] = 1; 32 for (INT I = 1; I <= 3; I ++) 33 {34 for (Int J = 0; j <= 4000; j ++) 35 {36 IF (j <A [I] | ans [I] [J-A [I] = 0) 37 ans [I] [J] = ans [I-1] [J]; 38 else {39 ans [I] [J] = max (ANS [I] [J-A [I] + 1, ANS [I-1] [J]); 40} 41} 42 43} 44/* For (INT I = 1; I <= 3; I ++) 45 {46 for (Int J = 1; j <= 7; j ++) 47 printf ("% d", ANS [I] [J]); 48 printf ("\ n "); 49} */50 printf ("% d \ n", ANS [3] [N]-1); 51 52 53 return 0; 54}
View code