고교군과 카쿠/Tak and Cards(dp)
Score : 300 points
Problem Statement
Tak has N cards. On the i-th (1≤i≤N) card is written an integer xi. He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A. In how many ways can he make his selection?
Constraints
Partial Score
Input
The input is given from Standard Input in the following format:
N A
x1 x2 … xN
Output
Print the number of ways to select cards such that the average of the written integers is exactly A.
Sample Input 1
Copy
4 8
7 9 8 9
Sample Output 1
Copy
5
Sample Input 2
Copy
3 8
6 6 9
Sample Output 2
Copy
0
Sample Input 3
Copy
8 5
3 6 2 8 7 6 5 9
Sample Output 3
Copy
19
Sample Input 4
Copy
33 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Sample Output 4
Copy
8589934591
o# 아이디어 전재
제목: n개의 숫자를 주고 이 n개의 숫자 평균수가 a인 조합수를 구하세요.
사고방식: 처음으로atcoder를 쳤는데 이 문제는 두 가지로 나뉘어 풀고 완전히 풀었다.부분 해제 요구 n<=16, 완전 해제 n<=50.
부분해의 방법은 매우 간단하다. 모든 자집을 매거하여 조건에 부합되는지 확인하면 된다.
가방으로 해야 한다는 것을 완전히 풀고 dp(i, j, k)를 설정하여 i번째 숫자 앞에서 j개와 k의 모든 조합 수를 선택한다.왜냐하면 우리는 평균수의 계산 방법이Σxi/X, a= 설정Σxi/X, a*X=Σxi.우리는 마지막으로 모든 dp(n, X, a*X)만 통계하면 된다.
옮기면 우선 계수는 이전의 결과를 현재의 결과로 옮기고 dp(i, j, k)+=dp(i-1, j, k).다음은 가방입니다. dp(i,j,k)+=dp(i-1,j-1,k-x(i-1)))|{k>=x(i-1)}
코드는 자기가 쳤어요.
#include
#include
typedef long long ll;
using namespace std;
ll dp[55][55][2551];
int main()
{
int x[2550],n,a,i,j,k;
cin>>n>>a;
for(i=0;i>x[i];
dp[0][0][0]=1;
for(i=1;i<=n;i++)
{
for(j=i;j>=0;j--)
{
for(k=a*n+1;k>=0;k--)
{
dp[i][j][k]+=dp[i-1][j][k];
if(k>=x[i-1] && j>=1)
dp[i][j][k]+=dp[i-1][j-1][k-x[i-1]];
}
}
}
ll ans=0;
for(i=1;i<=n;i++)ans+=(dp[n][i][i*a]);
cout<
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
PostgreSQL에서 수집해야 할 vacuum 테이블 정보를 찾는 방법PostgreSQL에서 VACUM을 사용하면 VACUM 테이블을 실행해야 하는 경우에만 삭제된 공간을 사용하지 않은 것으로 표시하여 나중에 이 공간을 다시 사용할 수 있지만 사용 공간을 운영 체제에 즉시 반환할 수 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.