题目链接

传送门

题意

初始时你有\(n\)张牌(按顺序摆放),每一次操作你将顶端的牌拿出,然后按顺序将上面的\(m\)张牌放到底部。

思路

首先我们发下拿走\(1\)后就变成了总共有\(n-1\)个人数到\(m+1\)的人出局,问你每个人是第几个出局的,很明显的约瑟夫环。

比赛的时候我还在推公式,然后发现机房有人用暴力模拟过了,尤其是题解也是暴力,就很无语。

如果这题标程不假并且只给\(1s\),那么该怎么写呢?

这题由于\(n\)很大,我们肯定不能将\(n\)个人出局顺序一一枚举,然后我们发现\(q\)很小,因此我们就对查询的那个编号进行求解即可。

由于第一个人始终是第一个出局的,那么我们将他特判掉,然后\(n=n-1\),又由于是数到\(m+1\)出局,因此令\(m=m+1\),然后开始解题。

假设当前有\(n(n\geq m\)人,那么将会出局\(m,2m,3m,\dots,\lfloor\frac{n}{m}\rfloor*m\),然后将\(\lfloor\frac{n}{m}\rfloor*m+1,\lfloor\frac{n}{m}\rfloor*m+2,\dots,n\)和前面没有出局的人拼接起来变成\(\lfloor\frac{n}{m}\rfloor*m+1,\lfloor\frac{n}{m}\rfloor*m+2,\dots,n,1,\dots\),然后更新现在查询的人的编号是多少即可。

注意当\(n<m\)时,再用上式就会发现没有人出局,因此需要转变思路,每次出局编号为\((m-1)\% n+1\),\(m-1\)后再取模加一是处理掉模\(n\)等于\(0\)的情况,之后就和上面一样拼接即可,这部分只需要循环\(m\)次,也就是说最多循环\(10\)次。

用这种方法只需要跑\(2ms\),因此这次网络赛给的时限使得这题成为了一道假题。

代码

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson (rt<<1)
#define rson (rt<<1|1)
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 200000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int t, n, m, q, x; int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &m, &q);
++m;
while(q--) {
scanf("%d", &x);
if(x == 1) {
printf("1\n");
continue;
}
--x;
int tot = n - 1, ans = 1;
while(1) {
if(tot < m) {
int nw = (m - 1) % tot + 1;
if(nw == x) {
printf("%d\n", ans + 1);
break;
}
if(nw < x) x = x - nw;
else x = tot - nw + x;
++ans, --tot;
continue;
}
if(x % m == 0) {
printf("%d\n", ans + (x / m));
break;
}
ans += tot / m;
if(x > (tot / m) * m) x = x - (tot / m) * m;
else x = tot - (tot / m) * m + x - (x / m);
tot -= tot / m;
}
}
}
return 0;
}

Magic Master(2019年南昌网络赛E题+约瑟夫环)的更多相关文章

  1. 2019 ICPC南昌网络赛 B题

    英雄灭火问题忽略了一点丫 一个超级源点的事情,需要考虑周全丫 2 #include<cstdio> #include<cstring> #include<queue> ...

  2. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  3. ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval

    ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...

  4. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  5. 2019 ICPC 南昌网络赛I:Yukino With Subinterval(CDQ分治)

    Yukino With Subinterval Yukino has an array a_1, a_2 \cdots a_na1,a2⋯*a**n*. As a tsundere girl, Yuk ...

  6. 2019 ICPC上海网络赛 A 题 Lightning Routing I (动态维护树的直径)

    题目: 给定一棵树, 带边权. 现在有2种操作: 1.修改第i条边的权值. 2.询问u到其他一个任意点的最大距离是多少. 题解: 树的直径可以通过两次 dfs() 的方法求得.换句话说,到任意点最远的 ...

  7. Peekaboo(2019年上海网络赛K题+圆上整点)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 你的位置在\(O(0,0)\),\(A\)的位置为\((x_1,y_1)\),\(B\)的位置为\((x_2,y_2)\),现在已知\(a=O ...

  8. 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)

    目录 题目链接 思路 代码 题目链接 传送门 思路 首先我们将原式化简: \[ \begin{aligned} &\sum\limits_{l_1=1}^{n}\sum\limits_{l_2 ...

  9. 2019 ICPC南京网络赛 F题 Greedy Sequence(贪心+递推)

    计蒜客题目链接:https://nanti.jisuanke.com/t/41303 题目:给你一个序列a,你可以从其中选取元素,构建n个串,每个串的长度为n,构造的si串要满足以下条件, 1. si ...

随机推荐

  1. [LeetCode] 76. Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  2. spring cloud gateway 全局过滤器

    全局过滤器作用于所有的路由,不需要单独配置,我们可以用它来实现很多统一化处理的业务需求,比如权限认证,IP访问限制等等. 接口定义类:org.springframework.cloud.gateway ...

  3. Cipolla算法学习笔记

    学习了一下1个$\log$的二次剩余.然后来水一篇博客. 当$p$为奇素数的时候,并且$(n, p) \equiv 1 \pmod{p}$,用Cipolla算法求出$x^2 \equiv n \pmo ...

  4. Navicat的安装和pymysql模块的使用

    内容回顾 select distinct 字段1,字段2,... from 表名 where 分组之前的过滤条件 group by 分组条件 having 分组之后过滤条件 order by 排序字段 ...

  5. netcat 传输T级别大文件

    接收端 nc -d -l 5002 |tar xf - nohup  sh receive.sh  &   发送端 tar cf - . | nc  1.1.1.1  5002 nohup   ...

  6. C#编写了一个基于Lucene.Net的搜索引擎查询通用工具类:SearchEngineUtil

    最近由于工作原因,一直忙于公司的各种项目(大部份都是基于spring cloud的微服务项目),故有一段时间没有与大家分享总结最近的技术研究成果的,其实最近我一直在不断的深入研究学习Spring.Sp ...

  7. 关于DataTable内部索引已损坏的问题 System.Data.RBTree

    1.错误提示: 最近,Winform程序在极其偶然的情况下会遇到如下错误提示 Framework 版本: v4.0.30319 说明: 由于未经处理的异常,进程终止. 异常信息: System.Inv ...

  8. Spring69道面试题

    Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring  ...

  9. [转帖]从零开始入门 K8s | 手把手带你理解 etcd

    从零开始入门 K8s | 手把手带你理解 etcd https://zhuanlan.zhihu.com/p/96721097 导读:etcd 是用于共享配置和服务发现的分布式.一致性的 KV 存储系 ...

  10. C# 删除文件到回收站

    首先添加Microsoft.VisualBasic引用 程序中引用 Microsoft.VisualBasic.FileIO 来进行处理 Console.WriteLine("删除文件到回收 ...