UVa1213 Sum of Different Primes

1120 단어 dpuva
한 개의 숫자가 몇 조의 k개의 다른 소수를 더할 수 있는지 물어봐라.처음으로 체로 시계를 치다.그리고 DP.다른 소수가 필요하기 때문에 한 소수만 할 수 있고 k는 dp를 거꾸로 할 수 있다.i는 소수로서 dp[j-i][k-1]는 반드시 dp[j][k]에 모든 조합을 제공할 수 있다.
#include <iostream>  
#include <stdio.h>  
#include <cmath>  
#include <algorithm>  
#include <iomanip>  
#include <cstdlib>  
#include <string>  
#include <memory.h>  
#include <vector>  
#include <queue>  
#include <stack>  
#include <map>
#include <set>
#include <ctype.h>  
#include<time.h>
#define INF 1000000

using namespace std;

bool tab[1130];
int dp[1130][15];

int main(){
	for(int i=0;i<1130;i++)tab[i]=1;
	tab[0]=tab[1]=0;
	for(int i=2;i<=560;i++){
		for(int j=i*i;j<=1120;j+=i){
			tab[j]=0;
		}
	}
	int n,k;
	
	memset(dp,0,sizeof(dp));
	dp[0][0]=1;
	for(int i=2;i<1120;i++){
		if(!tab[i])continue;
		for(int j=14;j>=1;j--){
			for(int k=1120;k>=i;k--){
				dp[k][j]+=dp[k-i][j-1];
			}
		}
	}
	
	while(cin>>n>>k){
		if(!(n||k))break;
		cout<<dp[n][k]<<endl;
	}
	
	return 0;
}

좋은 웹페이지 즐겨찾기