Magic Master (The 2019 Asia Nanchang First Round Online Programming Contest)
John is not only a magic master but also a shuffling master.
Famous though he is, he likes interacting with his fans by playing a game with his fantastic shuffling skills.
The game shows as follows:
He first shows a deck of pokers contains NN cards indexed 1,2,\dots ,N1,2,…,N and all the cards are the same. He notes that the side contains numbers as the front side and the other side as the back. Later he selects one of his fans to choose a positive integer MM that is not greater than 1010. After that, he will shuffle the cards purposefully and put it on the desktop. Note that at this moment the front sides of the cards are facing to the desk.
Then, the selected fans should perform the following steps until there is no card on the desk.
- Place the card on the top of the piles to John's hand. If there are already some cards in his hand, put it on the top of them.
- If there remain cards on the desk:
(a) Put a card from the top of the piles to the bottom of it.
(b) Execute the step (aa) of MM times.
(c) Go to step 11.
Next, it is time to witness the miracle.
John will continually overturn the cards on his hand top to bottom, and we will find that the cards are always in decreasing order.
One day, one of John's fans, Tom, a smart JBer, understands the key to this magic. He turns to you and comments that the key to that magic is the shuffling John did previously. For any number MM, he will turn the cards in a specific order after the shuffling. As you are not as smart as Tom, you should find the index of the KKth cards from the top of the piles after the shuffling.
Input
The first line contain a positive integer T (1\le T \le10)T(1≤T≤10) – the number of test cases.
For each test cases, the first line contain a positive integer N (1 \le N \le 40000000)N(1≤N≤40000000) , indicating the numberof cards.
The second line contain a positive integer M (1\le M \le 10)M(1≤M≤10) – the number the fans select.
The third line contain an integer Q (1 \le Q \le 100)Q(1≤Q≤100) – indicating that there are QQ questions.
Next, there are QQ lines, each with an integer K (1 \le K \le N)K(1≤K≤N) – representing a query.
Output
For each query, you should output the index of the KKth cards from the top of the piles after the shuffling.
样例输入
1 5 1 2 2 3
样例输出
5 2
样例解释
Sample description - The indexs of the cards after the shuffling are: 1~5~2~4~31 5 2 4 3 (Left is top)
#include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<stack> using namespace std; typedef long long ll; int ans[40000000]; queue<int>q;int x; int main() { ll n,m; ll t;scanf("%lld",&t); while(t--) {scanf("%lld%lld",&n,&m); while(!q.empty()) q.pop(); for(int i=n;i>1;i--) { q.push(i); for(int j=1;j<=m;j++) { ll k=q.front(); q.pop(); q.push(k); } } ans[1]=1; for(int i=n;i>1;i--) { ans[i]=q.front(); //cout<<ans[i]<<' '; q.pop(); } int ff;scanf("%d",&ff); for(int i=1;i<=ff;i++) { scanf("%d",&x); printf("%d\n",ans[x]); } } }
原文地址:https://www.cnblogs.com/Shallow-dream/p/11488851.html
- 【Java学习笔记之三十二】浅谈Java中throw与throws的用法及异常抛出处理机制剖析
- Linux上访问SQL Server数据库
- 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
- 【Java学习笔记之三十三】详解Java中try,catch,finally的用法及分析
- 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别
- NET跨平台:在Ubuntu下搭建ASP.NET 5开发环境
- 【Code】关关的刷题日记22——Leetcode 53. Maximum Subarray
- 【Java学习笔记之三十四】超详解Java多线程基础
- Codeforces 842B Gleb And Pizza【几何,水】
- Python3希尔排序
- Codeforces 842A Kirill And The Game【暴力,水】
- Wannafly模拟赛 A.矩阵(二分答案+hash)
- 【Java数据结构学习笔记之一】线性表的存储结构及其代码实现
- Comparison of Apache Stream Processing Frameworks: Part 1
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- strpos() 函数判断字符串中是否包含某字符串的方法
- Laravel框架基于ajax和layer.js实现无刷新删除功能示例
- 详解php伪造Referer请求反盗链资源
- Laravel框架基于ajax实现二级联动功能示例
- django haystack实现全文检索的示例代码
- 基于Python下载网络图片方法汇总代码实例
- 基于pytorch中的Sequential用法说明
- pytorch VGG11识别cifar10数据集(训练+预测单张输入图片操作)
- tp5(thinkPHP5)框架数据库Db增删改查常见操作总结
- PHP基于cookie实现统计在线人数功能示例
- PHP实现对数字分隔加千分号的方法
- PHP验证类的封装与使用方法详解
- Laravel框架实现的rbac权限管理操作示例
- Laravel框架实现的批量删除功能示例
- PyTorch中model.zero_grad()和optimizer.zero_grad()用法