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.
* 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
INPUT DETAILS:
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.
OUTPUT DETAILS:
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).
老唐最近迷上了飞盘,约翰想和他一起玩,于是打算从他家的N头奶牛中选出一支队伍。
每只奶牛的能力为整数,第i头奶牛的能力为R i 。飞盘队的队员数量不能少于 1、大于N。一
支队伍的总能力就是所有队员能力的总和。
约翰比较迷信,他的幸运数字是F,所以他要求队伍的总能力必须是F的倍数。请帮他
算一下,符合这个要求的队伍组合有多少?由于这个数字很大,只要输出答案除以10^8108 的余
数就可以了。
输入格式
?第一行:两个用空格分开的整数:N和F,1 ≤ N ≤ 2000,1 ≤ F ≤ 1000
?第二行到N + 1行:第i + 1行有一个整数R i ,表示第i头奶牛的能力,1 ≤ R i ≤ 10 5
输出格式
? 第一行:单个整数,表示方案数除以10^8108 的余数
一道非常有含量的DP(递推)
这道题的状态设置把我难倒了。
经过长期调试+题解帮助,我将dp[i] [j]设置成选前i头奶牛,%f余数为j的方案数,注意,是%f之后!!
初始条件为1.
所以我们初始化的时候需要把r[i]模一个f。
然后状态转移就是如代码所示了。
来吧看答案:
#include<cstdio>
#include<algorithm>
using namespace std;
const int mod=1e8;
int n,f,r[2001];
int dp[2001][1001];
int main()
{
scanf("%d%d",&n,&f);
for(int i=1;i<=n;i++)
scanf("%d",&r[i]);
for(int i=1;i<=n;i++)
dp[i][r[i]%f]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<f;j++)
dp[i][j]=(dp[i][j]+dp[i-1][j]+dp[i-1][((j-r[i])%f+f)%f])%mod;
printf("%d",dp[n][0]);
return 0;
}
原文:https://www.cnblogs.com/fusiwei/p/11293681.html