2023牛客寒假算法基础集训营3(A~G)

A题:

不断减损的时间

做法:根据题意要总和最小,所以遍历一下,偶数出一直除2,直到是奇数停或者0停止,但是注意,负数是不能除的,这样负数就会变大,代码如下:

/*
coder:sunshine
school:njupt
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' //交互题删掉
#define x first
#define y second
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;

const int N = 1e5 + 10;
int a[N], n;
ll sum;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if (a[i] <= 0)
        {
            sum += a[i];
            continue;
        }
        while (a[i] % 2 == 0)
        {
            a[i] /= 2;
            if (a[i] == 0)
                break;
        }
        sum += a[i];
    }
    cout << sum << endl;
    return 0;
}

F题:

迎接终结的寂灭

做法:签到题,输出42就行。

print(42)

D题:

宿命之间的对决

做法:一道博弈论,模拟几个样例发现结果了,就是奇偶有关,代码如下:

/*
coder:sunshine
school:njupt
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' //交互题删掉
#define x first
#define y second
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    if (n & 1)
    {
        cout << "yukari" << endl;
    }
    else
    {
        cout << "kou" << endl;
    }
    return 0;
}

C题:

忽远忽近的距离

做法:一道思维构造题, n n n等于7 小于4都是no。
如果 n n n刚好模 4 4 4 就每四个 3412 3 4 1 2 3412 就行
如果模余 1 1 1 就最后五个 34512 3 4 5 1 2 34512
2 2 2 最后 6 6 6 456123 4 5 6 1 2 3 456123
3 3 3 最后 11 11 11 3416285101179 3 4 1 6 2 8 5 10 11 7 9 3416285101179

代码如下:

/*
coder:sunshine
school:njupt
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' //交互题删掉
#define x first
#define y second
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;

const int N = 1e5 + 10;
int a[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    if (n == 1 || n == 3 || n == 2 || n == 7)
    {
        cout << -1 << endl;
    }
    else
    {
        for (int i = 1; i <= n + 5; i++)
        {
            a[i] = i;
        }
        if (n % 4 == 0)
        {
            for (int i = 1; i <= n; i += 4)
            {
                swap(a[i], a[i + 2]);
                swap(a[i + 1], a[i + 3]);
            }
            for (int i = 1; i <= n; i++)
            {
                cout << a[i] << " \n"[i == n];
            }
        }
        else if (n % 4 == 1)
        {
            for (int i = 1; i < n; i += 4)
            {
                swap(a[i], a[i + 2]);
                swap(a[i + 1], a[i + 3]);
            }
            swap(a[n], a[n - 2]);
            swap(a[n], a[n - 1]);
            for (int i = 1; i <= n; i++)
            {
                cout << a[i] << " \n"[i == n];
            }
        }
        else if (n % 4 == 2)
        {
            for (int i = 1; i + 4 < n; i += 4)
            {
                swap(a[i], a[i + 2]);
                swap(a[i + 1], a[i + 3]);
            }
            swap(a[n], a[n - 3]);
            swap(a[n], a[n - 2]);
            swap(a[n], a[n - 1]);
            swap(a[n], a[n - 4]);
            swap(a[n], a[n - 5]);
            for (int i = 1; i <= n; i++)
            {
                cout << a[i] << " \n"[i == n];
            }
        }
        else if (n % 4 == 3)
        {
            for (int i = 1; i + 4 < n; i += 4)
            {
                swap(a[i], a[i + 2]);
                swap(a[i + 1], a[i + 3]);
            }
            swap(a[n - 2], a[n]);
            swap(a[n - 1], a[n - 3]);
            swap(a[n - 1], a[n - 6]);
            swap(a[n - 6], a[n - 7]);
            for (int i = 1; i <= n; i++)
            {
                cout << a[i] << " \n"[i == n];
            }
        }
    }

    return 0;
}

E题:

公平守望的灯塔

做法:给出一条斜边,求直角点,通过画图构造出两个全等三角形,就可以算出来这个点了,在验证下是不是整点,代码如下:

/*
coder:sunshine
school:njupt
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' //交互题删掉
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b == d && a != c)
    {
        if (abs(a - c) % 2 == 0)
        {
            cout << min(a, c) + abs(a - c) / 2 << " " << d + abs(a - c) / 2 << endl;
        }
        else
        {
            cout << "No Answer!" << endl;
        }
    }
    else if (a == c && b != d)
    {
        if (abs(b - d) % 2 == 0)
        {
            cout << a + abs(b - d) / 2 << " " << min(b, d) + abs(b - d) / 2 << endl;
        }
        else
        {
            cout << "No Answer!" << endl;
        }
    }
    else
    {
        if (abs(a - c) == abs(b - d))
        {
            cout << min(a, c) << " " << min(b, d) << endl;
        }
        else
        {
            int x = (a + b + c - d);
            int y = (b + c + d - a);
            if (x / 2 * 2 == x && y / 2 * 2 == y)
            {
                cout << x / 2 << " " << y / 2 << endl;
            }
            else
            {
                cout << "No Answer!" << endl;
            }
        }
    }
    return 0;
}

B题:

勉强拼凑的记忆

做法:推公式就行,注意要特判2不存在,代码如下:

for _ in range(int(input())):
    n=int(input())
    if n==2:
        print(-1)
    else:
        print((n+1)//2+(n-(n+1)//2)//3)

G题:

严肃古板的秩序

做法:一道纯粹的DFS题,不过要注意的是快速幂中, a a % b 会爆 l o n g l o n g , 需要 ( a % b ) a % b ,或者开 _ _ i n t 128 a^a\%b会爆long long,需要(a\%b)^a\%b,或者开\_\_int128 aa%b会爆longlong,需要(a%b)a%b,或者开__int128

代码如下:

/*
coder:sunshine
school:njupt
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' //交互题删掉
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;

const int N = 20005;
char s[N];
ll cnt;
int n;
int len;
bool flag;
vector<int> a;
int qmi(ll m, ll k, ll p)
{
    ll res = 1 % p, t = m;
    while (k)
    {
        if (k & 1)
            res = ((__int128)res * t) % p;
        t = ((__int128)t * t) % p;
        k >>= 1;
    }
    return res;
}

void dfs(int now, ll ans, string s)
{
    if (flag)
        return;
    if (now == len)
    {
        if (ans == cnt)
        {
            cout << a[0];
            for (int i = 0; i < s.size(); i++)
            {
                cout << s[i] << a[i + 1];
            }
            flag = true;
        }
        return;
    }
    if (ans > 0)
    {
        dfs(now + 1, qmi(ans, ans, a[now]), s + '#');
    }
    dfs(now + 1, ans - a[now], s + '-');
    dfs(now + 1, ans + a[now], s + '+');
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> s;
    n = strlen(s);
    ll x = 0;
    vector<ll> v;
    for (int i = 0; i < n; i++)
    {
        if (s[i] >= '0' && s[i] <= '9')
            x = x * 10 + s[i] - '0';
        else
        {
            a.push_back(x);
            x = 0;
        }
    }
    len = a.size();
    cnt = x;
    dfs(1, a[0], "");
    if (flag)
        cout << "=" << cnt;
    else
        cout << -1 << endl;
    return 0;
}

后面还有四道,等明天起来补一下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值