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 ...
随机推荐
- js 分组数组
思路: 1.先将数组按照一定规则排序: 2.再拆分数组到Map中,按Key分类: 3.再从Map中遍历取出要显示的内容: sortBroadList: function (broadcastList) ...
- 在Windows笔记本上调试运行在iOS设备上的前端应用
我在每天工作中需要在不同的移动设备上测试我们开发的前端应用是否正常工作,比如iOS设备和Android设备.我用的工作笔记本电脑又是Lenovo的,安装的是Windows操作系统. 有的时候一个开发好 ...
- Maven项目报错:Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clea
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) ...
- IOS Array 排序方法
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerVal ...
- 三、绘图和可视化之matplotlib
#matplotlib简单绘图之plot import matplotlib.pyplot as plt a=[1,2,3] b=[10,2,30] plt.plot(a)#纵坐标为a的值,横坐标为a ...
- Hermite 矩阵的特征值不等式
将要学习 关于 Hermite 矩阵的特征值不等式. Weyl 定理 以及推论. Weyl 定理 Hermann Weyl 的如下定理是大量不等式的基础,这些不等式要么涉及两个 Hermite 矩 ...
- Jordan 标准型的实例
将学习到什么 练习一下如何把一个矩阵化为 Jordan 标准型. 将矩阵化为 Jordan 标准型需要三步: 第一步 求出矩阵 \(A \in M_n\) 全部的特征值 \(\lambda_1,\ ...
- CS 分解
将学习到什么 CS 分解是分划的酉矩阵在分划的酉等价之下的标准型. 它的证明涉及奇异值分解.QR 分解以及一个简单习题. 一个直观的习题 设 \(\Gamma, L \in M_p\). 假设 \ ...
- Encryption-基础:base64加解密
环境:vc2003 .h /********** This library is free software; you can redistribute it and/or modify it und ...
- HTML5拖放(drag和drog)
拖放(drag和drog)是HTML5的标准的组成部分,也是种常见的特性,意义为抓起一个元素放入到另外的一个位置,在HTML5中任何元素都可以被拖放,前题是要相关进行设置. 1.设置元素为可拖放,也就 ...