题解:CF361B Levko and Permutation】的更多相关文章

题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这个排列上的数与它所在位置的序号的最大公约数满足 > 1,并且这些数的个数恰好满足k个,输出这样的一个排列. 先说明什么时候得不到这样的一个排列,就是n = k的情况.因为任何一个数x放在第1个位置的gcd(x, 1) = 1的,所以要得出这样一个排列 k 最大只能为 n-1 .而k最小为0个,这个排…
题意:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这个排列上的数与它所在位置的序号的最大公约数满足 > 1,并且这些数的个数恰好满足k个,输出这样的一个排列. 思路:只需要后k个数与下标一样,前n-k个数逆序输出就复合题意. #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib&g…
http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n,k; int main() { while(scanf("%d%d",&n,&k)!=EOF) { if(n==k) { printf("-1\n"); con…
1.题目描述 2.问题分析 可以使用递归的方法解决,参考了别人的答案才写出来的. 3.代码 vector<string> letterCasePermutation(string S) { vector<string> res ; recursion( S,res, ); return res; } void recursion(string & s , vector<string> &r ,int p){ if( p == s.size() ){ r.…
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3…
[题解]CF359B Permutation 求一个长度为\(2n\)的序列,满足\(\Sigma |a_{2i}-a_{2i-1}|-|\Sigma a_{2i}-a_{2i-1}|=2k\) 这种带绝对值的题目套路就是把绝对值拆开.看看\(n=2\)时候的情况 \(\left[1,2,3,4\right]\) \(|2-1|+|4-3|-|2-1+4-3|=0\) \(swap(1,2) =>\) \(|1-2|+|4-3|-|1-2+4-3|=2\) 也就是交换一组产生\(2\)的贡献,直…
题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible…
[题目描述] Given n and k, return the k-th permutation sequence. Notice:n will be between 1 and 9 inclusive. 给定n和k,求123..n组成的排列中的第k个排列. [注]1 ≤ n ≤ 9 [题目链接] www.lintcode.com/en/problem/permutation-sequence/ [题目解析] 这道题给了我们n还有k,在数列 1,2,3,... , n构建的全排列中,返回第k个…
题面:https://www.cnblogs.com/Juve/articles/11631298.html permutation: 参考:https://www.cnblogs.com/clno1/p/10832579.html 因为原来的数组不好做于是我们想反过来数组,根据交换条件:值相邻且位置差大于等于k,那么在变换后的数组就变成了位置相邻且差值大于等于k.这样的话变换操作变成了,相邻的大于等于k的值临近交换,于是我们注意到因为现在只能临近交换的原因,两个差值小于k的数他们的相对位置不可…
// 昨天打了一场网络赛,表现特别不好,当然题目难度确实影响了发挥,但还是说明自己太菜了,以后还要多多刷题. 2018 CCPC 网络赛 I - Tree and Permutation 简单说明一下题意,给出一个1,2,3...N的排列,显然全部共有N!种排列,每种排列的数字代表树上的一个结点,设Pi是其中第i种排列的相邻数字表示的结点的距离之和,让我们求sum(Pi)(1<=i<=N!). 可以设dis(i, j)为树上任意两点间的最短距离,稍加分析一下容易得到所求答案为 (N-1)! *…