NPY and FFT
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1064 Accepted Submission(s): 699
Total Submission(s): 1064 Accepted Submission(s): 699
Problem Description
A boy named NPY is learning FFT algorithm now.In that algorithm,he needs to do an operation called "reverse".
For example,if the given number is 10.Its binary representaion is 1010.After reversing,the binary number will be 0101.And then we should ignore the leading zero.Then the number we get will be 5,whose binary representaion is 101.
NPY is very interested in this operation.For every given number,he want to know what number he will get after reversing.Can you help him?
For example,if the given number is 10.Its binary representaion is 1010.After reversing,the binary number will be 0101.And then we should ignore the leading zero.Then the number we get will be 5,whose binary representaion is 101.
NPY is very interested in this operation.For every given number,he want to know what number he will get after reversing.Can you help him?
Input
The first line contains a integer T — the number of queries (1≤T≤100
).
The next T lines,each contains a integer X(0≤X≤231−1),the given number.
The next T lines,each contains a integer X(0≤X≤231−1),the given number.
Output
For each query,print the reversed number in a separate line.
Sample Input
3 6 8 1
Sample Output
3 1 1
题意:把n转换为二进制再翻转,去掉前导零后的数是多少
思路:用数组保存二进制,处理前导零即可
#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <functional>
#include <ctime>
#define PI acos(-1)
#define eps 1e-8
#define inf 0x3f3f3f3f
#define debug(x) cout<<"---"<<x<<"---"<<endl
typedef long long ll;
using namespace std;
long long quickpow(long long a, long long b)
{
if (b < 0)
{
return 0;
}
long long ret = 1;
for (; b; b >>= 1, a = (a * a))
if (b & 1)
{
ret = (ret * a);
}
return ret;
}
int a[55];
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
memset(a, 0, sizeof(a));
int x;
cin >> x;
int cnt = 0, flag;
while (x)
{
a[++cnt] = x % 2;
x /= 2;
}
for (int i = 1; i <= cnt; i++)
{
if (a[i] == 1)
{
flag = i;
break;
}
}
ll ans = 0;
for (int i = flag; i <= cnt; i++)
{
ans += quickpow(2, cnt - i) * a[i];
}
cout << ans << endl;
}
return 0;
}