SDUT 2023 summer team contest(for 22) - 10

文章讲述了如何使用动态规划解决H-Exam中的问题,涉及状态转移方程和优化策略,包括通过1、2和3种操作让数值达到特定目标,同时讨论了区间最大公约数在另一个题目中的应用。
摘要由CSDN通过智能技术生成

H - Exam

题意:就是给你一个数N,和一个方案数K,初始值为1,你有三种操作方式让值恰为N

1:自身加1;        2:加上X;       3:自身乘以7;

要你确定一X使得你的方案数恰好为K

思路:一开始根本没有去想dp,只是感觉是一个组合数之类得题,赛后与其他队交流了一下,说是dp。。。(dp真是🔪我千百遍啊。。)思路见注释

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N = 2e5 + 10;
const int INF = 1e18;
const int mod = 1e9 + 7;
int n, k, ads, m, t, x, ans, a[N], ad, jd, f[N];
string s;
void ClearFloat()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int read()
{
    int ret = 0, f = 1;
    char ch = getchar();
    while ('0' > ch || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while ('0' <= ch && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * f;
}
/*
状态表示:f[i]表示和为i的所有集合
属性:方案数量
状态计算:f[i]+=f[i-1]
         f[i]+=f[i-x];(前提大于x)
         f[i]+=f[i/7];(前提为7的倍数)
*/
signed main()
{
    n = read(), m = read();
    for (int i = 2; i < n; i++)
    {

        for (int j = 0; j <= n; j++)//每次枚举一个x都要初始化为0

            f[j] = 0;

        f[1] = 1; // 初始化f[1]为1,因为到达1的方式只有一种那就是初始状态
        for (int j = 1; j <= n; j++)
        {
            f[j] += f[j - 1]; // 其通过1操作可以到达
            if (j > i)
                f[j] += f[j - i]; // 或者j大于x时,通过2操作到达,注意一定要大于,应为初始值为1
            if (j % 7 == 0)       // 或者为7的倍数,通过操作3来到达此时的值,
                f[j] += f[j / 7];
        }

        if (f[n] == m) // 由于枚举顺序由小到达,故一定为最优解
        {
            printf("%lld\n", i);
            return 0;
        }
    }
    printf("0\n");
}

D - 38 parrots

题意:就是给你一个数n,接下来n个操作,+加入一个动物的长度,--减去最前面的一个动物操作,询问当前有没有一个动物可以用其的倍数来表示全部的动物长度在,

思路:我们想这一定和区间最大公约数有关,但是如果不超时的维护他们呢,很明显我们维护区间最大公约数和区间最小值,因为我们知道,这个动物的长度一定是最小的,且一定是区间最大公约数,因此我们可以考虑用线段树来维护,且可以离线的操作,只需要动态根据加操作和减操作来更新查询的左右区间即可(我们甚至不用维护区间和,因为根本没有用

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define xx first
#define yy second
#define endl "\n"
#define lowbit(x) x & (-x)
#define int long long
#define ull unsigned long long
#define pb push_back
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
#define max(a, b) (((a) > (b)) ? (a) : (b))
// #define min(a, b) (((a) < (b)) ? (a) : (b))
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 6e5 + 10, M = 1010, inf = 0x3f3f3f3f, mod = 18446, P = 13331;
const double eps = 1e-8;
string s[N];
// map<int, int> mp;
int w[N], cnt, n;
struct Node
{
    int l, r;
    int sum, d;
    int minn;
} tr[N * 4];

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
void pushup(Node &u, Node &l, Node &r)
{
    u.sum = l.sum + r.sum;
    u.minn = min(l.minn, r.minn);
    u.d = gcd(l.d, r.d);
}
void pushup(int u)
{
    pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void build(int u, int l, int r)
{
    if (l == r)
    {
        int b = w[r];
        tr[u] = {l, r, b, b, b};
    }
    else
    {
        tr[u].l = l, tr[u].r = r;
        int mid = l + r >> 1;
        build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
}
Node query(int u, int l, int r)
{
    if (tr[u].l >= l && tr[u].r <= r)
        return tr[u];
    else
    {
        int mid = tr[u].l + tr[u].r >> 1;
        if (r <= mid)
            return query(u << 1, l, r);
        else if (l > mid)
            return query(u << 1 | 1, l, r);
        else
        {
            auto left = query(u << 1, l, r);
            auto right = query(u << 1 | 1, l, r);
            Node res;
            pushup(res, left, right);
            return res;
        }
    }
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> s[i];
        if (s[i][0] == '+')
        {
            string p = s[i].substr(1);
            int x = stoi(p);
            w[++cnt] = x;
        }
    }
    build(1, 1, cnt);
    int l = 1, r = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[i][0] == '+')
        {
            r++;
        }
        else if (s[i][0] == '-')
        {
            l++;
        }
        else
        {
            int x = query(1, l, r).d;
            int y = query(1, l, r).minn;
            if (x == y)
                cout << "Y" << x << endl;
            else
                cout << "N" << endl;
        }
    }
}
signed main()
{
    Ysanqian;
    int T;
    T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}

A - Normal Magic Square

 思路:就是求其1~n方的和除以行数即可,可以用等差数列求和

就不放代码了

J - Multidimensional Points

 思路:很明显就是一个前缀和,即可

I - Hole Punch

 思路:将其分解约数,除去1(最后一并输出1)和本身,如果除以这个因子为偶数则输出即可,

注意输出顺序

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define xx first
#define yy second
#define endl "\n"
#define lowbit(x) x & (-x)
#define int long long
#define ull unsigned long long
#define pb push_back
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 10, M = 1010, inf = 0x3f3f3f3f, mod = 18446, P = 13331;
const double eps = 1e-8;
int n, ans;
vector<int> g;
priority_queue<int, vector<int>, greater<int>> q;
void fun(int x)
{
    for (int i = 1; i <= x / i; i++)
    {
        if (x % i == 0)
        {
            g.pb(i);
            if (x / i != i)
                g.pb(x / i);
        }
    }
}
void solve()
{
    cin >> n;
    fun(n);
    for (auto ed : g)
    {
        if (ed != 1 && ed != n && n % ed == 0 && n / ed % 2 == 0)
        {
            q.push(ed);
            ans++;
        }
    }
    cout << ans + 1 << endl;
    cout << 1 << ' ';
    while (q.size())

    {
        int u = q.top();
        cout << u << ' ';
        q.pop();
    }
}
signed main()
{
    Ysanqian;
    int T;
    T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}

E - Black Box

题意:就是给你一个倒叙的01串,他是由原01串,左移位数,相加得来的,求原01串,注意倒序输出

思路:我们可以发现其与原串相加一定是0000长度不一定(以题目的n==4为例)

然后就是如何计算了,我们发现,我们只要让原串与给定串第一个不为0的地方相同,其余后面的取反,前面同为0,即可得到原串。

例如样例1101,第一位就不是0,那么我们这位取1,其余取反,答案就是1010

又如0010  , 原串就是0011,第三位才位1,我们前两位取0第三与其相同,后面取反即可

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define xx first
#define yy second
#define endl "\n"
#define lowbit(x) x & (-x)
#define int long long
#define ull unsigned long long
#define pb push_back
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define Ysanqian ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = 1e6 + 10, M = 1010, inf = 0x3f3f3f3f, mod = 18446, P = 13331;
const double eps = 1e-8;
int n;
string s;
void solve()
{
    cin >> n;
    cin >> s;
    int i = 0;
    while (s[i] == '0')
    {
        i++;
        cout << '0';
    }
    if (i < s.size())
        cout << '1';
    i++;
    for (i; i < s.size(); i++)
    {
        if (s[i] == '0')
            cout << '1';
        else
            cout << '0';
    }
}
signed main()
{
    Ysanqian;
    int T;
    T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值