Dollar Dayz
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 2
Problem Description
Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:
1 @ US$3 + 1 @ US$2Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).
1 @ US$3 + 2 @ US$1
1 @ US$2 + 3 @ US$1
2 @ US$2 + 1 @ US$1
5 @ US$1
Input
A single line with two space-separated integers: N and K.
Output
A single line with a single integer that is the number of unique ways FJ can spend his money.
Sample Input
5 3
Sample Output
5
居然是大数,我没用到。
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #define inf 0x3f3f3f3f 6 #define mod 1000000000000000000; 7 using namespace std; 8 int main() 9 { 10 long long dp[10005]; 11 long long n, k; 12 cin >> n >> k; 13 long long i, j, x; 14 for (i = 1; i <= n; i++) dp[i] = 1; 15 dp[0] = 1; 16 for (i = 2; i <= k; i++) 17 { 18 x = n / i; 19 for (long long l = 1; l <= x; l++) 20 { 21 for (j = n; j>=1; j--) 22 { 23 if (j - i * l >= 0 && dp[j - i * l] != 0) 24 dp[j] = (dp[j]+dp[j - i * l])%mod; 25 } 26 } 27 } 28 cout << dp[n] << endl; 29 }
别人的完全背包+大数代码
1 #include<stdio.h> 2 #include<string.h> 3 long long inf; 4 long long a[1010],b[1010]; 5 int main() 6 { 7 int i,j,n,k; 8 inf=1; 9 for(i=0;i<18;i++) 10 inf*=10; 11 while(~scanf("%d%d",&n,&k)) 12 { 13 memset(a,0,sizeof(a)); 14 memset(b,0,sizeof(b)); 15 a[0]=1; 16 for(i=1;i<=k;i++) 17 { 18 for(j=1;j<=n;j++) 19 { 20 if(i>j) 21 continue; 22 b[j]=b[j]+b[j-i]+(a[j]+a[j-i])/inf; 23 a[j]=(a[j]+a[j-i])%inf; 24 } 25 } 26 if(b[n]) printf("%lld",b[n]); 27 printf("%lld ",a[n]); 28 } 29 return 0; 30 }
1 <strong>#include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define Max(a,b) a>b?a:b 5 using namespace std; 6 __int64 mod=1000000000000000000; 7 int main (void) 8 { 9 int n,m,i,j,k,l; 10 __int64 dp[2][1111]; 11 while(scanf("%d%d",&n,&m)!=EOF) 12 { 13 memset(dp,0,sizeof(dp)); 14 dp[0][0]=1; 15 if(m>n)m=n; 16 for(i=1;i<=m;i++) 17 for(j=i;j<=n;j++) 18 { 19 dp[1][j]=dp[1][j-i]+dp[1][j]+(dp[0][j-i]+dp[0][j])/mod; //高位 20 dp[0][j]=(dp[0][j-i]+dp[0][j])%mod; //低位 21 } 22 if(dp[1][n]) 23 { 24 printf("%I64d",dp[1][n]); 25 printf("%018I64d ",dp[0][n]); //补充前导零 26 }else 27 { 28 printf("%I64d ",dp[0][n]); //这个不用补充前导零 29 } 30 } 31 return 0; 32 }