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 ...
随机推荐
- nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati
root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx --add-mod ...
- 查看进程lsof
查看8000端口 lsof -i :8000 杀死进程 pkill -ns <pid>
- js设置元素float的问题
用js设置一个元素的float样式 div.style.float = 'left'; 这句话在谷歌浏览器或许没问题,但是在IE,火狐下会无效 正确写法是 div.style.styleFloat = ...
- 获得stixel的gt数据
这是论文中的作者博客地址https://sites.google.com/site/danmlevi/ 这是作者现在的博客地址https://sites.google.com/view/danlevi ...
- w3 parse a url
最新链接:https://www.w3.org/TR/html53/ 2.6 URLs — HTML5 li, dd li { margin: 1em 0; } dt, dfn { font-wei ...
- 电脑上文件的后缀名被隐藏,把一个文本文件改成.bat时,默认打开的还是文本。
1.打开文件夹,选择组织,点击“文件夹和搜索选项”,如图: 2.选择“查看”,找到“隐藏已知文件类型的扩展名”,不要勾选这一项,如图: 3.点击“确定”或者“应用”
- ubuntu 18.* 重启网卡
systemctl UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting A ...
- jquery 获得某一组name的id并合并
var attachmentids = $("input[name='attachmentid']").map(function(){return $(this).val()}). ...
- PWA介绍
https://codelabs.developers.google.com/codelabs/your-first-pwapp/#0 PWA是一些技术的集合.用于消除web与其他客户端之间的差距,最 ...
- 在centos7中为php7安装redis扩展
在此之前一直是用php5.6,安装redis也没遇到啥问题,嗖嗖的就安装上了 更新php版本到php7后,编译的时候报错 include <ext/standard/php_smart_str. ...