总结
\color{red}{\texttt{注:由于luogu.me的原因,若总结有代码错误请}}
\color{red}{\texttt{复制原文至markdown查看}}
总结链 | 他人查看用总结链
比赛
| 序号 | 分数 | 状态 |
|---|---|---|
| A | 100 | AC |
| B | 55 | WA |
| C | 50 | ^ |
| D | 30 | ^ |
| E | NULL | < |
::::::info[总结]
:::::success[T1]
::::success[解题思路]
:::error[非正确方式]
使用
DFS:
二进制枚举:
| ::: |
|---|
| ::::success[正确代码] |
| :::success[二进制枚举] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 1e5+5,M = 5*N,K=5000; |
| ll n; |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> n;//输入 |
| int len = 1<<n;//可能性 |
| for(int i = 0;i<len;i++){//枚举 |
| int ii = i;//备份 |
| for(int j = n-1;j>=0;j--){//判断输出 |
| if((ii>>j)%2==1) cout << "Y";//达到条件 |
| else cout << "N";//没达到 |
| } |
| cout << endl; |
| } |
| return 0; |
| } |
| `````` |
| ::: |
| :::info[DFS] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 1e5+5,M = 5*N,K=5000; |
| ll n; |
| char box[N]; |
| void dfs(int u){//进行DFS |
| if(u==n+1){//到达边界 |
| for(int i = 1;i<=n;i++) cout << box[i]; |
| cout << endl;//换行 |
| return ;//放TLE |
| } |
| box[u]='N';//标记 |
| dfs(u+1); |
| box[u]='Y';//回溯替代 |
| dfs(u+1); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> n; |
| dfs(1);//调用 |
| return 0; |
| } |
| `````` |
| ::: |
| :::: |
| ::::: |
| :::::warning[T2] |
| ::::error[错误代码] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 1e5+5,M = 5*N,K=5000; |
| ll k,n,m,t,ans; |
| void dfs(ll u,ll w){ |
| if(u>k) return ; |
| if(u==k){ |
| ans=max(ans,u); |
| return ; |
| } |
| if(u+n<=k) dfs(u+n,w); |
| if(u+m<=k) dfs(u+m,w); |
| if(w>=1) dfs(u/2,w-1); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> k >> n >> m >> t; |
| dfs(0,t); |
| cout << ans; |
| return 0; |
| } |
| `````` |
| :::: |
| ::::warning[解题思路] |
| :::info[题意分析] |
| $$ |
| 给你T,a,b,k,告诉你可以对变量ans进行任意次一下操作:\ |
| 1.将ans+a\ |
| 2.将ans+b\ |
| 3.将ans变为\frac{ans}{2\times{}k}(注:题目中ans绝对\le1)并将k-1\ |
| 求出ans的最大值 |
| $$ |
| ::: |
| :::success[解题思路] |
| $$ |
| 使用DFS_u\ |
| 如果u>T,那么结束函数\ |
| 如果u=T,那么输出u结束程序\ |
| 否则:\ |
| 将ans设为ans与u的最大值\ |
| 进行嵌套DFS{u+a} |
| 如果k=1,那么嵌套DFS_{\frac{u}{2}} |
| $$ |
| ::: |
| :::: |
| :::success[正确代码] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 1e5+5,M = 5*N,K=5000; |
| ll k,n,m,t,ans;//定义 |
| void dfs(ll u,ll w){//定义dfs |
| if(u>k) return ;//出界 |
| if(u==k){//相等 |
| cout << u; |
| exit(0);//结束 |
| } |
| ans=max(u,ans);//比较 |
| if(u+n<=k) dfs(u+n,w);//调用 |
| if(u+m<=k) dfs(u+m,w); |
| if(w==0&&t==1) dfs(u/2,w+1); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> k >> n >> m >> t; |
| dfs(0,0); |
| cout << ans;//输出 |
| return 0; |
| } |
| `````` |
| ::: |
| ::::: |
| :::::warning[T3] |
| ::::error[错误代码] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 1e5+5,M = 5*N,K=5000; |
| ll n; |
| map<ll,ll> m; |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> n; |
| m[0]=0; |
| for(int i = 1;i<=n+1;i++){ |
| string s=to_string(i); |
| if(s.find("0")==string::npos&&s.find("1")==string::npos&&s.find("2")==string::npos&&s.find("4")==string::npos&&s.find("6")==string::npos&&s.find("8")==string::npos&&s.find("9")==string::npos&&s.find("3")!=string::npos&&s.find("5")!=string::npos&&s.find("7")!=string::npos){ |
| m[i]=m[i-1]+1; |
| //cout << s << ' ' << i << endl; |
| }else m[i]=m[i-1]; |
| } |
| cout << m[n]; |
| return 0; |
| } |
| `````` |
| :::: |
| ::::info[解题思路] |
| :::info[题意分析] |
| $$ |
| 给定n,求在100\sim{}n中有几个仅由357这3个数组成的三位数,且必须有357 |
| $$ |
| ::: |
| :::success[题目思路] |
| $$ |
| 使用dfs_s,每次判断:\ |
| 如果s>n,那么结束函数\ |
| 如果s中仅有357组成,那么ans+1\ |
| 接着调用:\ |
| dfs_{s\times{}10+3}\ |
| dfs_{s\times{}10+5}\ |
| dfs_{s\times{}10+7}\ |
| $$ |
| ::: |
| :::: |
| ::::success[正确代码] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr ll N = 1e5+5,M = 5*N,K=5000; |
| ll ans,n; |
| bool check(ll x){//判断 |
| ll _3=0,_5=0,_7=0; |
| while(x){ |
| if(x%10==3) _3++; |
| if(x%10==5) _5++; |
| if(x%10==7) _7++; |
| x/=10; |
| } |
| return _3>=1&&_5>=1&&_7>=1; |
| } |
| void dfs(ll x){//dfs |
| if(x>n) return ;//出界 |
| if(check(x)) ans++;//达到 |
| dfs(x*10+3);//调用 |
| dfs(x*10+5); |
| dfs(x*10+7); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> n; |
| dfs(0); |
| cout << ans;//输出 |
| return 0; |
| } |
| `````` |
| :::: |
| ::::: |
| :::::warning[T4] |
| ::::error[错误代码] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 1e5+5,M = 5*N,K=5000; |
| ll n,v,a[N],b[N]; |
| bool vis[N]; |
| ll ans=2147483647; |
| void dfs(ll x,ll y){ |
| if(x+y>=v){ |
| ans=min(ans,abs(x-y)); |
| return ; |
| } |
| for(int i = 1;i<=n;i++){ |
| if(vis[i]==0){ |
| vis[i]=1; |
| dfs(x+a[i],y+b[i]); |
| vis[i]=0; |
| } |
| } |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> n >> v; |
| for(ll i = 1;i<=n;i++){ |
| cin >> a[i] >> b[i]; |
| } |
| dfs(0,0); |
| if(ans!=2147483647)cout << ans; |
| else cout << -1; |
| return 0; |
| } |
| `````` |
| :::: |
| ::::info[解题思路] |
| :::info[题意分析] |
| $$ |
| 给定2个长度相等的序列A和B,定义2个变量n和m\ |
| 求:选定A和B中下标相同部分元素分别加在n和n上\ |
| 求出n+m\ge{}k且 |
| $$ |
| ::: |
| :::success[题目思路] |
| $$ |
| 使用dfs进行选和不选,求出最大值 |
| $$ |
| ::: |
| :::: |
| :::success[正确代码] |
| ```cpp |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 1e5+5,M = 5*N,K=5000; |
| int n,v,a[N],b[N]; |
| int ans=2147483647; |
| void dfs(int u,int x,int y){//dfs |
| if(u>n){//达到 |
| if(x+y>=v) ans=min(ans,abs(x-y)); |
| if(ans==0){ |
| cout << 0; |
| exit(0); |
| } |
| return ; |
| } |
| dfs(u+1,x+a[u],y+b[u]);//选和不选 |
| dfs(u+1,x,y); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin.tie(0);cout.tie(0); |
| cin >> n >> v; |
| for(int i = 1;i<=n;i++){//输入 |
| cin >> a[i] >> b[i]; |
| } |
| dfs(1,0,0);//dfs |
| if(ans==2147483647) cout << -1;//输出 |
| else cout << ans; |
| return 0; |
| } |
| `````` |
| ::: |
| ::::: |
| ::::error[T5] |
| :::info[解题思路] |
| $$ |
| 使用dfs每次枚举和判断 |
| $$ |
| ::: |
| :::success[正确代码] |
| ```cpp |
| //我要当空格哥 |
| #include<bits/stdc++.h> |
| #define ll long long |
| #define endl '\n' |
| using namespace std; |
| constexpr int N = 55; |
| int n, sum, max_len, ans; |
| int a[N], vis[N]; |
| bool flag; |
| void dfs(int len, int cnt, int pos) {//判断长度 |
| if (flag) return; |
| if (cnt == sum / ans) {//判断 |
| flag = 1; |
| return; |
| } |
| if (len == ans) {//判断 |
| dfs(0, cnt + 1, n);//调用 |
| return; |
| } |
| for (int i = pos; i >= 1; i--) {//枚举 |
| if (vis[i] |
| if (i < pos && a[i] == a[i + 1] && !vis[i + 1]) continue; |
| vis[i] = 1; |
| dfs(len + a[i], cnt, i - 1); |
| vis[i] = 0; |
| if (flag) return;//返回 |
| if (len == 0) return;//返回 |
| if (len + a[i] == ans) return;//返回 |
| } |
| } |
int main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin >> n;//输入 sum = 0, max_len = 0; for (int i = 1; i <= n; i++) {//录入 cin >> a[i]; sum += a[i]; max_len = max(max_len, a[i]); } sort(a + 1, a + n + 1);//排序 for (ans = max_len;ans<=sum/2;ans++) {//枚举并判断 if (sum % ans != 0) continue; int need = sum / ans; if (need > n) continue; flag = 0; memset(vis, 0, sizeof(vis)); dfs(0, 0, n); if (flag) break;//退出循环 } if (!flag) ans = sum;//达到条件 cout << ans << endl;//输出 return 0; }
:::
::::
::::::