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.
假如输入
应当输出
思路:
此题输入数据如此之大,必然存在某个特定的规律。
用程序打表后。发现 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;
- }
- NEU 1010: NEW RDSP MODE I 数论 每次将交换把偶数位的拖到前面,奇数位的拖到最后面
- NEU 1010: NEW RDSP MODE I 数论 每次将交换把偶数位的拖到前面,奇数位的拖到最后面
- 【面试题】剑指offer14--调整一组数据使得奇数位 位于偶数位的前面
- 将一个整数的二进制表示的奇数位与偶数位交换位置
- 写一个宏可以将一个数字的奇数位和偶数位交换。
- 写一个宏可以将一个数字的奇数位和偶数位交换
- 写一个宏可以将一个数字的奇数位和偶数位交换
- 写一个宏将一个数字的奇数位和偶数位交换
- 写一个宏将一个数的奇数位和偶数位交换
- 【C语言】写一个宏将一个数字的奇数位和偶数位交换
- 写一个宏,将一个数的奇数位和偶数位交换
- 写一个宏可以将一个数字的奇数位和偶数位交换。
- 写一个宏可以将一个数字的奇数位和偶数位交换
- 写一个宏可以将一个数的奇数位和偶数位交换
- 写一个宏可以将一个数字的奇数位和偶数位交换
- 写一个宏可以将一个数字的奇数位和偶数位(二进制)交换。
- C语言--宏将一个数字的奇数位和偶数位交换
- 写一个宏可以将一个数字的奇数位和偶数位交换
- 实现高效的数据库连接池(附带完整代码C#和Java实现)
- coco2d-x环境搭建及编译(一)
- 到底哪些事情属于测试人员的职责?
- StringBuffer和StringBuild
- ACM-知识点
- NEU 1010: NEW RDSP MODE I 数论 每次将交换把偶数位的拖到前面,奇数位的拖到最后面
- 获取外网的IP地址
- Watchcow 欧拉路径 DFS
- latex lstlisting备用代码
- Flow Problem 最大流 最小增广路 SAP算法 从EK算法的753MS降到了46MS
- 设备驱动-----2.Linux内核模块-Makefile解析
- 学习PHP基础条件语句
- Happiness Hotel 数论 佩尔方程
- PHP5.5 将会带来哪些新特征