当前位置: 首页 > 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.jsqmd.com/news/43640/

相关文章:

  • 2025年福祉座椅定制厂家权威推荐榜单:轮椅升降平台/轮椅升降机/福祉车源头厂家精选
  • 如何通过 KubeSphere 构建云原生平台,加速金融科技业务创新
  • SQL学习:WITH RECURSIVE
  • 2025年温度传感器批发厂家权威推荐榜单:水温传感器/传感器/红外温度传感器源头厂家精选
  • 视频汇聚平台EasyCVR构筑新时代边防哨所的“智能视觉防线”
  • 【哲学思考】我常用的方法论
  • 30、cp 、mv 命令
  • linux apache php配置
  • [随笔15] 日常杂事 - 枝-致
  • linux apache php 配置
  • M02694:波兰表达式 25-11-18
  • 详细介绍:金融专业毕业设计:python股票数据分析预测系统 神经网络LSTM预测算法 股价预测 深度学习 requests爬虫 Flask框架 大数据 毕业设计✅
  • linux android环境
  • 【E3S出版 | 高录用快见刊 | 即将截稿】第二届环境工程、城市规划与设计国际学术会议(EEUPD 2025)
  • 2025年塑料回收公司排名:这些企业领跑行业,市场可靠的塑料回收品牌选哪家聚焦优质品牌综合实力排行
  • 2025年塑料回收企业区域影响力榜单,评价好的塑料回收直销厂家排行榜单聚焦优质品牌综合实力排行
  • 2025年塑料回收行业领军企业排名出炉,塑料回收供应商TOP企业引领行业技术新高度
  • 2025年度塑料回收行业领军企业TOP5,塑料回收排行综合实力与口碑权威评选
  • 2025杭州最好的留学中介排名榜单
  • 2025年系统门窗10大品牌定做厂家推荐榜单:系统门窗厂家/系统门窗制造商/系统门窗价格源头厂家精选
  • 2025年全年度隔热条品牌权威排名榜单:若克斯新材料领跑行业
  • Python 机器学习03 - 常见分类算法
  • 用Python代码理解和实现简单的神经网络
  • Java哈希表入门详解(Hash) - 指南
  • AE/PR电影级视频调色插件 Shift for Adobe V1.2 Win附使用教程
  • 2025年不锈钢桥梁防护栏生产厂家权威推荐:201不锈钢桥梁护栏/不锈钢桥梁护栏杆/桥梁不锈钢防撞护栏源头厂家精选
  • 2025年11月国内百叶窗企业综合实力排行榜单:专业厂家推荐与选择指南
  • 2025年国内百叶窗厂家综合实力排行榜TOP10推荐
  • 预制装配式厨房厂 ,预制整体厨房定制厂家,民宿成品卫生间厂,宾馆集成卫生间厂 ,民宿快装式墙板厂 ,宿舍成品卫生间工厂,养老院整体厨房直供 --南京正标环保
  • 2025 最新年教务管理系统软件公司推荐!教培机构教务管理系统软件公司口碑排行榜,覆盖多校区 / 连锁 / 学科类 / 文化课机构优质解决方案