当前位置:网站首页>Luogu p1036 number selection
Luogu p1036 number selection
2022-06-13 05:00:00 【Clown clown clown】
Topic link : Selection
Knowledge point : Combinatorial enumeration
Write here Recursive composite enumeration
The recursive search tree is as follows :
Recursive composite enumeration template
void dfs(int u,int start)
{
if(u == m)
{
for(int i = 0; i < m; i++) printf("%d ",path[i]);
printf("\n");
return;
}
for(int i = start; i <= n; i++)
{
path[u] = i;
dfs(u+1,i+1);// Can not write start+1, Draw a recursive search tree .
}
}
So let's talk about that dfs Inside pass start+1 and i+1 The difference between :
start Represents the first number that can be selected at present .
i Representative means the number that can be selected ( No requirement is the first )
Take an example to know why i+1 了 .
notes :
1. The combinations thus assembled are sorted in dictionary order , Because first fill in the smallest number in the dictionary
2. Recursive composite enumerations do not require vis Array
3. No pruning here
AC Code :
#include <iostream>
using namespace std;
const int N = 30;
int n, k;
int ans[N];
int obj[N];
int cnt;
bool check(int x)
{
for (int i = 2; i <= x / i; i++)
if (x % i == 0)
return false;
return true;
}
void dfs(int u,int start)
{
if (u == k)
{
int res = 0;
for (int i = 0; i < k; i++) res += ans[i];
if (check(res)) cnt++;
return;
}
for (int i = start; i < n; i++)
{
ans[u] = obj[i];
dfs(u + 1,i+1);
}
}
int main()
{
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> obj[i];
dfs(0,0);
cout << cnt;
}
边栏推荐
- Section 5 - Operator details
- Clause 28: understanding reference folding
- Clause 48: understand template metaprogramming
- Optocoupler working principle function electric parameter application circuit
- Clause 34: lambda is preferred over std:: bind
- On switch() case statement in C language
- C language learning log 1.16
- Introduction to QT XML
- Section 6 - pointers
- C language learning log 10.5
猜你喜欢
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
C # get all callable methods of WebService interface [webmethod]
The games that you've tasted
Createanonymousthreadx passes parameters to anonymous threads
Shell built-in string substitution
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
[JS solution] leedcode 200 Number of islands
Sampo Lock
Converting MySQL data to PostgreSQL with Navicat
The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
随机推荐
自动评教脚本使用的配置
OJ problem solution summary
How to understand JS expressions and JS statements
JS to realize the conversion between string and array and an interview question
Chapter 15 mechanism: Address Translation
Shell variable learning notes
Section 6 - pointers
Draw a hammer
[try to hack] upload labs (temporarily write to 12)
[LeetCode]-二分查找
C language learning log 1.17
CMB written test graphical reasoning
Clause 32: moving objects into closures using initialization capture objects
QT direction key to move focus
Elliptic curve encryption
C language exercise 1
UNO
Section 8 - Practical commissioning techniques
SQL notes
Force buckle 25 A group of K flipped linked lists