Title Description
You have a stack of numbered 1 to n card.
You have an operation, these cards can be rearranged, as follows:
1. the card into a front half and a second half.
2. sequentially depicting a card from the latter half, the front half portion, into a new sequence.
For example, the card sequence (1,2,3,4,5,6) for the result of the operation (4,1,5,2,6,3).
Now you have an initially (1,2,3, ⋯, n) of the sequence of cards, you need to determine the reference card on the x-th position after operation m times.
You have an operation, these cards can be rearranged, as follows:
1. the card into a front half and a second half.
2. sequentially depicting a card from the latter half, the front half portion, into a new sequence.
For example, the card sequence (1,2,3,4,5,6) for the result of the operation (4,1,5,2,6,3).
Now you have an initially (1,2,3, ⋯, n) of the sequence of cards, you need to determine the reference card on the x-th position after operation m times.
Entry
The first line contains three non-negative integer n, m, x.
Export
Output line a number that represents the answer.
Sample input
6 2 3
Sample Output
6
prompt
For 60% of the data, m≤10 . 7 .
To 100% of the data, 0 ≦ n, m, x≤10 . 9 .
Graded data, to ensure that n is even.
answer:
Suppose the original position x (also values), the current position p;
Because once transformed, or changed positions 2x, or becomes 2x- (n + 1), it is the equation (2 ^ m) * x + (n + 1) * y = p;
Since we only required the x, so the equation can be written as ((2 ^ m)% (n + 1)) * x + (n + 1) * y = p;
Simple to understand what extend_gcd during the execution of the function you can get the above conclusions.
AC Code:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll qpow(ll a,ll b,ll mod) 5 { 6 ll res=1; 7 while(b){ 8 if(b&1) res=res*a%mod; 9 a=a*a%mod; 10 b>>=1; 11 } 12 return res; 13 } 14 ll extend_gcd(ll a,ll b,ll &x,ll &y) 15 { 16 if(b==0){ 17 x=1;y=0; 18 return a; 19 } 20 ll r=extend_gcd(b,a%b,y,x); 21 y-=x*(a/b); 22 return r; 23 } 24 int main() 25 { 26 ll n,m,p,a,b,x,y; 27 scanf("%lld %lld %lld",&n,&m,&p); 28 a=qpow(2,m,n+1);b=n+1; 29 ll gcd=extend_gcd(a,b,x,y); 30 b/=gcd;p/=gcd; 31 x=x%b*p%b; 32 if(x<0) x+=b; 33 printf("%lld\n",x); 34 return 0; 35 }