0129-407-第五次课-dfs2
T723530 传球游戏
题意
有
思路
用
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, m;
int a[N];
int sum;
void dfs(int s, int cnt){
if(s == m + 1){
if(cnt == 1){
sum++;
}
return ;
}
if(cnt + 1 == n + 1){
a[s] = 1;
dfs(s + 1, 1);
}else{
a[s] = cnt + 1;
dfs(s + 1, cnt + 1);
}
if(cnt - 1 == 0){
a[s] = n;
dfs(s + 1, n);
}else{
a[s] = cnt - 1;
dfs(s + 1, cnt - 1);
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m;
dfs(1, 1);
cout << sum;
return 0;
}
赛时分析
用时
思路
答案必须包含
void dfs(int s){
if(s > n){
return ;
}
dfs(选3);
dfs(选5);
dfs(选7);
}
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
int n;
int res;
void dfs(int s, int c1, int c2, int c3){
if(s > n){
return ;
}
if(c1 > 0 && c2 > 0 && c3 > 0){
res++;
}
dfs(s * 10 + 3, c1 + 1, c2, c3);
dfs(s * 10 + 5, c1, c2 + 1, c3);
dfs(s * 10 + 7, c1, c2, c3 + 1);
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
dfs(0, 0, 0, 0);
cout << res;
return 0;
}
赛时分析
用时
思路
提前处理出每个素数,用
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 205;
int n;
int a[N], v[N], h;
void f(int n){
v[0] = 1;
v[1] = 1;
for(int i = 2;i <= n;i++){
if(v[i] == 0){
h++;
a[h] = i;
for(int j = 2 * i;j <= n;j += i){
v[j] = 1;
}
}
}
return ;
}
int sum;
int maxn = 0;
void dfs(int s, int cnt, int f){
if(cnt > n){
return ;
}
if(cnt == n){
maxn = max(maxn, f);
return ;
}
if(s > h){
return ;
}
dfs(s + 1, cnt + a[s], f + 1);
dfs(s + 1, cnt, f);
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
f(200);
cin >> n;
dfs(1, 0, 0);
cout << maxn;
return 0;
}
赛时分析
用时
T726607 Air Cownditioning II B
题意
有
思路
首先用一个数组
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 25;
struct P{
int a, b, m, p;
};
P q[N];
int wd[105];
int n, m;
int mk[105];
int minn = INT_MAX;
void dfs(int s, int sum){
if(s > m){
for(int i = 1;i <= 100;i++){
if(mk[i] < wd[i]){
return ;
}
}
minn = min(minn, sum);
return ;
}
for(int i = q[s].a;i <= q[s].b;i++){
mk[i] += q[s].p;
}
dfs(s + 1, sum + q[s].m);
for(int i = q[s].a;i <= q[s].b;i++){
mk[i] -= q[s].p;
}
dfs(s + 1, sum);
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m;
for(int i = 1;i <= n;i++){
int s, t, c;
cin >> s>> t>> c;
for(int j = s;j <= t;j++){
wd[j] = c;
}
}
for(int i = 1;i <= m;i++){
cin >> q[i].a>> q[i].b>> q[i].p>> q[i].m;
}
dfs(1, 0);
cout << minn;
return 0;
}
赛时分析
得分
0分
错因
没写