浙江农林大学第二十一届程序设计竞赛校选拔赛(同步)

瓜瓜的A+B

#include <iostream>
using namespace std;

typedef long long ll;

ll qmi(ll a, ll b, ll p)
{
    ll res = 1;
    while (b)
    {
        if (b & 1) res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}

int main()
{
    int _; scanf("%d", &_);
    while (_ -- )
    {
        ll a, b, p; scanf("%lld%lld%lld", &a, &b, &p);
        printf("%lld\n", qmi((a + b) % p, p, p));
    }
}

阿巴阿巴

#include <iostream>
using namespace std;

const int N = 1e3 + 10;

int cnta[N], cntb[N];

int main()
{
    int _; scanf("%d", &_);
    while (_ -- )
    {
        int n;
        string s;
        cin >> n >> s;
        for (int i = 0; i < n; i ++ )
        {
            cnta[i] = cnta[i - 1] + (s[i] == 'a');
            cntb[i] = cntb[i - 1] + (s[i] == 'b');
        }
        
        bool ok = false;
        for (int l = 0; l + 7 < n; l ++ )
        {
            int r = l + 7;
            if (cnta[r] - cnta[l - 1] == 4 && cntb[r] - cntb[l - 1] == 4)
            {
                ok = true;
                cout << l + 1 << ' ' << r + 1 << endl;
                break;
            }
        }
        
        if (!ok) cout << "impossible" << endl;
        
        for (int i = 0; i < n; i ++ ) cnta[i] = cntb[i] = 0;
    }
}

俺拜俺拜

  • 注意map如果ma[x]还不一定被赋值过的时候,一定要先count!然后在if里面再套一层if,不能&&
  • 因为如果你用了ma[x],直接自动给它赋值为0了,在此之后的count(x)操作全是true,因此,使用ma[x]需要谨慎(先用count)
#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
    int _; scanf("%d", &_);
    while (_ -- )
    {
        unordered_map<int, int> ma;
        int n, x; scanf("%d%d", &n, &x);
        string s; cin >> s;
        
        ma[0] = -1;
        
        bool ok = false;
        int cnt = 0;
        for (int i = 0; i < n; i ++ )
        {
            if (s[i] == 'a') cnt ++ ;
            else cnt -- ;
            
            if (ma.count(cnt))
            {
                if (i - (ma[cnt] + 1) + 1 >= x)
                {
                    ok = true;
                    printf("%d %d\n", ma[cnt] + 2, i + 1);
                    break;
                }
            }
            else ma[cnt] = i;
        }
        
        if (!ok) puts("impossible");
    }
}

瓜瓜的特别任务

  • 字符串哈希时注意 1.字符串下标要从1开始;2.p[0] = 1初始化
#include <iostream>
using namespace std;

typedef unsigned long long ull;

const int N = 5e3 + 10, P = 131;

ull h[N], p[N];

ull get(int l, int r)
{
    return h[r] - h[l - 1] * p[r - l + 1];
}

int main()
{
    int _; scanf("%d", &_);
    while (_ -- )
    {
        string s; cin >> s; s = " " + s;
        int n = (int)s.size();
        
        p[0] = 1;
        for (int i = 1; i <= n - 1; i ++ )
        {
            h[i] = h[i - 1] * P + s[i];
            p[i] = p[i - 1] * P;
        }
        
        int mx = 1;
        for (int len = 1; len <= n - 1; len ++ )
        {
            for (int i = 1; i + len - 1 <= n - 1; i ++ )
            {
                int cnt = 1, j = i + len;
                while (j + len - 1 <= n - 1 && get(i, i + len - 1) == get(j, j + len - 1))
                {
                    cnt ++ ; j += len;
                }
                mx = max(mx, cnt);
            }
        }
        printf("%d\n", mx);
    }
}

磊爷与佣兵战记

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

typedef long long ll;

const int N = 2e5 + 10;

ll w[N];
ll sum[N];
bool is_lazy[N];

vector<int> g[N];
vector<int> lazy[N];

int main()
{
    int n, m; scanf("%d%d", &n, &m);
    
    for (int i = 0, a, b; i < m && scanf("%d%d", &a, &b); i ++ )
    {
        g[a].push_back(b);
        g[b].push_back(a);
    }
    
    int t = sqrt(n);
    for (int i = 1; i <= n; i ++ )
    {
        if (g[i].size() >= t)
        {
            is_lazy[i] = true;
            for (auto u : g[i])
                lazy[u].push_back(i);
        }
    }
    
    int q; scanf("%d", &q);
    int op, p;
    while (q -- )
    {
        scanf("%d%d", &op, &p);
        if (op == 1)
        {
            int x; scanf("%d", &x);
            w[p] += x;
            if (is_lazy[p]) sum[p] += x;
            else
            {
                for (auto v : g[p])
                    w[v] += x;
            }
        }
        else
        {
            ll res = w[p];
            for (auto v : lazy[p])
                res += sum[v];
            printf("%lld\n", res);
        }
    }
}

瓜瓜的春天

  • dp时注意是否要使用滚动数组
  • 使用滚动数组的时候,注意,在初始化和最后输出结果的时候也要&1
#include <iostream>
using namespace std;

typedef long long ll;

const int N = 1e4 + 10;
const ll mod = 998244353;

ll f[2][N];

ll qmi(ll a, ll b, ll p)
{
    ll res = 1;
    while (b)
    {
        if (b & 1) res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}

int main()
{
    int ii, jj, a, b; scanf("%d%d%d%d", &ii, &jj, &a, &b);
    
    ll inv3 = qmi(3, mod - 2, mod);
    
    f[1 & 1][1] = inv3;
    
    for (int i = 1; i <= ii; i ++ )
        for (int j = 1; j <= jj; j ++ )
        {
            if (i == 1 && j == 1) continue;
            f[i & 1][j] = ((f[i & 1][j - 1] * inv3 % mod + f[i - 1 & 1][j] * inv3 % mod) % mod + f[i - 1 & 1][j - 1] * inv3 % mod) % mod;
        }

    printf("%lld\n", f[ii & 1][jj]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值