2024 蓝桥杯模拟赛3(div1+div2)
A
思路:暴力枚举
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
int n,k;
cin>>n>>k;
vector<int>a(n);
for(int i=0;i<n;++i)cin>>a[i];
int ans=0;
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j)
if(a[i]*a[j]<=k)ans++;
}
cout<<ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
B
思路:二分或直接算
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
int a,b,n;
cin>>a>>b>>n;
int ans,c=5*a+2*b;
ans=n/c*7;
n%=c;
if(n<=a*5)ans+=(n+a-1)/a;
if(n>a*5)ans+=5,n-=5*a,ans+=(n+b-1)/b;
cout<<ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
C
思路:由编号求位置,由位置求距离
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
int w,m,n;
cin>>w>>m>>n;
PII a,b;
auto P=[w](int x){
PII y;
y.first=(x+w-1)/w;
if(y.first%2){
y.second=x%w;
if(y.second==0)y.second=w;
}else{
x%=w;
if(x==0)x=w;
y.second=w-x+1;
}
return y;
};
a=P(m),b=P(n);
int ans=abs(a.first-b.first)+abs(a.second-b.second);
cout<< ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
D
思路:60年一个轮回,为了方便计算,把年数替换成不小于2020的最小的数,之后枚举即可
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
string a[10]={"jia","yi","bing","ding","wu","ji","geng","xin","ren","gui"};
string b[12]={"zi","chou","yin","mao","chen","si","wu","wei","shen","you","xu","hai"};
void solve() {
int n;
cin>>n;
if(n<2020)
while(n<2020)n+=60;
if(n>=2080)
while(n>=2080)n-=60;
int x=6,y=0;
for(int i=2021;i<=n;++i){
x++,y++;
x%=10,y%=12;
}
cout<<a[x]<<b[y];
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
E
思路:枚举ijk,再由ijkn判断第4个数是否可行
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
int n;
cin>>n;
for(int i=0;i*i<=n;++i){
for(int j=0;i*i+j*j<=n;++j){
for(int z=0;i*i+j*j+z*z<=n;++z){
int x=n-i*i-j*j-z*z;
if(x== (int)::sqrt(x)* (int)::sqrt(x)){
cout<<i<<' '<<j<<' '<<z<<' '<<::sqrt(x);
return ;
}
}
}
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
F
思路:保证了要连续且是前缀,可以贪心的找最靠前的相同的字符
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
string a,b;
cin>>a>>b;
int ans=0;
int x=0;
for(int i=0;i<b.size();++i){
while(x<a.size()&&a[x]!=b[i])x++;
if(x>=a.size()){
ans=i;
break;
}
x++;
if(x==a.size()){
ans=i+1;
break;
}
if(i==b.size()-1){
ans=b.size();
}
}
cout<<ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
G
思路:已经给了编号,直接并查集就可以
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
vector<int>fa;
int find(int x){
if(x!=fa[x])fa[x]=find(fa[x]);
return fa[x];
}
void solve() {
int n,m,k;
cin>>n>>m>>k;
fa=vector<int>(n*m+1);
for(int i=1;i<=n*m;++i)fa[i]=i;
for(int i=0;i<k;++i){
int x,y;
cin>>x>>y;
x=find(x),y=find(y);
if(x!=y)fa[x]=y;
}
set<int>se;
for(int i=1;i<=n*m;++i){
int x=find(i);
se.insert(x);
}
cout<<se.size();
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
H
思路:分类讨论。一编号的人数大于2的话,多出的人都要改,一编号人数为1的话,所有相同情况的人可以只改一半
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
int n;
cin>>n;
map<int,int>mp;
for(int i=0;i<n;++i){
int x;
cin>>x;
mp[x]++;
}
int a=0,b=0;
for(auto[x,y]:mp){
if(y==1)a++;
else if(y>2)b+=y-2;
}
if(a>=b){
cout<<(a+b)/2;
}else{
cout<<b;
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
I
思路:若一个点为关键点,说明x到y的每条路径都经过它,统计下x到y的路径数,已经经过每个点的次数,当一个点的次数等于总路径数说明其为关键点
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
vector<vector<int>>ve;
vector<int>num,vis;
int n,m,all;
int st,ed;
void dfs(int u){
if(u==ed){
all++;
for(int i=1;i<=n;++i){
if(vis[i])num[i]++;
}
return ;
}
for(auto v:ve[u]){
if(!vis[v]){
vis[v]=1;
dfs(v);
vis[v]=0;
}
}
}
void solve() {
cin>>n>>m;
vis=num=vector<int>(n+1);
ve=vector<vector<int>>(n+1);
for(int i=0;i<m;++i){
int x,y;
cin>>x>>y;
ve[x].push_back(y),ve[y].push_back(x);
}
cin>>st>>ed;
// num[st]=1;
vis[st]=1;
dfs(st);
int ans=0;
for(int i=1;i<=n;++i){
if(i!=st&&i!=ed&&num[i]==all)ans++;
}
if(all)cout<<ans;
else cout<<-1;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
J
思路:可以按正方形的大小进行分类,对于给定边长i的正方形a,可以发现四点均在a的边上的正方形恰好有i个,在长度为n的正方形里有(n-i+1)*(n-i+1)个这样的a,即为i*(n-i+1)*(n-i+1),统计所有的i的i*(n-i+1)*(n-i+1)
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
int n,ans=0;
cin>>n;
n--;
for(int i=1;i<=n;++i){
ans=(ans+i*(n-i+1)*(n-i+1))%mod;
}
cout<<ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
K
思路:区间dp,对于区间[i,j],若s[i]和s[j]不相等直接由大小关系判断区间[i,j]是否可以,若相等,由[i+1,j-1]判断。用区间dp即可
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
void solve() {
string s;
cin>>s;
int n=s.size();
s.insert(s.begin(),' ');
vector<vector<int>>f(n+1,vector<int>(n+1));
for(int i=2;i<=n;++i){
for(int j=1;j+i-1<=n;++j){
if(s[j]>s[j+i-1])f[j][j+i-1]=1;
else if(i!=2&&s[j]==s[j+i-1])f[j][j+i-1]=f[j+1][j+i-2];
}
}
int ans=0;
for(int i=1;i<n;++i)
for(int j=i+1;j<=n;++j)
ans+=f[i][j];
cout<<ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}
L
思路:可以看到n最大为6,且每一列的状态只与前一列和前前一列有关,每一列的状态可用二进制表示,用四维数组分别记录当前列数,前前列状态,当前列状态,已用棋子数,然后更新状态。要判断是否冲突可以对二进制进行移位
查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define int __int128
#define double long double
typedef pair<int,int>PII;
typedef pair<string,int>PSI;
typedef pair<string,string>PSS;
const int N=1e6+5,INF=0x3f3f3f3f,mod=1e9+7,Mod=998244353;
const int MAXN=1e8+5;
const double eps=1e-9;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
int f[101][64][64][21];
void solve() {
int n,m,kk;
cin>>n>>m>>kk;
vector<int>cnt(1<<n);
auto num=[](int x){
int s=0;
while(x){
if(x&1)s++;
x>>=1;
}
return s;
};
for(int i=1;i<1<<n;++i)cnt[i]=num(i);
for(int i=0;i<1<<n;++i){
for(int j=0;j<1<<n;++j)
if(cnt[i]+cnt[j]<=kk)f[2][i][j][cnt[i]+cnt[j]]=1;
}
for(int i=2;i<=m;++i){
for(int j=0;j<1<<n;++j){
for(int c=cnt[j];c<=kk;++c){
for(int x=0;x<1<<n;++x){
for(int y=0;y<1<<n;++y){
if(cnt[j]+cnt[x]+cnt[y]>kk)continue;
if(x&(y<<2)||x&(y>>2)||x&(j<<1)||x&(j>>1)||y&(j<<2)||y&(j>>2))continue;
f[i][y][j][c]=(f[i][y][j][c]+f[i-1][x][y][c-cnt[j]])%mod;
}
}
}
}
}
int ans=0;
for(int i=0;i<1<<n;++i){
for(int j=0;j<1<<n;++j){
ans=(ans+f[m][i][j][kk])%mod;
}
}
cout<<ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
// init();
while(t--){
solve();
}
return 0;
}