hdu5916 构造

来源:互联网 发布:智能化数据分析 编辑:程序博客网 时间:2024/05/22 18:23

题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=5916


题意:给两个数n,k(1<= k < 2*k <=n),然你构造一组数列,使得  为所有排列可能中的第k小个排列


思路:观察发现2*k小于等于n,如果把2*k和k放到最前面,剩余的按互质进行排列,正好满足要求(队友想出来的),很奇妙。

然后 k-1到1,k+1到2k-1,如果2*k<n,最后一个数为2*k+1。 

2*k-1和2*k+1很容易证出互质,这题就解决了


代码如下:

#include <iostream>#include <algorithm>#include <cstring>#include <stdio.h>#include <string>#include <cmath>#include <queue>#include <set>using namespace std;#define   lson          l,m,rt<<1#define   rson          m+1,r,rt<<1|1#define   ll            long long#define   ull           unsigned long long#define   mem(n,v)      memset(n,v,sizeof(n))#define   MAX           105#define   MAXN          2000005#define   PI            3.1415926#define   E             2.718281828459#define   opnin         freopen("text.in.txt","r",stdin)#define   opnout        freopen("text.out.txt","w",stdout)#define   clsin         fclose(stdin)#define   clsout        fclose(stdout)#define   haha1          cout << "haha1"<< endl#define   haha2          cout << "haha2"<< endl#define   haha3          cout << "haha3"<< endlconst int    INF    =   0x3f3f3f3f;const ll     INFF   =   0x3f3f3f3f3f3f3f3f;const double pi     =   3.141592653589793;const double inf    =   1e18;const double eps    =   1e-8;const ll     mod    =   1e9+7;const ull    mx     =   133333331;int main(){    int kace;    cin >> kace;    for(int j=1;j<=kace;j++){        cout << "Case #" << j << ":";        int n,k;        cin >> n >> k;        cout << ' ' << k*2 << ' ' << k;        for(int i=k-1;i>=1;i--)            cout << ' ' << i;        for(int i = k+1;i<2*k;i++)            cout << ' ' << i;        for(int i= 2*k+1;i<=n;i++)            cout << ' ' << i;        cout << endl;    }    return 0;}



0 0
原创粉丝点击