Poj 3132 sum of different primes DP backpack

Source: Internet
Author: User

Http://poj.org/problem? Id = 3132

Question:

Given N and K, the number of N schemes is represented by exactly k different prime numbers.

Analysis:

N and K are both small. It's a backpack. You just need to select K items to fill up n.

 

 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4  5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k, size;10 void create_prime()11 {12     memset(a, true, sizeof(a));13     size = 0;14     for (int i = 2; i <= 1120; i++){15         if (a[i]) p[size++] = i;16         for (int j = 2*i; j <= 1120; j += i)17             a[j] = false;18     }19 }20 int main()21 {22     create_prime();23     while(scanf("%d%d", &n, &k) && (n + k))24     {25         memset(dp, 0, sizeof(dp));26         dp[0][0] = true;27         ct[0][0] = 1;28         for (int i = 0; i < size; i++){29             int tmp = p[i];30             if (tmp > n) break;31             for (int j = n; j >= tmp; j--)32                 for (int kk = 1; kk <= k; kk++) if (dp[j-tmp][kk-1]){33                     if (!dp[j][kk]){34                         dp[j][kk] = true;35                         ct[j][kk] = ct[j-tmp][kk-1];36                     }37                     else38                         ct[j][kk] += ct[j-tmp][kk-1];39             }40         }41         if (!dp[n][k]) ct[n][k] = 0;42         printf("%d\n", ct[n][k]);43     }44     return 0;45 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.