Problem Description
The harmonic value of the permutation
p1,p2,⋯pn
is
Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].
∑i=1n−1gcd(pi.pi+1)
Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].
Input
The first line contains only one integer T (
1≤T≤100
), which indicates the number of test cases.
For each test case, there is only one line describing the given integers n and k ( 1≤2k≤n≤10000 ).
For each test case, there is only one line describing the given integers n and k ( 1≤2k≤n≤10000 ).
Output
For each test case, output one line “Case #x:
p1 p2 ⋯ pn
”, where x is the case number (starting from 1) and
p1 p2 ⋯ pn
is the answer.
Sample Input
2 4 1 4 2
Sample Output
Case #1: 4 1 3 2 Case #2: 2 4 1 3Problem DescriptionThe harmonic value of the permutation p1,p2,⋯pn is∑i=1n−1gcd(pi.pi+1)Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].
InputThe first line contains only one integer T ( 1≤T≤100 ), which indicates the number of test cases. For each test case, there is only one line describing the given integers n and k ( 1≤2k≤n≤10000 ).
OutputFor each test case, output one line “Case #x: p1 p2 ⋯ pn ”, where x is the case number (starting from 1) and p1 p2 ⋯ pn is the answer.
Sample Input2 4 1 4 2
Sample OutputCase #1: 4 1 3 2 Case #2: 2 4 1 3
思路:如果k等于1直接输出1到n否则从2开始依次输出k个相邻gcd为2的偶数 2 4 6 8。。
比如:6 1为1 2 3 4 5 66 2为2 4 1 3 5 6 依次类推。因为k<=n/2,所以无需考虑其他。#include<stdio.h> #include<string.h> const int maxm=10005; int flag[maxm]={0},p[maxm]={0},f[maxm]; int gcd(int x,int y) { if(y==0) return x; else return(gcd(y,x%y)); } int main() { int n,i,j,k,sum,t,len=0,ans,rev=1; flag[1]=1; scanf("%d",&t); while(t--) { j=0; memset(flag,0,sizeof(flag)); scanf("%d%d",&n,&k); printf("Case #%d: ",rev++); if(k>1) { printf("2"); flag[2]=1; sum=4; while(k-1) { if(sum<=n) { printf(" %d",sum); flag[sum]=1; sum+=2; f[++j]=sum; k--; } } for(i=1;i<=n;i++) { if(!flag[i]) printf(" %d",i),f[++j]=i; } printf("\n"); } else { for(i=1;i<=n;i++) { if(i==n) printf("%d\n",i); else printf("%d ",i); f[i]=i; } } } return 0; }
Problem Description
The harmonic value of the permutation
p1,p2,⋯pn
is
Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].
∑i=1n−1gcd(pi.pi+1)
Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].
Input
The first line contains only one integer T (
1≤T≤100
), which indicates the number of test cases.
For each test case, there is only one line describing the given integers n and k ( 1≤2k≤n≤10000 ).
For each test case, there is only one line describing the given integers n and k ( 1≤2k≤n≤10000 ).
Output
For each test case, output one line “Case #x:
p1 p2 ⋯ pn
”, where x is the case number (starting from 1) and
p1 p2 ⋯ pn
is the answer.
Sample Input
2 4 1 4 2
Sample Output
Case #1: 4 1 3 2 Case #2: 2 4 1 3