POJ 3181 Dollar Dayz(高精度 动态规划)

题目链接:http://poj.org/problem?id=3181

题目大意:用1,2...K元的硬币,凑成N元的方案数。

Sample Input

5 3

Sample Output

5

分析:这不是母函数是什么,不管你信不信,反正我是信了。

  之前有过一篇,讲母函数的动态规划做法http://www.cnblogs.com/acm-bingzi/archive/2013/04/30/3051725.html

  这道题目,坑就坑在高精度上了,刚开始怎么也没想到,而且做法也很奇特。就是将2个long long 的数字进行拼接。

代码如下:

 1 # include<stdio.h>
 2 # include<string.h>
 3 # define maxn 1005
 4 long long int a[maxn],b[maxn],inf;
 5 int n,k;
 6 int main(){
 7     int i,j;
 8     while(scanf("%d%d",&n,&k)!=EOF){
 9         for(inf=1,i=0;i<18;i++)
10             inf *= 10;
11         memset(a,0,sizeof(a));
12         memset(b,0,sizeof(b));
13         a[0] = 1;
14         for(j=1;j<=k;j++)
15             for(i=1;i<=n;i++)
16                 if(i >= j) {
17                     b[i] = b[i] + b[i-j] + (a[i]+a[i-j])/inf;
18                     a[i] = (a[i]+a[i-j])%inf;
19                 } 
20                 if(b[n])
21                     printf("%lld",b[n]);
22                 printf("%lld
",a[n]);
23     }
24     return 0;
25 }
原文地址:https://www.cnblogs.com/acm-bingzi/p/3280071.html