BNU graceful prime decomposition memory-based search

Source: Internet
Author: User

Divide N into the form of prime number addition. The prime number must be smaller than K and the prime number cannot appear continuously. The order of prime numbers is different in addition.

Given N, K, are there several ways to add prime numbers? Questions & examples

I used to create a huge table. Look at what I wrote below. It's amazing !!! Memory-based search, DP thinking...

# Include <iostream> # include <cstdio> # include <cmath> # include <string> # include <cstring> # include <cstdlib> # include <algorithm> using namespace STD; # define M 1004int num [m]; int P [m]; int N, K; void PRI () {memset (Num, 0, sizeof (Num )); int CNT = 1; num [1] = 1; for (INT I = 2; I <= 36; I ++) {If (Num [I] = 0) {P [CNT ++] = I; for (Int J = I * 2; j <60; j + = I) num [J] = 1 ;}}} void solve () {CIN> N> K; int DP [55] [55]; int ans [55]; ans [1] = 0; memset (DP, 0, sizeof (DP); For (int tmp = 2; TMP <= N; TMP ++) {ans [TMP] = 0; If (! Num [TMP] & TMP <= k) {DP [TMP] [TMP] = 1; ans [TMP] ++;} For (INT I = 2; I <TMP; I ++) {if (I> K) break; If (Num [I]) continue; DP [TMP] [I] + = (ANS [TMP-I]-DP [TMP-I] [I]); ans [TMP] + = DP [TMP] [I] ;}} cout <ans [N] <Endl ;}int main () {int T; scanf ("% d", & T); PRI (); For (int cas = 1; CAS <= T; CAS ++) {solve ();} return 0 ;}

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.