首页 > 其他分享 >AtCoder Beginner Contest 367

AtCoder Beginner Contest 367

时间:2024-08-22 13:17:32浏览次数:6  
标签:AtCoder cout Beginner int ios cin long 367 define

A - Shout Everyday

思路:水题一道,模拟即可。

B - Cut .0

思路:直接 cincout 即可,c++输入输出性质。

C - Enumerate Sequences

思路:注意到数据范围很小,因此考虑到搜素所有的序列,然后判断是否合法。

D - Pedometer

思路:观察到是环上问题,先断环为链,观察题目,可以发现,对于 s,它的终点范围在 [s+1,s+n-1]。同时,设 a 为到 s 的步数,设 b 为到 t 的步数,题目的限制条件可以转化为 (b - a) mod k = 0,即 b mod k = a mod k。因此,我们可以用哈希表来维护。

代码:

#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define pii pair<int, int>
#define debug(x) cout << "x = " << x << endl;
#define lowbit(x) (x & (-x))
using namespace std;
const int N = 5e5 + 5, P = 13331;
int n, a[N], m, ans;
map<int, int> cnt;
void solve()
{
    int T = 1;
    // cin>>T;
    while (T--)
    {
        cin >> n >> m;
        for (int i = 2; i <= n + 1; i++)
        {
            cin >> a[i];
        }
        for (int i = n + 2; i <= 2 * n; i++)
        {
            a[i] = a[i - n];
        }
        for (int i = 1; i <= 2 * n; i++)
        {
            a[i] = (a[i] + a[i - 1]) % m;
        }
        for (int i = 1; i <= n; i++)
        {
            cnt[a[i]]++;
        }
        for (int i = 1; i <= n; i++)
        {
            cnt[a[i]]--;
            ans += cnt[a[i]];
            cnt[a[i + n]]++;
        }
        cout << ans;
    }
}
signed main()
{
    ios;
    solve();
    return 0;
}

E - Permute K times

思路:可以观察到,每个点其实对应一个基环树,问题转化为求每个基环树的第k个点。考虑倍增。

代码:

/*
 * @OJ:
 * @ID:
 * @Author: ZhangChao
 * @Date: 2024-08-15 17:35:14
 */
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define pii pair<int, int>
#define debug(x) cout << "x = " << x << endl;
#define lowbit(x) (x & (-x))
using namespace std;
const int N = 5e5 + 5, P = 13331;
int n, x[N], a[N], k;
int dp[N][63];
int get(int d, int id)
{
    int s = id;
    for (int i = 62; i >= 0; i--)
    {
        if (((int)1 << i) <= d)
        {
            d -= ((int)1 << i);
            s = dp[s][i];
        }
    }
    return a[s];
}
void solve()
{
    int T = 1;
    // cin>>T;
    while (T--)
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
        {
            cin >> x[i];
        }
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++)
        {
            dp[i][0] = x[i];
        }
        for (int i = 1; i < 63; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                dp[j][i] = dp[dp[j][i - 1]][i - 1];
            }
        }
        for (int i = 1; i <= n; i++)
        {
            cout << get(k, i) << " ";
        }
    }
}
signed main()
{
    ios;
    solve();
    return 0;
}

标签:AtCoder,cout,Beginner,int,ios,cin,long,367,define
From: https://www.cnblogs.com/zc-study-xcu/p/18373634

相关文章

  • AtCoder Beginner Contest 046
    A-AtCoDeerandPaintCans#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;intmain(){ ios::sync_with_stdio(false),cin.tie(nullptr); set<int>s; for(inti=0;i<3;i++){ intx; cin>>x; s.inser......
  • [AtCoder - tdpc_game] :ゲーム 题解
    [AtCoder-tdpc_game]:ゲーム题解一道小清新\(dp\)题。定义\(dp_{i,j}\)为第一堆山还有\(i\)个物品,第二堆山还有\(j\)个物品,すぬけ君能取得物品的最大价值。由于只能取两座山最上面的物品,假设当前两座山分别有\({x,y}\)个物品,すぬけ君选后只能有两种情况,分别为\(d......
  • AtCoder ABC 367
    前言本题解部分思路来自于网络,仅供参考。A-ShoutEveryday题目大意给定Takahashi每天的睡觉时间和起床时间,求Takahashi在$A$时是睡着的还是清醒的。解题思路根据题意模拟即可。code#include<bits/stdc++.h>usingnamespacestd;intmain(){inta,b,c;......
  • ABC367
    Alink先判断一下时间是否跨天,如果跨天了,把后一个加上\(24\),使后一个大于前一个,再判断国王喊的时间或喊的时间加\(24\)是否在范围内。神奇的代码#include<bits/stdc++.h>usingnamespacestd;signedmain(){ inta,b,c; cin>>a>>b>>c; if(b>c)c+=24; ......
  • [ABC367D] Pedometer-xzy巨佬简洁做法
    [ABC367D]Pedometer-xzy巨佬简洁做法https://www.luogu.com/article/n64n78cs对照巨佬的代码进一步理解//徐知鱼#include<bits/stdc++.h>usingnamespacestd;inlineintread(){ intx=0,f=1; charch=getchar(); while(!isdigit(ch)){ if(ch=='-')f=......
  • AtCoder Beginner Contest 367
    题目链接:AtCoderBeginnerContest367总结:发挥很一般,A一直wa。开场有点事,导致D也没debug出来。A.ShoutEverydaytag:模拟Solution:注意\(B>C\)与\(B<C\)的不同情况即可。voidsolve(){  inta,b,c;  cin>>a>>b>>c;  if(c>b){    if(......
  • 题解:AT_abc367_c [ABC367C] Enumerate Sequences
    大致题意让你按照字典序输出一些长\(n\)的序列,序列中的数字有范围,且序列和需要为\(k\)。分析直接深搜。搜索的时候对从第一个元素开始,每个元素从小到大枚举所有可能的值,这样就保证答案按照字典序升序排列。用一个vector存储序列,到达边界之后计算一遍和,判断是否满足条件,然......
  • ABC 367D Pedometer
    题意给定N个人成的环,第i个人到第i+1个人之前的距离为a[i](第N个人到第1个人的距离为a[n]),每个人只能顺时针走动。求问有多少点对(s,t)使得第s个人走到第t个人的距离是M的倍数。思路对于这种环状问题,我们最开始的思路就是开个双倍数组把环破坏成链转换成线性问题。接下来就是我们......
  • ABC 367 G 题解
    ABC367G神奇题目场上想到了引入多元生成函数之后就嗝屁了。定义两个多项式的运算\(A(z)*B(z)=\sum_{i}\sum_{j}z^{i\oplusj}a_ib_j\),也就是异或卷积。定义两个二元生成函数\(A(x,y)*B(x,y)=\sum_{i,p}\sum_{j,q}x^{i\oplusj}y^{p+q}a_{i,p}b_{j,q}\)我们仍然选用\(\prod......
  • [LeetCode] 1367. Linked List in Binary Tree 二叉树中的链表
    Givenabinarytree root anda linkedlistwith head asthefirstnode.ReturnTrueifalltheelementsinthelinkedliststartingfromthe head correspondtosome downwardpath connectedinthebinarytree otherwisereturnFalse.Inthiscontext......