I - Intense Bit Wheel Gym - 101845I
There is a new intense giant wheel in UNAL town, in UNAL town the wheels move in counterclockwise direction, also as this wheel has n cabins, everyone and everything can play. In particular, numbers come to this attraction. However, a complete number doesn’t fit in one cabin, in fact, each cabin have space only for one bit. For this reason a number splits into its binary from and ride the wheel each bit per cabin. As numbers do not want to mess it up, the bits enter into the attraction in the same order the number is formed. Nonetheless, the wheel moves k times (entering and exiting from the attraction don’t count as moves), then when the bits have to get out of the wheel they probably don’t do it in the same order they entered, therefore making a different number.
For example, when the number 13 enter in a intense 8-bit it will look like the left side of the image below, after 5 moves the wheel will look like the right side of the image, in this case the number exiting the wheel will be 161.
You as the chief of the numbers in the UNAL town want to know how the wheel can affect the numbers that ride this attraction.
Input
First line of input contains 2 numbers n (1 ≤ n ≤ 50) and m (1 ≤ m ≤ 1000) - the quantity of cabins in the wheel and the quantity of numbers that ride in the wheel, respectively.
Next m lines of input contains each 2 integers num (0 ≤ num < 2n) and k (1 ≤ k ≤ 1018) - the number that ride the wheel and the quantity of times the wheel move, respectively.
Output
For each number that ride the wheel print the resulting number after leaving the wheel. Output this number in its decimal form.
Example
Input
8 3
1 1
13 5
17 12
Output
2
161
17
Note
Note that all numbers that ride into the wheel have exactly n bits, for example the number 13 in a 8-cabin wheel is not 1101 but 00001101 (see example).
**题意:**两个数n, m;
接下来有m 行
每行输入两个数num , k;
把第一个数n转换为二进制数,移动k次(把第一位数移到最后一位)
num是二进制数的位数(不够的在前面加0)
求出经过移动后的二进制数所对应的十进制数
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, m, u, t, tt, k, j;
int a[55];
long long int x, y, i, sum;
while(~scanf("%d %d", &n, &m))
{
while(m--)
{
stack<int> s;
scanf("%lld %lld", &x, &y);
y=y%n;//(一开始一直超时是因为少了这一步)没有考虑全面
t=0;
while(x)
{
u=x%2;
s.push(u);
t++;
x/=2;
}
k=n-t;
i=0;
while(k)
{
a[++i]=0;
k--;
}
while(!s.empty())
{
tt = s.top();
s.pop();
a[++i]=tt;
}
for(i=1; i<=y; i++)
{
t=a[1];
for(j=1; j<n; j++)
{
a[j]=a[j+1];
}
a[n]=t;
}
sum=0;
for(i=1; i<=n; i++)
{
sum+=a[n-i+1]*pow(2, i-1);
}
printf("%lld\n", sum);
}
}
return 0;
}