【HDU - 5916】Harmonic Value Description (构造,思维,SJ题)
題干:
The harmonic value of the permutation?p1,p2,?pnp1,p2,?pn?is?
∑i=1n?1gcd(pi.pi+1)∑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≤1001≤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≤100001≤2k≤n≤10000).
Output
For each test case, output one line “Case #x:?p1?p2???pnp1?p2???pn”, where x is the case number (starting from 1) and?p1?p2???pnp1?p2???pn?is the answer.
Sample Input
2 4 1 4 2Sample Output
Case #1: 4 1 3 2 Case #2: 2 4 1 3解題報告:
? ? ? 構造。這題顯然不會有統一的規律讓你去找或者暴力求解。所以我們要考慮是不是個Special Judge的題,也就是,我們只需要給出其中的一個答案就可以了。
? ? ? 顯然我們不難發現,最小的∑值,恰好是n-1,也就是說,按照升序排序,每兩個數的gcd都=1,所以他讓求第k小的,也就是我們只需要讓其中一對兒gcd=1的變成=k的就可以了,題目給了一個隱藏的暗示:2*k<=n,所以我們就把2*k 和 k?放在前兩個的位置上,其余的數依舊保證“相鄰的數的gcd=1”即可。于是想到先繼續從k+1開始輸出到2*k-1,然后再輸出1~k-1? 。這樣剛好符合題意。
AC代碼:
#include<bits/stdc++.h> using namespace std;int main(){int t,n,cas=1,k;cin>>t;while(t--){cin>>n>>k;printf("Case #%d: %d %d",cas++,2*k,k);for(int i=k+1;i<=n;i++){if(i==2*k)continue;printf(" %d",i);}for(int i=1;i<=k-1;i++)printf(" %d",i);puts("");}return 0; }?
總結
以上是生活随笔為你收集整理的【HDU - 5916】Harmonic Value Description (构造,思维,SJ题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黑猫投诉:尚德教育虚假宣传,霸王条款,小
- 下一篇: 【POJ - 2406】Power St