NEU 1010: NEW RDSP MODE I 数论 每次将交换把偶数位的拖到前面,奇数位的拖到最后面

73 篇文章 21 订阅
6 篇文章 0 订阅

题目描述:

Little A has became fascinated with the game Dota recently, but he is not a good player. In all the modes, the rdsp Mode is popular on online, in this mode, little A always loses games if he gets strange heroes, because, the heroes are distributed randomly.

Little A wants to win the game, so he cracks the code of the rdsp mode with his talent on programming. The following description is about the rdsp mode:

There are N heroes in the game, and they all have a unique number between 1 and N. At the beginning of game, all heroes will be sorted by the number in ascending order. So, all heroes form a sequence One.

These heroes will be operated by the following stages M times:

1.Get out the heroes in odd position of sequence One to form a new sequence Two;

2.Let the remaining heroes in even position to form a new sequence Three;

3.Add the sequence Two to the back of sequence Three to form a new sequence One.

After M times' operation, the X heroes in the front of new sequence One will be chosen to be Little A's heroes. The problem for you is to tell little A the numbers of his heroes.


题目大意: 每次执行交换,将数位上的偶数位的移到前面,奇数位的拖到末尾,执行交换M次,N表示1-N个数,最后输出前1->X个数


如 1 2 3 4 5  操作一次-> 2 4 1 3 5  操作一次-> 4 3 2 1 5 操作一次->3 1 4 2 5 操作一次->1 2 3 4 5

Input:

There are several test cases.

Each case contains three integers N (1<=N<1,000,000), M (1<=M<100,000,000), X(1<=X<=20).

Proceed to the end of file.

Output:

For each test case, output X integers indicate the number of heroes. There is a space between two numbers. The output of one test case occupied exactly one line.

假如输入

5 1 2
5 2 2

应当输出

2 4
4 3


思路:

此题输入数据如此之大,必然存在某个特定的规律。

用程序打表后。发现 2*k 跟 2*k+1 数字的前2*k个字符变化完全相同,当为2*k+1数列,第2*k+1个数永远为2*k+1
之后仔细观察n=2*k+1这种情况。发现如下

第一个数字是  1*2^(m)%n ,第2个数字是2*2^(m)%n,第3个是3*2^(m)%n,以此类推。所以此题即可解出

又有当  1*2^(m)%n = num时,由(a+a)%n = ((a)%n+(a)%n)%n。减少了运算次数

计算2^n时。可采用乘法加速幂来进行快速运算。

此题跟约瑟夫回环问题有点点相似之处


附上代码: AC代码

/*
 * @user ipqhjjybj
 * @Time
 * @data 20130630
 */
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>

#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define inf 0x3f3f3f3f
#define MAXN 1000
#define clr(x,k) memset((x),(k),sizeof(x))
#define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000

typedef vector<int> vi;

#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

#define ll long long
ll pow(ll a,ll n,ll MOD){  // 返回 2^n % MOD
    ll r=1;
    while(n){
        if(n&1)
            r = (a*r)%MOD;
        a=(a*a)%MOD;
        n>>=1;
    }
    return r;
}
int main(){
    ll n,m,k,zn;
    int x,i;
    while(scanf("%lld %lld %d",&n,&m,&x)!=EOF){
        if(!(n&1)) //n 是偶数
          n++;
        zn = pow(2,m,n);
        printf("%lld",zn);
        for(k=zn,i=2;i <= x;i++){
            k+=zn; k %= n;
            printf(" %lld",k);
        }
        printf("\n");
    }
    return 0;
}

打表代码:

/*
 * @user ipqhjjybj
 * @Time
 * @data 20130630
 */
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <utility>

#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define inf 0x3f3f3f3f
#define MAXN 1000
#define clr(x,k) memset((x),(k),sizeof(x))
#define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))
#define cpy(x,k) memcpy((x),(k),sizeof(x))
#define Base 10000

typedef vector<int> vi;

#define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

int nums[3000][MAXN];
void changeTo(int *a,int *b,int n){
    int i,j;
    for(j = 1,i=2;i<=n;i+=2,j++)
        a[j]=b[i];
    for(i=1;i<=n;i+=2,j++)
        a[j]=b[i];
}
int main(){
    int n,m,x,i,k;
    while(scanf("%d %d %d",&n,&m,&x)!=EOF){
        for(i = 1;i <= n;i++)
            nums[0][i]=i,printf("%d ",i);
        printf("\n");
        changeTo(nums[1],nums[0],n);
        for(i=2;nums[i-1][1]!=nums[0][1];i++){
            for(int z = 1;z <= n;z++)
                printf("%d ",nums[i-1][z]);
            printf("\n");
            changeTo(nums[i],nums[i-1],n);
        }
        k = m%i;
        //printf("i=%d k=%d\n",i,k);
        printf("%d",nums[k][1]);
        for(i = 2;i <= x;i++)
            printf(" %d",nums[k][i]);
        printf("\n");
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值