第十一届蓝桥杯省赛第一场C++A/B组真题

题目:2065. 整除序列

在这里插入图片描述
题解:模拟。

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<double,double>PII;
const int N=1e5+10;
const int mod=1000000009;

int main(){
    LL n;
    scanf("%lld",&n);
    while(n){
        printf("%lld ",n);
        n/=2;
    }
    return 0;
}

题目:2066. 解码

在这里插入图片描述
题解:模拟+字符串处理

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<double,double>PII;
const int N=1e5+10;
const int mod=1000000009;
char a[110];
int main(){
    scanf("%s",a);
    int lena=strlen(a);
    for(int i=0;i<lena;){
        if(i+1<lena&&a[i+1]>='2'&&a[i+1]<='9'){
            for(int j=0;j<a[i+1]-'0';j++){
                printf("%c",a[i]);
            }
            i+=2;
        }else{
            printf("%c",a[i]);
            i++;
        }

    }
    return 0;
}

题目:2067. 走方格

在这里插入图片描述
题解:dp。

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<double,double>PII;
const int N=1e5+10;
const int mod=1000000009;
LL f[35][35];
int main(){
    f[1][1]=1;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(i%2==0&&j%2==0) continue;
            f[i][j]+=f[i-1][j]+f[i][j-1];
        }
    }
    printf("%lld",f[n][m]);
    return 0;
}

题目:2068. 整数拼接

在这里插入图片描述
题解:假设选的是a[i]和a[j],且满足(a[i]*len(a[j])+a[j]) %k ==0,等价于 a[i]*len(a[j])%k == -1 * a[j]%k。
这时我们可以预先处理出左边的等式,考虑到每个a[i]<=10^9,也就是说每个数最多乘10 ^ 10。
f[i][j]是当(a[k]*10^i) %k ==j。处理左边等式的复杂度为o(n * 10),即最大10^6。然后再遍历右边的式子即可,但要注意排除(a[i]+a[i]*len)%k ==0的情况

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<double,double>PII;
const int N=1e5+10;
const int mod=1000000009;
int a[N];
int f[15][N];
int main(){
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++){
        int t=a[i];
        for(int j=1;j<=10;j++){
            t=(1ll*t*10)%k;
            f[j][t]++;
        }
    }
    LL ans=0;
    for(int i=1;i<=n;i++){
        int len=to_string(a[i]).size();
        ans+=f[len][(-1*a[i]%k+k)%k];
        int t=a[i]%k;
        while(len--){
            t=(t*10)%k;
        }
        if(t==(-1*a[i]%k+k)%k) ans--;//这里排除是(a[i]+a[i]*len)%k==0
    }
    printf("%lld",ans);
    return 0;
}

题目:2069. 网络分析

在这里插入图片描述
在这里插入图片描述

题解:并查集+树上差分
在这里插入图片描述
上图来自acwing某大佬链接

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<double,double>PII;
const int N=1e5+10;
const int mod=1000000009;
int n,m;
int p[20010];
int v[20010];
int root=10001;
vector<int > g[20010];
int get(int u){
    if(p[u]!=u) p[u]=get(p[u]);
    return p[u];
}
void dfs(int u,int fa){
    v[u]+=v[fa];
    for(int i=0;i<g[u].size();i++){
        dfs(g[u][i],u);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=20005;i++)
        p[i]=i;
    int x,y,z;
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&x,&y,&z);
        if(x==1){
            int yy=get(y);
            int zz=get(z);
            if(yy!=zz){
                p[yy]=root;
                p[zz]=root;
                g[root].push_back(yy);
                g[root].push_back(zz);
                root++;

            }
        }else{
            int yy=get(y);
            v[yy]+=z;
        }
    }
    for(int i=10001;i<root;i++){
        if(p[i]==i) dfs(i,0);
    }
    for(int i=1;i<=n;i++){
        printf("%d ",v[i]);
    }
    return 0;
}

题目:2875. 超级胶水

在这里插入图片描述
题解:经过分析会发现,消耗的胶水量其实就是n个物品中任意两个的乘积之和。

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<double,double>PII;
const int N=1e5+10;
const int mod=1000000009;
int n;
int w[N];
int sum[N];
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&w[i]);
        sum[i]=sum[i-1]+w[i];
    }
    LL ans=0;
    for(int i=1;i<=n;i++){
        ans+=1ll*w[i]*sum[i-1];
    }
    printf("%lld",ans);
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值