pku2346 Lucky tickets

准确的说,这个题叫递推,不叫DP,

用f[i,j]表示长度为i,和为j的方案数

f[i,j]=∑f[i-1,j-k]

边界见程序。

View Code
 1 program pku2346(input,output);
 2 var
 3     f:array[0..5,0..45] of int64;
 4     answer:array[0..11] of int64;
 5     n:longint;
 6     i,j,k:longint;
 7 begin
 8     fillchar(f,sizeof(f),0);
 9     for i:=0 to 9 do
10         f[1,i]:=1;
11     for i:=1 to 5 do
12         for j:=0 to 45 do
13             if f[i-1,j]>0 then
14             for k:=0 to 9 do
15                 if j+k<=45 then
16                     f[i,j+k]:=f[i,j+k]+f[i-1,j];
17     answer[0]:=0;
18     for i:=1 to 5 do
19     begin    
20         answer[i]:=0;
21         for j:=0 to 45 do
22             inc(answer[i],f[i,j]*f[i,j]);
23     end;
24     readln(n);
25     writeln(answer[n>>1]);
26 end.
posted @ 2012-04-19 16:48  Codinginging  阅读(194)  评论(0编辑  收藏  举报