cf287D Shifting
John Doe has found the beautiful permutation formula.
Let's take permutation p = p1, p2, ..., pn. Let's define transformation f of this permutation:
where k (k > 1) is an integer, the transformation parameter, r is such maximum integer that rk ≤ n. If rk = n, then elements prk + 1, prk + 2 and so on are omitted. In other words, the described transformation of permutation p cyclically shifts to the left each consecutive block of length k and the last block with the length equal to the remainder after dividing n by k.
John Doe thinks that permutation f(f( ... f(p = [1, 2, ..., n], 2) ... , n - 1), n) is beautiful. Unfortunately, he cannot quickly find the beautiful permutation he's interested in. That's why he asked you to help him.
Your task is to find a beautiful permutation for the given n. For clarifications, see the notes to the third sample.
A single line contains integer n (2 ≤ n ≤ 106).
Print n distinct space-separated integers from 1 to n — a beautiful permutation of size n.
2
2 1
3
1 3 2
4
4 2 3 1
A note to the third test sample:
- f([1, 2, 3, 4], 2) = [2, 1, 4, 3]
- f([2, 1, 4, 3], 3) = [1, 4, 2, 3]
- f([1, 4, 2, 3], 4) = [4, 2, 3, 1]
在每次变换的时候,都是取一个长度是t的区间,然后把区间的第一个元素放末尾
那么只要每次把所有这样长度为t的区间的a[kt+1]放到a[kt+t+1]即可。
比如样例的变换:
1 2 3 4 0 0 0
0 2 1 4 3 0 0
0 0 1 4 2 3 0
0 0 0 4 2 3 1
这样每次元素在数组当中的位置都会往后移一位,但是总长度还是n
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int a[];
int main()
{
while (~scanf("%d",&n))
{
for (int i=;i<=n;i++)a[i]=i;
for (int k=;k<=n;k++)
{
int ed=n+k-,rp=n/k+(n%k!=);
for (int t=k-+(rp-)*k;t>=k-;t-=k)
{
swap(a[ed],a[t]);
ed=t;
}
}
for (int i=n;i<=*n-;i++)printf("%d ",a[i]);
puts("");
}
}
cf 287D
cf287D Shifting的更多相关文章
- Level shifting a +/- 2.5V signal to 0 - 5V
Google : Op-Amp Level Shifter Level shifting a +/- 2.5V signal to 0 - 5V I have a front end module t ...
- [LeetCode] Shifting Letters 漂移字母
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...
- [Swift]LeetCode848. 字母移位 | Shifting Letters
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...
- Codeforces 286B Shifting (看题解)
Shifting 感觉这题被智力打击了.. 刚开始我想的是对于每个位置我们可以暴力找出最后的位置在哪里. 因为对于当前位置p, 在进行第x步操作时, 如果p % x == 1 则 p = p + x ...
- XBee Level Shifting
http://www.faludi.com/bwsn/xbee-level-shifting/ The XBee communication (RX/TX) pins definitely opera ...
- LeetCode 848. Shifting Letters
原题链接在这里:https://leetcode.com/problems/shifting-letters/ 题目: We have a string S of lowercase letters, ...
- 848.Shifting Letters——weekly contest 87
848. Shifting Letters 题目链接:https://leetcode.com/problems/shifting-letters/description/ 思路:O(N^2)复杂度过 ...
- 《Learning to warm up cold Item Embeddings for Cold-start Recommendation with Meta Scaling and Shifting Networks》论文阅读
<Learning to warm up cold Item Embeddings for Cold-start Recommendation with Meta Scaling and Shi ...
- 【LeetCode】848. Shifting Letters 解题报告(Python)
[LeetCode]848. Shifting Letters 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
随机推荐
- sping IOC的设计原理和高级特性
1. IOC 是Spring的内核,字面意思是控制反转,并提出了DI依赖注入的概念. 2.Spirng 容器的设计中,一个是实现BeanFactory 接口的简单饿汉容器,另外一个是比较高级的Appl ...
- Hermite 矩阵及其特征刻画
将学习到什么 矩阵 \(A\) 与 \(\dfrac{1}{2}(A+A^T)\) 两者生成相同的二次型,而后面那个矩阵是对称的,这样以来,为了研究实的或者复的二次型,就只需要研究由对称矩阵生成的二次 ...
- Django-C001-快速入门
此文章完成度[100%]留着以后忘记的回顾.多写多练多思考,我会努力写出有意思的demo,如果知识点有错误.误导,欢迎大家在评论处写下你的感想或者纠错. Django Django是一个开放源码的We ...
- 多线程threadvar 变量设定
Delphi管理多线程之线程局部存储:threadvar 尽管多线程能够解决许多问题,但是同时它又给我们带来了很多的问题.其中主要的问题就是:对全局变量或句柄这样的全局资源如何访问?另外,当必须确保一 ...
- javase(10)_多线程基础
一.排队等待 1.下面的这个简单的 Java 程序完成四项不相关的任务.这样的程序有单个控制线程,控制在这四个任务之间线性地移动.此外,因为所需的资源 ― 打印机.磁盘.数据库和显示屏 -- 由于硬件 ...
- [LUOGU] 1002 过河卒
题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为"马拦过 ...
- 文件的软硬链接& 文件编辑vi和vim
目录 文件的软硬链接 1.软链接 2.硬链接 文件编辑vi和vim 须先安装vim命令的软件包yum install -y vim 三种模式: 1.普通模式 2.编辑模式 3.末行模式 文件的软硬链接 ...
- 与LCD_BPP相关的函数
board/freescale/mx6q_sabresd/mx6q_sabresd.c: panel_info.vl_bpix = LCD_BPP; common/lcd.c: off = ...
- Makefile文件中的sed介绍
haoxin$ sed --helpUsage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... -n, --quie ...
- Knockout v3.4.0 中文版教程-4-通过监控数组工作
2.通过监控数组工作 1. 监控数组 如果你想检测或者响应一个对象的改变,你用observables.如果你想检测和响应一个集合的改变,使用observableArray.这个在很多情况下都非常有用, ...