当前位置:网站首页>Noip 2002 popularity group selection number
Noip 2002 popularity group selection number
2022-07-03 08:53:00 【Xizhou theory】
subject
Topic link
Brief description of the topic :
Description
It is known that n It's an integer x1,x2,…,xn, And an integer k(k<n). from n Any number of integers k Add the whole numbers , We can get a series of and respectively . For example, when n=4,k=3,4 The integers are 3,7,12,19 when , All the combinations and their sum can be obtained as :
3+7+12=22 3+7+19=29 7+12+19=38 3+12+19=34.
Now? , Ask you to work out how many kinds of sum are prime numbers .
For example, the above example , There is only one sum of prime numbers :3+7+19=29).
Input
Keyboard entry , The format is :
n , k (1<=n<=20,k<n)
x1,x2,…,xn (1<=xi<=5000000)
Output
Screen output , The format is :
An integer ( The number of species that satisfy the condition ).
Sample Input
4 3
3 7 12 19
Sample Output
1
Ideas
- This type of topic is combinatorial enumeration , The corresponding traversal method is DFS + prune
- DFS stay n Selected from the number k Number , use chosen preservation , The number of choices exceeds k(chosen.size() > k) Or even if the number has been selected now + All the remaining numbers cannot be equal to k Of DFS Pruning (chosen.size() + (n - u) < k)
- use check_valid Function to judge the selected k Whether the number meets the meaning of the question , take chosen Add all the numbers in to get sum, Judge sum Is it a prime , If it's a prime number , The number of types that meet the meaning of the topic cnt ++;
Code
#include <iostream>
#include <vector>
using namespace std;
const int N = 30;
long long q[N]; // Input array
int n, k, cnt; // cnt The number of categories required for the topic
vector<int> chosen; // Store the number selected each time
bool is_prime(int n) // Prime judgment template
{
if (n < 2) return false;
for (int i = 2; i <= n / i; i ++)
if (n % i == 0) return false;
return true;
}
bool check_valid(vector<int> a) // Judge whether the sum of the selected numbers is a prime number
{
long long sum = 0;
for (int i = 0; i < a.size(); i ++ ) sum += a[i];
if (is_prime(sum))
return true;
return false;
}
void dfs(int u)
{
// prune , If you have selected more than m Number , Or even if you reselect all the remaining numbers, it's not enough m individual , You can know in advance that the current problem has no solution
if (chosen.size() > k || chosen.size() + (n - u) < k) return;
if (u == n) // Boundary situation
{
if (check_valid(chosen)) cnt ++; // use check_valid Function to determine whether the selected number meets the requirements
return;
}
dfs(u + 1); // No second choice u Number
chosen.push_back(q[u]); // Choose the first u Number
dfs(u + 1);
chosen.pop_back(); // to flash back , Restore scene
}
int main()
{
cin >> n >> k;
for (int i = 0; i < n; i ++ ) cin >> q[i];
dfs(0);
cout << cnt << endl;
return 0;
}
边栏推荐
- 使用dlv分析golang进程cpu占用高问题
- 【Rust笔记】05-错误处理
- Unity interactive water ripple post-treatment
- On the setting of global variable position in C language
- Sending and receiving of request parameters
- Final review of Database Principles
- LeetCode 241. 为运算表达式设计优先级
- Unity Editor Extension - drag and drop
- [concurrent programming] concurrent security
- Query XML documents with XPath
猜你喜欢

TP5 multi condition sorting

PHP uses foreach to get a value in a two-dimensional associative array (with instances)

高斯消元 AcWing 883. 高斯消元解线性方程组

Es8 async and await learning notes

Deeply understand the underlying data structure of MySQL index
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

Servlet的生命周期

XPath实现XML文档的查询
![[concurrent programming] consistency hash](/img/5e/3d0a52caa8ca489a6e6267274bbb39.jpg)
[concurrent programming] consistency hash

Deep parsing (picture and text) JVM garbage collector (II)
随机推荐
file_ put_ contents
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
Convert video to GIF
[rust notes] 05 error handling
[rust notes] 08 enumeration and mode
Six dimensional space (C language)
php public private protected
状态压缩DP AcWing 91. 最短Hamilton路径
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
如何应对数仓资源不足导致的核心任务延迟
Facial expression recognition based on pytorch convolution -- graduation project
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
[concurrent programming] concurrent tool class of thread
Markdown learning
Find the combination number acwing 886 Find the combination number II
Monotonic stack -503 Next bigger Element II
ES6 promise learning notes
Alibaba canal actual combat
【Rust笔记】05-错误处理
Really explain the five data structures of redis