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

来源:互联网 发布:老虎扎古淘宝 编辑:程序博客网 时间:2024/05/22 02:05

题目描述:

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代码

[cpp] view plaincopy
  1. /* 
  2.  * @user ipqhjjybj 
  3.  * @Time 
  4.  * @data 20130630 
  5.  */  
  6. #include <cstdio>  
  7. #include <cmath>  
  8. #include <cstdlib>  
  9. #include <ctime>  
  10.   
  11. #include <iostream>  
  12. #include <cmath>  
  13. #include <algorithm>  
  14. #include <numeric>  
  15. #include <utility>  
  16.   
  17. #include <cstring>  
  18. #include <vector>  
  19. #include <stack>  
  20. #include <queue>  
  21. #include <map>  
  22. #include <string>  
  23. using namespace std;  
  24.   
  25. #define inf 0x3f3f3f3f  
  26. #define MAXN 1000  
  27. #define clr(x,k) memset((x),(k),sizeof(x))  
  28. #define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))  
  29. #define cpy(x,k) memcpy((x),(k),sizeof(x))  
  30. #define Base 10000  
  31.   
  32. typedef vector<int> vi;  
  33.   
  34. #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)  
  35.   
  36. #define max(a,b) ((a)>(b)?(a):(b))  
  37. #define min(a,b) ((a)<(b)?(a):(b))  
  38.   
  39. #define ll long long  
  40. ll pow(ll a,ll n,ll MOD){  // 返回 2^n % MOD  
  41.     ll r=1;  
  42.     while(n){  
  43.         if(n&1)  
  44.             r = (a*r)%MOD;  
  45.         a=(a*a)%MOD;  
  46.         n>>=1;  
  47.     }  
  48.     return r;  
  49. }  
  50. int main(){  
  51.     ll n,m,k,zn;  
  52.     int x,i;  
  53.     while(scanf("%lld %lld %d",&n,&m,&x)!=EOF){  
  54.         if(!(n&1)) //n 是偶数  
  55.           n++;  
  56.         zn = pow(2,m,n);  
  57.         printf("%lld",zn);  
  58.         for(k=zn,i=2;i <= x;i++){  
  59.             k+=zn; k %= n;  
  60.             printf(" %lld",k);  
  61.         }  
  62.         printf("\n");  
  63.     }  
  64.     return 0;  
  65. }  

打表代码:

[cpp] view plaincopy
  1. /* 
  2.  * @user ipqhjjybj 
  3.  * @Time 
  4.  * @data 20130630 
  5.  */  
  6. #include <cstdio>  
  7. #include <cmath>  
  8. #include <cstdlib>  
  9. #include <ctime>  
  10.   
  11. #include <iostream>  
  12. #include <cmath>  
  13. #include <algorithm>  
  14. #include <numeric>  
  15. #include <utility>  
  16.   
  17. #include <cstring>  
  18. #include <vector>  
  19. #include <stack>  
  20. #include <queue>  
  21. #include <map>  
  22. #include <string>  
  23. using namespace std;  
  24.   
  25. #define inf 0x3f3f3f3f  
  26. #define MAXN 1000  
  27. #define clr(x,k) memset((x),(k),sizeof(x))  
  28. #define clrn(x,k) memset((x),(k),(n+1)*sizeof(int))  
  29. #define cpy(x,k) memcpy((x),(k),sizeof(x))  
  30. #define Base 10000  
  31.   
  32. typedef vector<int> vi;  
  33.   
  34. #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it)  
  35.   
  36. #define max(a,b) ((a)>(b)?(a):(b))  
  37. #define min(a,b) ((a)<(b)?(a):(b))  
  38.   
  39. int nums[3000][MAXN];  
  40. void changeTo(int *a,int *b,int n){  
  41.     int i,j;  
  42.     for(j = 1,i=2;i<=n;i+=2,j++)  
  43.         a[j]=b[i];  
  44.     for(i=1;i<=n;i+=2,j++)  
  45.         a[j]=b[i];  
  46. }  
  47. int main(){  
  48.     int n,m,x,i,k;  
  49.     while(scanf("%d %d %d",&n,&m,&x)!=EOF){  
  50.         for(i = 1;i <= n;i++)  
  51.             nums[0][i]=i,printf("%d ",i);  
  52.         printf("\n");  
  53.         changeTo(nums[1],nums[0],n);  
  54.         for(i=2;nums[i-1][1]!=nums[0][1];i++){  
  55.             for(int z = 1;z <= n;z++)  
  56.                 printf("%d ",nums[i-1][z]);  
  57.             printf("\n");  
  58.             changeTo(nums[i],nums[i-1],n);  
  59.         }  
  60.         k = m%i;  
  61.         //printf("i=%d k=%d\n",i,k);  
  62.         printf("%d",nums[k][1]);  
  63.         for(i = 2;i <= x;i++)  
  64.             printf(" %d",nums[k][i]);  
  65.         printf("\n");  
  66.     }  
  67.     return 0;  
  68. }