[USACO09MAR]Cow Frisbee Team

 嘟嘟嘟

 

这个是一个很明显的dp,遇到这种倍数的问题的,就令dp[i][j]表示选到了第 i 只牛(不是选了 i 只牛),sum(Ri) % f == j 的方案数,则,

    dp[i][j] = dp[i - 1][j] + dp[i - 1][(j + f - a[i] % f) % f]

等式右边第一项表示第 i 只牛不选,第二项表示第 i 只牛选了,j + f 是为了防止出现负数。

初始化令dp[0][0] = 1,但实际上这个状态应该是0,所以随后答案是dp[n][0] - 1.

[USACO09MAR]Cow Frisbee Team[USACO09MAR]Cow Frisbee Team
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("") 
13 #define space putchar(' ')
14 #define Mem(a) memset(a, 0, sizeof(a))
15 typedef long long ll;
16 typedef double db;
17 const int INF = 0x3f3f3f3f;
18 const db eps = 1e-8;
19 const int mod = 1e8;
20 const int maxn = 1e3 + 5;
21 inline ll read()
22 {
23     ll ans = 0;
24     char ch = getchar(), last = ' ';
25     while(!isdigit(ch)) {last = ch; ch = getchar();}
26     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
27     if(last == '-') ans = -ans;
28     return ans;
29 }
30 inline void write(ll x)
31 {
32     if(x < 0) x = -x, putchar('-');
33     if(x >= 10) write(x / 10);
34     putchar(x % 10 + '0');
35 }
36 
37 int n, f, a[maxn << 1];
38 int dp[maxn << 1][maxn];
39 
40 int main()
41 {
42     n = read(); f = read();
43     for(int i = 1; i <= n; ++i) a[i] = read();
44     dp[0][0] = 1;
45     for(int i = 1; i <= n; ++i)
46         for(int j = f - 1; j >= 0; --j)
47         {
48             dp[i][j] = dp[i - 1][j] + dp[i - 1][(j + f - a[i] % f) % f];
49             dp[i][j] %= mod;
50         }
51     write(dp[n][0] - 1); enter;
52     return 0;
53 }
View Code

 

上一篇:P1588 [USACO07OPEN]Catch That Cow S


下一篇:题目二:隐式图的搜索问题(A*算法解决八数码)(实验准备)