当前位置: 首页 > news >正文

CF2172H - Shuffling Cards with Problem Solver 68!

Aru loves playing card games (Poker, Texas hold 'em, Balatro, etc.) and she has perfected the art of shuffling cards, especially the riffle shuffle. She is playing with Mutsuki now, and it's her turn to shuffle the cards!

However, Mutsuki knows that Aru is too perfect with her shuffling game. In fact, given a deck with an even number of cards, Aru always performs a perfect riffle: she cuts the deck evenly and interleaves the two halves. Formally, if the deck is represented by a string \(s\) of length \(n\), where \(s_i\) is the \(i\)-th card from the top, one riffle produces the deck

\[s^\prime = s_1 + s_{n/2 + 1} + s_2 + s_{n/2 + 2} + \ldots + s_{n/2} + s_{n}. \]

Mutsuki also knows that when handed a deck of cards, Aru will riffle it exactly \(t\) times.

Mutsuki currently holds a deck of \(2^k\) cards, represented by a string \(d\). Before giving the deck to Aru, Mutsuki can choose to cut the deck, by moving some number of cards from the top to the bottom of the deck. Formally, she can choose any \(m\) from \(0\) to \(2^k - 1\), and produce the deck

\[d^\prime = d_{m + 1} + d_{m + 2} + \ldots + d_{2^k} + d_1 + d_2 + \ldots + d_m. \]

Among all \(2^k\) possible cuts, Mutsuki wants to choose the one that results in the lexicographically smallest deck after Aru riffles it \(t\) times. Can you figure this out for her?

Input

The first line contains two integers \(k\) and \(t\), representing the size parameter of the deck, and the number of times Aru will riffle the deck, respectively.

The second line contains a string \(d\) of \(2^k\) lowercase characters, representing the original deck of cards that Mutsuki has.

  • \(1 \le k \le 18\)
  • \(0 \le t \le 10^9\)

Output

Print a string in one line, representing the lexicographically smallest deck of cards that Mutsuki can produce, by first cutting the deck and letting Aru riffle it \(t\) times.


考虑外洗法的洗牌规律,不妨设牌从上到下为 \(s[0 \sim n - 1]\),不难发现一次洗牌相当于在模 \(n - 1\) 意义下乘二,而 \(ord_{2^k - 1}(2) = k\),也即阶为 \(k\),故题面的 \(t\) 可以视为 \(k\) 的大小。

考虑预处理出每张牌的映射,我们可以知道对于任意一个位移的字符串是什么,所以可以对每个位移做排序。而做排序也很简单,只用二分出每一个字符串相等的部分,找到一下个恰好满足条件的字符串即可。

时间复杂度 \(O(n\log^2{n})\)\(O(n\log{n})\)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define ld long double
#define PII pair<ll, ll>
#define PPI pair<ll, PII>
#define clr(f, n) memset(f, 0, sizeof(int) * (n))
#define cpy(f, g, n) memcpy(f, g, sizeof(int) * (n))
#define rev(f, n) reverse(f, f + (n))
const int N = 2e5 + 10, mod = 998244353, INF = 1e9;
const ll B = 331;void solve() {int k, t, n;cin >> k >> t, n = 1 << k, t %= k;string s; cin >> s;vector<int> p(n);vector<vector<ll>> st(k, vector<ll>(n));vector<ll> base = {B}; base.resize(k);iota(p.begin(), p.end(), 0);while (t -- ) for (int i = 1; i < n - 1; i ++ ) p[i] = (p[i] >> 1) | ((p[i] & 1) << (k - 1));for (int i = 1; i < k; i ++ ) base[i] = base[i - 1] * base[i - 1];for (int i = 0; i < k; i ++ ) {for (int j = 0; j < n; j ++ ) {if (!i) st[i][j] = s[j];else st[i][j] = st[i - 1][j] * base[i - 1] + st[i - 1][(j + p[1 << (i - 1)]) % n];}}auto cmp = [&](int x, int y) {for (int i = k - 1; i >= 0; i -- ) {if (st[i][x] == st[i][y]) {x = (x + p[1 << i]) % n;y = (y + p[1 << i]) % n;}}return s[x] < s[y];};int ans = 0;for (int i = 1; i < n; i ++ ) if (cmp(i, ans)) ans = i;for (int i = 0; i < n; i ++ ) cout << s[(p[i] + ans) % n];
}int main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int T = 1;// cin >> T;while (T -- ) solve();return 0;
}
http://www.gsyq.cn/news/53130.html

相关文章:

  • 2025年福祉座椅定制厂家权威推荐榜单:轮椅升降平台/轮椅升降机/福祉车源头厂家精选
  • SQL学习:WITH RECURSIVE
  • 【哲学思考】我常用的方法论
  • 30、cp 、mv 命令
  • linux apache php配置
  • [随笔15] 日常杂事 - 枝-致
  • linux android环境搭建
  • linux android环境
  • 设置word中第一页不显示页码,第二页页码从1开始
  • 2025年度塑料回收行业领军企业TOP5,塑料回收排行综合实力与口碑权威评选
  • 2025成都留学机构十强名单排名
  • P5658 [CSP-S 2019] 括号树 题解
  • Python 机器学习03 - 常见分类算法
  • 2025年全年度隔热条品牌权威排名榜单:若克斯新材料领跑行业
  • 用Python代码理解和实现简单的神经网络
  • Java哈希表入门详解(Hash) - 指南
  • AE/PR电影级视频调色插件 Shift for Adobe V1.2 Win附使用教程
  • 2025年不锈钢桥梁防护栏生产厂家权威推荐:201不锈钢桥梁护栏/不锈钢桥梁护栏杆/桥梁不锈钢防撞护栏源头厂家精选
  • 2025 最新年教务管理系统软件公司推荐!教培机构教务管理系统软件公司口碑排行榜,覆盖多校区 / 连锁 / 学科类 / 文化课机构优质解决方案
  • 区块链交易所中心化架构与风控体系详解
  • 2025 年无锡短视频拍摄公司推荐,企拓网络 14 年深耕新媒体营销,短视频全案运营赋能企业高效拓客
  • linux android 环境变量
  • 2025年贴标机生产厂家权威推荐榜单:直角贴标机/自动贴标机/矿泉水贴标机源头厂家精选
  • 2025年双车道双翻集装箱翻转机厂家权威推荐榜单:20吨集装箱翻转机/双车道单翻集装箱翻转机/40尺集装箱翻转机源头厂家精选
  • springboot~通过集成测试来理解Accept和Content-Type
  • 【马来西亚理工大学主办,SPIE出版】2025年量子计算与通信技术国际学术会议(ICQCT 2025)
  • 详细介绍:Next steps for BPF support in the GNU toolchain
  • 2025成都留学中介机构排名前十
  • 2025美国留学开除处理机构推荐,靠谱申诉/转学/身份保障服务哪家好
  • 【马来亚大学主办,SPIE出版,快至会后4个月检索】2025年医学图像处理与识别国际会议(IPOR 2025)