CCPC2016长春F (hdu 5916 Harmonic Value Description)

构造一个n个数字的排列,使其作为 sigma( gcd( a[i] , a[i+1] ) ) 中第k小的排列。

因为题目给的 k*2 <= n ,所以可以知道只会询问最小到第 n/2 小,易得最多移动一个数字即可得到答案。

假设原始排列为1 2 3 ... n,如果询问最小直接输出排列,否则询问x且x是偶数就把x移动到2*n与2*n-1之间。

如果x是奇数就将x与 2*n-1或者2*n+1交换。因为有可能2*n-1和2*n+1与x-1或者x+1不互质。

//#define test
#include<bits/stdc++.h>
using namespace std;
const int Nmax=1e6+7;
typedef long long ll;
int a[Nmax];
int n,m;
int main()
{
#ifdef test
#endif
    int t;
    //freopen("f.in","r",stdin);
    scanf("%d",&t);

    for(int cases=1; cases<=t; cases++)
    {
        printf("Case #%d:",cases);

        scanf("%d%d",&n,&m);

        //printf("n:%d m:%d\n",n,m);
        for(int i=1; i<=n; i++)
            a[i]=i;

        if(m&1)
        {
            int l=m-1,r=m+1;

            if((m!=l && __gcd(2*m-1,l) !=1) || (m != r && __gcd(2*m-1,r)!=1))
                swap(a[m],a[2*m+1]);
            else
                swap(a[m],a[2*m-1]);
        }
        else
        {

            a[2*m]=m;

            for(int i=2*m+1; i<=n+1; i++)
                a[i]=i-1;

            for(int i=m; i<=n; i++)
                a[i]=a[i+1];
        }

        for(int i=1; i<=n; i++)
            printf(" %d",a[i]);

        printf("\n");

    }

    return 0;
}

原文地址:https://www.cnblogs.com/LMissher/p/9673971.html

时间: 2024-10-26 14:19:50

CCPC2016长春F (hdu 5916 Harmonic Value Description)的相关文章

HDU 5916 Harmonic Value Description 【构造】(2016中国大学生程序设计竞赛(长春))

Harmonic Value Description Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description The harmonic value of the permutation p1,p2,?pn is ∑i=1

CCPC2016长春D (hdu 5914 Triangle)

water,给一个数n,问从1...n的数字里最少删去多少数字让剩下的数字必不可能组成三角形. 保留的数字必定是1.2.3.5.8.13.21.....这样的数字. //#define test #include<bits/stdc++.h> using namespace std; const int Nmax=1e6+7; typedef long long ll; int _,n; int f[10],cas=1; int main() { #ifdef test #endif // f

CCPC2016长春B (hdu 5912 Fraction)

water //#define test #include<bits/stdc++.h> using namespace std; const int Nmax=1e6+7; typedef long long ll; struct LL { ll a,b; LL(){ } LL(ll aa,ll bb){ a=aa,b=bb; ll g=__gcd(a,b); a/=g; b/=g; } friend LL operator + (const LL &a,const LL &

Harmonic Value Description

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 786    Accepted Submission(s): 456Special Judge Problem Description The harmonic value of the permutation p1,p2,?pn is ∑i=1n−1gcd(pi.pi+1) Mr. Fr

2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 447    Accepted Submission(s): 201 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort,

F -- HDU 1022

Train Problem I Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student

hdu 5916

题意:sigma(gcd(pi,pi+1)),输出一个和严格第k小的p序列 分析:相邻的两个数gcd为1,并且题目给出提示2*k<=n,gcd(k,2*k)=k,这只要保证剩下的相邻都是1,就可以保证这个序列是符合条件的 如何来保证呢,2*k 和2*k+1 2*k -1必然两两gcd=1,问题在于怎么处理k-1 k+1之间的矛盾 ,那么我先利用第一个条件,把k后面的都放进去,然后放1-  k-1,1必然与任意数gcd为1 #include<bits/stdc++.h> using nam

hdu 3572 Task Schedule(最大流)

hdu 3572 Task Schedule Description Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th tas

hdu 3338 Kakuro Extension(最大流)

hdu 3338 Kakuro Extension Description If you solved problem like this, forget it.Because you need to use a completely different algorithm to solve the following one. Kakuro puzzle is played on a grid of "black" and "white" cells. Apart