题目描述
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.
输入
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.
输出
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
提示
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,n个数,操作m次之后输出前k个数,1~N个数字组成数组A,将数组中奇数位组成一个数组B,偶数位组成一个数组C,然后将B连接到C后面,求执行M次,输出最后的前X位数
思路:
分析:这道题可以理解成求一个数在M次变换之后最后的位置,那么首先就从两次变换的关系开始推导
假设本次的位置为X,分为偶数和奇数两种情况:
如果X为偶数,那么X下一次的坐标就会是X'=X/2,可以理解成前面有一半的奇数被拿走了,变换一下 X=2*X'
如果X为奇数,那么X下一次的坐标就会是X'=N/2+X/2,可以理解成前面有N/2个偶数,然后还有X/2个奇数排在X前面,变换一下 X=2*X' - N
可以发现,无论是奇数还是偶数,都是 X=2*X 之后对N取模,那么经过M次变换可以看成2的M次幂,这里写一个快速幂就行了。
还要注意,当N为偶数的时候要+1,因为N为偶数的时候,取模后下标可能为0,当N为奇数时,第N位数在变换中位置是不变的,所以情况和N-1一样。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll pow(ll a,ll b,ll mod)
{
ll res=1;
for(;b>0;b/=2)
{
if(b%2==1)
res=(res*a)%mod;
a=(a*a)%mod;
}
return res;
}
int main()
{
ll n,m,x;
while(scanf("%lld%lld%lld",&n,&m,&x)!=EOF)
{
if(n%2==0)
n++;
ll sum=pow(2,m,n);
ll ans=sum;
printf("%lld",sum);
for(int i=2;i<=x;i++)
{
ans+=sum;
ans%=n;
printf(" %lld",ans);
}
printf("\n");
}
return 0;
}