Levko and Permutation CodeForces - 361B 思维 数论
2017-12-13 17:17
411 查看
题意
对一个长度,最大值为n的排列 构造出其恰好有k个GCD(i,a[i])>1 的排列 n<=1e5;
分析
由于每个大于1的数与自己的gcd必然>1 所以构造大于1的gcd可以用i与i去构造 那么还有就是相邻的两个数的gcd必然等于1 任何数与1的gcd都等于1 所以让k个数 从2开始使得a[i] = i 并且后面的数填比自己下标大1的数 多的从前面补上 当k+1<=n的情况都有解
code
#include<bits/stdc++.h> using namespace std; int a[100010]; int main() { int n,k; ios::sync_with_stdio(0); scanf("%d%d",&n,&k); if(k>=n)puts("-1"); else{ if(k!=n-1){ int c,i,tag = 2; for(c=1,i = 2;c<=k;c++,i++){ a[i]=i; } c=i; for(;i<=n;i++){ a[i] = (i+1==n?n:(i+1)%n); //如果下表为n 注意填n不是0 } a[1]=c; for(int i=1;i<=n;i++) printf("%d%c",a[i],i==n?'\n':' '); } else{ for(int i=1;i<=n;i++) printf("%d%c",i,i==n?'\n':' '); } } return 0; }
相关文章推荐
- Levko and Permutation CodeForces - 361B(思维)
- Levko and Table CodeForces - 361A(思维)
- codeforces Karen and Coffee (区间贡献 思维)
- Codeforces Round #394(Div. 2)B. Dasha and friends【思维+暴力】
- codeforces Karen and Coffee (区间贡献 思维)
- codeforces Karen and Coffee (区间贡献 思维)
- Codeforces Round 421 Div.2 D. Mister B and PR Shifts 思维 乱搞
- Codeforces Round #253 (Div. 2) C Borya and Hanabi (思维+bitmask)
- J - Pashmak and Parmida's problem CodeForces - 459D(思维优化+树状数组)
- Codeforces #305 div2 E. Mike and Foam 数论 容斥原理
- codeforces Karen and Coffee (区间贡献 思维)
- Codeforces Round #394 (Div. 2)E. Dasha and Puzzle【思维+Dfs构造】
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)
- codeforces Karen and Coffee (区间贡献 思维)
- Petya and Staircases CodeForces - 362B(思维)
- Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
- codeforces Karen and Coffee (区间贡献 思维)
- Codeforces Round #403 D. Innokenty and a Football League(思维+模拟)
- Codeforces Round #395 (Div. 2) D Timofey and rectangles(思维题)
- 【前缀和 && 思维转换】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)Molly's Chemicals