牛客小白月赛62

A题是一道模拟题按照题目的意思模拟就行,B题是一道思维题,C题是一道数论的题,D题是一道思维题,E题是一道树上差分的问题,E题关于树上差分看了好多博客其实还是不太理解。

A题

#include<iostream>
using namespace std;

const int N = 1010;
int h[N];
int a, b, k, n, m;

int main()
{
    int T;cin >> T;
    while(T --)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; ++ i)    scanf("%d", &h[i]);
        scanf("%d%d%d%d", &a, &k, &b, &m);
        for(int i = 1; i <= m - 1; i ++)
        {
            for(int j = 0; j < n; ++ j)
            {
                h[j] += a;
                if(h[j] > k)    h[j] = b;
                
            }
        }
        for(int i = 0; i < n; ++ i)    printf("%d ", h[i]);
        cout << endl;
    }
    return 0;
}

B题

#include<iostream>
using namespace std;

int main()
{
    int T;cin >> T;
    while(T --)
    {
        long long l, r;cin >> l >> r;
        long long sum = (l + r) * (r - l + 1) / 2;
        long long m;cin >> m;
        while(m --)
        {
            long long  x;cin >> x;
            if(sum % x)    cout << 1 << endl;
            else cout << 0 << endl;
        }
    }
    return 0;
}

C题

c题是对a中的所有数分解质因数,然后枚举b中的数,看b中数的质因数是否在a的质因子里面出现过。

#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 1e5 + 110;

int a[N], b[N];
int n;

int main()
{
    unordered_map<int, int>mp;
    cin >> n;
    for(int i = 0; i < n; ++ i)    scanf("%d", &a[i]);
    for(int i = 0; i < n; ++ i)    scanf("%d", &b[i]);
    for(int i = 0; i < n; ++ i)
    {
        int k = a[i];
        for(int j = 2; j <= k / j; ++ j)
        {   
            if(k % j == 0)
            {
                while(k % j == 0)    k /= j;
                mp[j] ++;
            }
        }
        if(k > 1)    mp[k] ++;
    }
    for(int i = 0; i < n; ++ i)
    {
        int k = b[i];
        for(int j = 2; j <= k / j; ++ j)
        {   
            if(k % j == 0)
            {
                while(k % j == 0)    k /= j;
                if(mp[j])   
                {
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
        if(k > 1 && mp[k])
        {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

D题

#include <iostream>

using namespace std;
typedef long long ll;

void solve()
{
    ll n;
    int k, m;
    cin >> n >> k >> m;
    while(m --)
    {
        ll q;
        ll ans = 0;
        scanf("%lld", &q);
        if(k == 1)    cout << n - q << endl;
        else
        {
            ll l = q, r = q;
            while(l <= n)
            {
                ans += r - l + 1;
                r = min(k * r + k, n - 1);
                l = k * l + 1;
            }
            cout << ans << endl;
        }
    }
}

int main()
{
    int T;cin >> T;
    while(T --)    solve();
    return 0;
}

E题

#include <bits/stdc++.h>
using namespace std;
const int N = 10000009, M = 500009;

int n;
int rt[N][2];
int cnt[M];

int dfs(int x, int sum) {
	if(x > n) return 0;
	sum += rt[x][0];
	int ans = sum, add = rt[x][1];
	add += dfs(2 * x, sum);
	add += dfs(2 * x + 1, sum);
	cnt[ans + add] ++;
	return add;
}
int main() {
	int m;  cin >> n >> m;
	int op, x;
	for(int i = 0; i < m; i ++) {
		cin >> op >> x;
		if(op == 1) {
			rt[x][0] ++;
		} else if(op == 2) {
			rt[x][0] --;
			rt[1][0] ++;
		} else if(op == 3) {
			rt[x][1] ++;
		} else {
			rt[x][1] --;
			rt[1][0] ++;
		}
	}
	dfs(1, 0);
	for(int i = 0; i <= m; i ++) cout << cnt[i] << ' ';
	return 0;
}

这种做法好像不是差分的做法了

#include <iostream>
using namespace std;

const int N = 1e8 + 10;
int n, m;
int cf[N], ans[N], tag[N];

void dfs(int u, int f)
{
    if(u > n)    return;
    int w = 0;
    w += cf[u];
    w += tag[u];
    ans[w] ++;
    cf[u * 2] += cf[u];
    cf[u * 2 + 1] += cf[u];
    dfs(u * 2, u);
    dfs(u * 2 + 1, u);
}

int main()
{
    cin >> n >> m;
    for(int i = 0; i < m; ++ i)
    {
        int op, x; scanf("%d%d", &op, &x);
        if(op == 1)    cf[x] ++;
        else if(op == 2)    
        {
            cf[x] --;
            cf[1] ++;
        }
        else if(op == 3)
        {
            while(x)
            {
                tag[x] ++;
                x /= 2;
            }
        }
        else if(op == 4)
        {
            while(x)
            {
                tag[x] --;
                x /= 2;
            }
            cf[1] ++;
        }
    }
    dfs(1, 0);
    for(int i = 0; i <= m; ++ i)    printf("%d ", ans[i]);
    return 0;
}

F题没做

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值