Dp + backpack linear thinking [USACO09MAR] Cow Frisbee Team S (Los Valley P2946)

[USACO09MAR]Cow Frisbee Team S

Title Description

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.

Dayton farmer due to start after playing Frisbee, John also plans to let the cows enjoy Frisbee. He wants to set up a cow Frisbee

team. His N (1≤N≤2000) cows, each unit has a standard Frisbee index Ri (1≤Ri≤100000). John to elect one or more than one cow came to his Frisbee team. Because of John's lucky number is F (1≤F≤1000), he hoped that all cows Frisbee level Index and is a multiple of lucky numbers.

John to help calculate a total of how many team approach.

Input Format

  • Line 1: Two space-separated integers: N and F

  • Lines 2…N+1: Line i+1 contains a single integer: R_i

Output Format

  • Line 1: A single integer representing the number of teams FJ can choose, modulo 100,000,000.

If you backpack will only cover one-dimensional template, then, this question is not reflected in the backpack thinking;

DP [i] [j] denotes the front i j digital number composed of the number of kinds;

Transfer equation is obvious:

  1. If you do not take the i-th, dp [i] [j] + = dp [i-1] [j]
  2. If you take the i-th, dp [i] [j] + = dp [i-1] [ja [i]]

Is not that what you thought 01 backpack?

But this problem with a bare question there are differences, first modulo problem, first enter numbers a [i] modulo proceeds, the volume becomes j ranges from 0 to f-1;

In general, when 1 to f, then, is generally initialized DP [0] [0], and j from zero, but this is obviously not necessary in advance for all dp [i] [a [i]] is initialized, and can not be initialization dp [0] [0];

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=2010;
const int M=2000100;
const LL mod=1e8;
int a[N];
LL dp[N][N];
int main(){
	int n,f;
	cin>>n>>f;
	for(int i=1;i<=n;i++) cin>>a[i],a[i]%=f; 
	for(int i=1;i<=n;i++) dp[i][a[i]]=1;//必须先赋初值,所以dp[0][0]就不要了 
	for(int i=1;i<=n;i++){
		for(int j=0;j<f;j++){//这个相当于从1开始遍历,所以之前要赋初值 
			(dp[i][j]+=dp[i-1][j]+dp[i-1][(j-a[i]+f)%f])%=mod;
		}
	}
	cout<<dp[n][0]<<endl;
	return 0;
}
Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105278398