G - NEW RDSP MODE I
Time limit 1000 ms
Memory limit 131072 kB
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.
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.
Sample Input
5 1 2 5 2 2
Sample Output
2 4 4 3
Hint
In case two: N=5,M=2,X=2,the initial sequence One is 1,2,3,4,5.After the first operation, the sequence One is 2,4,1,3,5. After the second operation, the sequence One is 4,3,2,1,5.So,output 4 3.
题意大概为给定一个1—n的递增数列,经过m次变换,每次变换将数列中奇数位置的数取出放在数列末尾,问变换后数列
的前x个数是多少。
这道题时间限制为一秒,暴力肯定超时,所以我们要找其中的规律,n和m都很大,x却很小,所以我们可以以x为突破口。
假定数i在数列中的位置为s,经过一次变换后数i的位置S:n为偶数时s=2*S (S<=n/2),s=2*S-n-1 (n/2<S<=n);n为奇数时s=
2*S (S<=n/2),s=2*S-n (n/2<S<=n)。通个计算可以计算出变换后数列中前x个数在变化前的位置,变化前的位置就对应其值。
#include<iostream>
#include<cstdio>
using namespace std;
int location(int n,int m,int s)
{
int cnt=0,flag=true,S=s;
if(n%2)
while(flag){
// printf("!\n");
if(s<=n/2) s*=2;
else s=2*s-n;
if(S==s) flag=false;
cnt++;
}
else
while(flag){
if(s<=n/2) s*=2;
else s=2*s-n-1;
if(S==s) flag=false;
cnt++;
}
return cnt;
}
int gbs(int a,int b)
{
int c,A=a,B=b;
while(b!=0){
c=a%b;
a=b;
b=c;
}
return A*B/a;
}
int findnum(int n,int m,int s)
{
if(n%2)
while(m--){
if(s<=n/2) s*=2;
else s=2*s-n;
}
else
while(m--){
if(s<=n/2) s*=2;
else s=2*s-n-1;
}
return s;
}
int main()
{
int n,m,x;
while(scanf("%d %d %d",&n,&m,&x)!=-1){
int k=1;
for(int i=1;i<=x;i++)
k=gbs(k,location(n,m,i));//计算周期
m%=k;
for(int i=1;i<=x;i++){
printf("%d",findnum(n,m,i));
if(i!=x)
printf(" ");
}
printf("\n");
}
return 0;
}