6.19总结
6.19总结
课堂总结
{\color{Orange}{P16713}} 相思
题目传送门(比赛)
考察知识点
- 数论
错误原因
-
### 错误思路 用双重循环判断所有可能中是否有可能和为质数,有可能则`Edisnrub`胜,否则`Burnside`胜 ### 错误代码${\color{Red}{(WA)}}$: :::error[错误代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int l1,r1,l2,r2; bool st[200005]={1,1}; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); //质数判断(预处理) for(int i=2;i<=200000;i++){ if(st[i])continue; for(int j=i+i;j<=200000;j+=i){ st[j]=1; } } cin>>l1>>r1>>l2>>r2; for(int i=l1;i<=r1;i++){ for(int j=l2;j<=r2;j++){ if(st[i+j])continue; cout<<"Edisnrub\n"; return 0; } } cout<<"Burnside\n"; return 0; } ``` ::: ### 正确思路 用双重循环判断所有可能中是否存在`Burnside`选的数让`Edisnrub`无论如何都无法让和为质数的情况,有可能则`Burnside`胜,否则`Edisnrub`胜。因为连续非质数个数$≤100$,所以暴力枚举不会超时 ### 正确代码${\color{Green}{(AC)}}$: :::success[正确代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int l1,r1,l2,r2; bool isPrime(int x){ for(int i=2;i*i<=x;i++){ if(x%i==0)return 0; } return 1; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>l1>>r1>>l2>>r2; for(int i=l1;i<=r1;i++){ bool f=0;//存在Burnside选的数让Edisnrub无论如何都无法让和为质数的情况为0,否则为1 for(int j=l2;j<=r2;j++){ if(isPrime(i+j)){ f=1; break; } } if(!f){ cout<<"Burnside\n"; exit(0); } } cout<<"Edisnrub\n"; return 0; } ``` ::: ### ${\color{Gray}{T772411}}$ 旋转之乘 [题目传送门(比赛)](https://www.luogu.com.cn/problem/T772411?contestId=333540) ### 考察知识点 - 广度优先搜索(
BFS )错误原因
-
### 错误思路 用$BFS$搜索到达终点的最短路径 ### 错误代码${\color{Orange}{(32分)}}$: :::error[错误代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a,n; bool st[1000005]; struct node{ int x,step; }; queue<node>q; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>a>>n; //增加起点的影响 st[1]=1; q.push({1,0}); //搜索 while(q.size()){ node t=q.front(); q.pop(); //到达终点 if(t.x==n){ cout<<t.step<<endl; return 0; } //操作1 int nx=t.x*a; if(nx<=1000000&&!st[nx]){ st[nx]=1; q.push({nx,t.step+1}); } //操作2 nx=t.x<10?t.x:n/10+n%10*(int)pow(10,(int)log10(n/10)+1); if(!st[nx]){ st[nx]=1; q.push({nx,t.step+1}); } } //无法到达 cout<<-1<<endl; return 0; } ``` ::: ### 正确思路 用$BFS$搜索到达终点的最短路径即可。注意把末尾的数字移动到字符串的开头时只有当$t≥10$且$t$不能被$10$整除时才能进行操作 ### 正确代码${\color{Green}{(100分)}}$: :::success[正确代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a,n,dis[1000005]; bool st[1000005]; queue<int>q; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>a>>n; //增加起点的影响 st[1]=1; dis[1]=0; q.push(1); //搜索 while(q.size()){ int t=q.front(); q.pop(); //操作1 int nx=t*a; if(nx<=1000000&&!st[nx]){ st[nx]=1; dis[nx]=dis[t]+1; q.push(nx); } //操作2 int x=t,len=0; while(x)len++,x/=10;//计算log10(n/10) nx=t%10*pow(10,len-1)+t/10; if(nx>10&&t%10&&!st[nx]){ st[nx]=1; dis[nx]=dis[t]+1; q.push(nx); } } if(st[n])cout<<dis[n]<<endl;//有解 else cout<<-1<<endl;//无解 return 0; } ``` ::: ### ${\color{Gray}{T772460}}$ 集合覆盖 [题目传送门(比赛)](https://www.luogu.com.cn/problem/T772460?contestId=333540) ### 考察知识点 - 深度优先搜索(
DFS )错误原因
-
### 错误思路 (与正确思路一致,见下文“正确思路”) ### 错误代码${\color{YellowGreen}{(91分)}}$: :::error[错误代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int n,m,s[15][15],ans; map<int,int>ma; void dfs(int x){ if(x>n){ if(ma.size()==n)ans++; return; } //选该集合 for(int i=1;i<=s[x][0];i++){ ma[s[x][i]]++; } dfs(x+1); //不选该集合 for(int i=1;i<=s[x][0];i++){ ma[s[x][i]]--; if(ma[s[x][i]]==0)ma.erase(s[x][i]); } dfs(x+1); } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=m;i++){ cin>>s[i][0]; for(int j=1;j<=s[i][0];j++){ cin>>s[i][j]; ma[s[i][j]]++; } } if(ma.size()<n){ cout<<0<<endl; return 0; } ma.clear(); dfs(1); cout<<ans<<endl; return 0; } ``` ::: ### 正确思路 用$DFS$搜索出所有可能,再统计答案即可 ### 正确代码${\color{Green}{(100分)}}$: :::success[正确代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int n,m,s[15][15],ans; map<int,int>ma; void dfs(int x){ if(x>m){ if(ma.size()==n)ans++; return; } //选该集合 for(int i=1;i<=s[x][0];i++){ ma[s[x][i]]++; } dfs(x+1); //不选该集合 for(int i=1;i<=s[x][0];i++){ ma[s[x][i]]--; if(ma[s[x][i]]==0)ma.erase(s[x][i]); } dfs(x+1); } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m; for(int i=1;i<=m;i++){ cin>>s[i][0]; for(int j=1;j<=s[i][0];j++){ cin>>s[i][j]; ma[s[i][j]]++; } } if(ma.size()<n){ cout<<0<<endl; return 0; } ma.clear(); dfs(1); cout<<ans<<endl; return 0; } ``` :::