当前位置:网站首页>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;
}
边栏推荐
- [concurrent programming] explicit lock and AQS
- Unity editor expansion - the design idea of imgui
- 我们有个共同的名字,XX工
- Using DLV to analyze the high CPU consumption of golang process
- Methods of checking ports according to processes and checking processes according to ports
- LinkedList set
- Concurrent programming (III) detailed explanation of synchronized keyword
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- [RPC] RPC remote procedure call
- 22-05-26 西安 面试题(01)准备
猜你喜欢
Complex character + number pyramid
Six dimensional space (C language)
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Monotonic stack -42 Connect rainwater
状态压缩DP AcWing 91. 最短Hamilton路径
Format - C language project sub file
Query XML documents with XPath
Unity interactive water ripple post-treatment
随机推荐
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Unity editor expansion - the framework and context of unity imgui
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
【Rust 笔记】11-实用特型
[concurrent programming] working mechanism and type of thread pool
DOM render mount patch responsive system
如何应对数仓资源不足导致的核心任务延迟
[rust notes] 09- special types and generics
XPath实现XML文档的查询
C language student management system based on linked list, super detailed
Allocation exception Servlet
Phpstudy 80 port occupied W10 system
Location of package cache downloaded by unity packagemanager
Deeply understand the underlying data structure of MySQL index
Query XML documents with XPath
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
On the difference and connection between find and select in TP5 framework
求组合数 AcWing 886. 求组合数 II
【Rust笔记】06-包和模块