acwing算法提高之基础算法--位运算、递推与递归

123 篇文章 1 订阅

1 介绍

本博客用来记录位运算、递推与递归相关的题目。

2 训练

题目1第90题-64位整数乘法

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

LL qadd(LL a, LL b, LL p) {
    LL res = 0;
    while (b) {
        if (b & 1) res = (res + a) % p;
        a = (a + a) % p;
        b >>= 1;
    }
    return res;
}

int main() {
    LL a, b, p;
    cin >> a >> b >> p;
    
    cout << (LL)qadd(a, b, p) << endl;
    
    return 0;
}

题目295费解的开关

C++代码如下,

#include <cstdio>
#include <cstring>

using namespace std;

const int N = 6;

char g[N][N], bg[N][N];
int dx[5] = {-1, 0, 1, 0, 0}, dy[5] = {0, 1, 0, -1, 0};

void turn(int x, int y) {
    for (int i = 0; i < 5; ++i) {
        int a = x + dx[i], b = y + dy[i];
        if (a < 0 || x >= 5 || b < 0 || b >= 5) continue;
        g[a][b] ^= 1;
    }
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        for (int i = 0; i < 5; ++i) scanf("%s", bg[i]);
        
        int res = 10;
        for (int op = 0; op < 32; op++) {
            int cnt = 0;
            memcpy(g, bg, sizeof g);
            for (int i = 0; i < 5; ++i) {
                if (op >> i & 1) {
                    turn(0, i);
                    cnt++;
                }
            }
            
            for (int i = 0; i < 4; ++i) {
                for (int j = 0; j < 5; ++j) {
                    if (g[i][j] == '0') {
                        turn(i + 1, j);
                        cnt++;
                    }
                }
            }
            
            bool success = true;
            for (int i = 0; i < 5; ++i) {
                if (g[4][i] == '0') {
                    success = false;
                }
            }
            if (success && res > cnt) res = cnt;
        }
        
        if (res > 6) res = -1;
        printf("%d\n", res);
    }
    
    return 0;
}

题目397约数之和

C++代码如下,

#include <cstdio>

const int mod = 9901;

int qmi(int a, int k) {
    int res = 1;
    a %= mod;
    while (k) {
        if (k & 1) res = res * a % mod;
        a = a * a % mod;
        k >>= 1;
    }
    return res;
}

int sum(int p, int k) {
    if (k == 1) return 1;
    if (k % 2 == 0) return (1 + qmi(p, k / 2)) * sum(p, k / 2) % mod;
    return (sum(p, k - 1) + qmi(p, k - 1)) % mod;
}

int main() {
    int a, b;
    scanf("%d%d", &a, &b);
    
    int res = 1;
    for (int i = 2; i * i <= a; ++i) {
        if (a % i == 0) {
            int s = 0;
            while (a % i == 0) {
                a /= i, s ++;
            }
            res = res * sum(i, b * s + 1) % mod;
        }
    }
    
    if (a > 1) res = res * sum(a, b + 1) % mod;
    if (a == 0) res = 0;
    
    printf("%d\n", res);
    
    return 0;
}

题目498分形之城

C++代码如下,

#include <cstdio>
#include <cstring>
#include <cmath>

typedef long long LL;

struct Point {
    LL x, y;
};

Point get(LL n, LL a) {
    if (n == 0) return {0,0};
    LL block = 1ll << n * 2 - 2, len = 1ll << n - 1;
    auto p = get(n - 1, a % block);
    LL x = p.x, y = p.y;
    int z = a / block;
    
    if (z == 0) return {y, x};
    else if (z == 1) return {x, y + len};
    else if (z == 2) return {x + len, y + len};
    return {len * 2 - 1 - y, len - 1 - x};
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        LL n, a, b;
        scanf("%lld%lld%lld", &n, &a, &b);
        auto pa = get(n, a - 1);
        auto pb = get(n, b - 1);
        double dx = pa.x - pb.x, dy = pa.y - pb.y;
        printf("%.0lf\n", sqrt(dx * dx + dy * dy) * 10);
    }
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值