Little Monkey Programming C++ | Collect digital coins

Learn C++ from a young age! Record the questions in the learning process of Xueersi Monkey programming and record every moment. Any infringement will be deleted immediately, thank you for your support!

Attached is a summary post: Little Monkey Programming C++ | Summary-CSDN Blog


[Title description]

Composite number: refers to a number that is divisible by other positive integers in addition to 1 and itself among integers greater than 1.

For example, 4, in addition to being divisible by 1 and 4, 4 can also be divisible by 2.

The little monkey has collected  n (2≤ n ≤25) digital coins, and each digital coin has a face value (the face value can be repeated). Randomly select k (2≤ kn ) digital coins  . There are multiple selection methods. Please add up the face value of the digital coins selected each time, and then solve the following two problems:

  • Question 1: How many different results are there in the cumulative sum?
  • Question 2: How many different composite numbers are there in the accumulated sum.

For example: n =5, k =3, the face values ​​on the 5 digital coins are 2, 1, 4, 5, 3 respectively. Choose any 3 digital coins. There are 10 selection methods. The face value on each selection method is Accumulation: 2 + 1 + 4 = 7, 2 + 1 + 5 = 8, 2 + 1 + 3 = 6, 2 + 4 + 5 = 11, 2 + 4 + 3 = 9, 2 + 5 + 3 = 10, 1 + 4 + 5 = 10, 1 + 4 + 3 = 8, 1 + 5 + 3 = 9, 4 + 5 + 3 = 12.

There are 7 different results in the accumulated sum, which are 7, 8, 6, 11, 9, 10, 12. There are 5 different composite numbers in the accumulated sum, which are 8, 6, 9, 10, 12.

【enter】

The first line contains two positive integers  n and k , indicating the number of digital coins and the number of digital coins to be selected.

The second line contains  n  positive integers  a 1, a 2,⋯, an , which represent the face value of the digital currency. The positive integers are separated by a space.

【Output】

In one line, output two integers, respectively representing the number of different results in the accumulated sum and the number of different composite numbers in the accumulated result. The two integers are separated by a space.

【Input sample】

5 3
2 1 4 5 3

【Output sample】

7 5

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int n, k, a[30];
bool f[300000];

bool check(int n)
{
    if (n<2) return false;
    for (int i=2; i*i<=n; i++) {
        if (n%i==0) return true;
    }
    return false;
}
void dfs(int x, int c, int s)
{
    if (c==k) {
        f[s] = true;
        return;
    }
    if (x>n) return;
    dfs(x+1, c, s);
    dfs(x+1, c+1, s+a[x]);
}
int main()
{
    cin >> n >> k;
    for (int i=1; i<=n; i++) cin >> a[i];

    dfs(1, 0, 0);

    int ans1 = 0, ans2 = 0;
    for (int i=1; i<=300000; i++) {
        if (f[i]) {
            ans1++;
            if (check(i)) ans2++;
        }
    }
    cout << ans1 << " " << ans2 << endl;
    return 0;
}

【operation result】

5 3
2 1 4 5 3
7 5

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133914714