当前位置:网站首页>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;
}
边栏推荐
- Clause 32: moving objects into closures using initialization capture objects
- Vercel uses HTTP caching
- Explain the differences and usage scenarios between created and mounted
- 利用Javeswingjdbc基於mvc設計系統
- Advanced C language - Section 1 - data storage
- Embedded hardware - read schematic
- C disk lossless move file
- Spice story
- PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
- JS to realize the conversion between string and array and an interview question
猜你喜欢

Section 7 - structures

Explain the role of key attribute in V-for

Brick story

Win8.1和Win10各自的优势

2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation

Simple SR: best buddy Gans for highly detailed image super resolution

C language learning log 12.5

QT direction key to move focus

Robot pose description and coordinate transformation

Configuration used by automatic teaching evaluation script
随机推荐
Draw a hammer
Section 5 - Operator details
CMB's written test -- data analysis
Elliptic curve encryption
Robot pose description and coordinate transformation
Use service worker to preferentially request resources - continuous update
QT client development -- driver loading problem of connecting to MySQL database
Advanced C - Section 2 - pointers
Common skills in embedded programming
Clause 31: avoid default capture mode
C language learning log 1.24
Section 7 - structures
String()和toString()方法得区别
C language exercise 1
Chapter 13 abstraction: address space
Little C's Notepad
Section 3 - functions
Several methods of identifying equivalent circuit of circuit drawing
C language learning log 1.22
Kaggle time series tutorial