LA3882 And Then There Was One
And Then There Was One
https://vjudge.net/problem/UVALive-3882
题目大意:n个数编号1..n排成一圈,第一次删除m,后来每k个删除一个(下一次删除m + k....),问最后剩下哪一个?
先考从0开始数,每k个删除一个。设f[i]表示共有i个数最后剩下的数是多少。
考虑删除k - 1后,第k个数重新标号为0,第k + 1重新标号为1.......,变成了i-1个数的情况,而i个数的情况标号是i-1个数的标号 + k得到的
有f[i] = (f[i-1]+k)%n
题目中要求先删除m,我们考虑先删除0,即整体坐标减k - 1,变为((0 - (k - 1) + f[n])%n + n)%n
然后考虑从m开始,即整体左移m - 1(因为从0开始计数)变为((0 - (k - 1) + f[n] + m - 1)%n + n)%n
最终答案((m - k + f[n])%n + n)%n + 1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = ; int n,m,k; int f[MAXN + ]; int main()
{
while(scanf("%d %d %d", &n, &k, &m) != EOF && n && m && k)
{
//f[i]表示i个数总0开始每k个删一个 (第一次删k - 1)
f[] = ;
for(register int i = ;i <= n;++ i)
f[i] = (f[i - ] + k) % i;
printf("%d\n", ((m - k + f[n]) % n + n) % n + );
}
return ;
}
LA3882
LA3882 And Then There Was One的更多相关文章
- 约瑟夫问题的变种 LA3882
题目大意: N个数排成一圈,第一次删除m,以后每k个数删除一次,求最后一被删除的数. 如果这题用链表或者数组模拟整个过程的话,时间复杂度都将高达O(nk),而n<=10000,k<=100 ...
- 【LA3882】And then there was one
做sb题也是一种乐趣,是吧…… #include<bits/stdc++.h> #define N 10005 using namespace std; int f[N],m,n,k; i ...
随机推荐
- Python第二课-输入输出
name = input() 输入的字符串已经赋值给变量name print() 输出内容 print(,) print中,连接字符串相当于空格
- POJ 1696 /// 凸包
题目大意: 不能向左拐 不能重复走 就是求一个螺旋凸包 把已经是凸包内的点标记一下就行 因为凸包的性质 所有点都能走到 注意起点的选择 还有 反复求凸包的过程中边界的改变 #include <c ...
- 用星星画菱形--Java
用星星画菱形 public class Hello{ public static void main(String[] args) { char star = '\u2605'; System.out ...
- Luogu P2717 寒假作业(平衡树)
P2717 寒假作业 题意 题目背景 \(zzs\)和\(zzy\)正在被寒假作业折磨,然而他们有答案可以抄啊. 题目描述 他们共有\(n\)项寒假作业.\(zzy\)给每项寒假作业都定义了一个疲劳值 ...
- Android开发 从代码里设置Drawable图片不显示的问题
问题描述 我们从代码里获得Drawable在设置给View时会发现,图片不显示的问题.比如如下代码: Drawable drawable = getResources().getDrawable(R. ...
- Atcoder arc085
C:HSI 期望模型,不想说. #include<cstdio> using namespace std; typedef long long ll; int main() { int n ...
- 二分图——poj2239
水题 /* n门课,每门课有一个时间t 要求最大的n->t的匹配 */ #include<iostream> #include<cstring> #include< ...
- centos 7 安装dotnet core
dotnetcore 支持的Linux 版本请看官网:https://docs.microsoft.com/zh-cn/dotnet/core/linux-prerequisites?tabs=net ...
- JEECMS文库工具安装
下载地址: Swftools下载地址 http://www.swftools.org/swftools-0.9.2.tar.gz openoffice下载地址 http://www.openoffic ...
- python流程控制-条件语句If,while循环
一.If,条件语句-选择 格式:python简洁优美,注意缩进 1.第一种: if 条件: 四个空格(tab键) 满足条件时的执行步骤 if 5>4 : print(666) print(77 ...