首页 > 代码库 > P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team
P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team
P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team
题目描述
After Farmer Don took up Frisbee, Farmer John wanted to join in the fun. He wants to form a Frisbee team from his N cows (1 <= N <= 2,000) conveniently numbered 1..N. The cows have been practicing flipping the discs around, and each cow i has a rating R_i (1 <= R_i <= 100,000) denoting her skill playing Frisbee. FJ can form a team by choosing one or more of his cows.
However, because FJ needs to be very selective when forming Frisbee teams, he has added an additional constraint. Since his favorite number is F (1 <= F <= 1,000), he will only accept a team if the sum of the ratings of each cow in the team is exactly divisible by F.
Help FJ find out how many different teams he can choose. Since this number can be very large, output the answer modulo 100,000,000.
Note: about 50% of the test data will have N <= 19.
农夫顿因开始玩飞盘之后,约翰也打算让奶牛们享受飞盘的乐趣.他要组建一只奶牛飞盘
队.他的N(1≤N≤2000)只奶牛,每只部有一个飞盘水准指数Ri(1≤Ri≤100000).约翰要选出1只或多于1只奶牛来参加他的飞盘队.由于约翰的幸运数字是F(1≤F≤1000),他希望所有奶牛的飞盘水准指数之和是幸运数字的倍数.
帮约翰算算一共有多少种组队方式.
输入输出格式
输入格式:Line 1: Two space-separated integers: N and F
- Lines 2..N+1: Line i+1 contains a single integer: R_i
- Line 1: A single integer representing the number of teams FJ can choose, modulo 100,000,000.
输入输出样例
4 5 1 2 8 2
3
说明
FJ has four cows whose ratings are 1, 2, 8, and 2. He will only accept a team whose rating sum is a multiple of 5.
FJ can pair the 8 and either of the 2‘s (8 + 2 = 10), or he can use both 2‘s and the 1 (2 + 2 + 1 = 5).
找单调队列多重背包题目的时候找到了这道题(然而不是多重背包。。)
顺手写了吧,毕竟USACO的,以后打算把USACO全刷掉。。
方程f[i][j]表示前i个数模数是j的方案数
方程 f[i][j] = f[i-1][j] = f[i-1][(j + num[i])%F]
初始状态f[0][0] = 1
定义这个状态是为了让f[i][F]可以顺利的拿到至少一个点
理论上来说输出f[n][F]跟f[n][0]是等价的,但是由于f[0][0]本该是0但被弄成1,因此会多一个,所以我们要减一
这样就有两种写法了
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#define max(a,b) ((a) > (b) ? (a) : (b))#define min(a,b) ((a) > (b) ? (b) : (a))#define lowbit(a) ((a) & (-(a)))int read(){ int x = 0;char ch = getchar();char c = ch; while(ch > ‘9‘ || ch < ‘0‘)c = ch, ch = getchar(); while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); if(c == ‘-‘)return -x; return x;}const int INF = 0x3f3f3f3f;const int MAXN = 2000 + 10;const int MAXF = 1000;const int MOD = 100000000;int num[MAXN],n,f,dp[MAXN][MAXF];inline void init(){ n = read();f = read(); for(int i = 1;i <= n;i ++) { num[i] = read(); num[i] = num[i] % f; }}inline void DP(){ dp[0][0] = 1; for(int i = 1;i <= n;i ++) { for(int j = 0;j <= f;j ++) { int tmp = j + num[i]; while(tmp >= f)tmp = tmp - f; dp[i][j] = dp[i - 1][j] + dp[i - 1][tmp]; while(dp[i][j] >= MOD)dp[i][j] -= MOD; } }}inline void shuchu(){ printf("%d", dp[n][0] - 1/*dp[n][f]也可以过*/);}int main(){ init(); DP(); shuchu(); return 0;}
P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team