# CSU 1977: Bit-reversal Permutation

## Description

A fast Fourier transform (FFT) algorithm computes the discrete Fourier transform (DFT) of a sequence, or its inverse (IFFT). Fourier analysis converts a signal from its original domain (often time or space) to a representation in the frequency domain and vice versa. An FFT rapidly computes such transformations by factorizing the DFT matrix into a product of sparse (mostly zero) factors. As a result, it manages to reduce the complexity of computing the DFT from O(n2), which arises if one simply applies the definition of DFT, to O(nlogn), where n is the data size.

——From Wikipedia

During this summer holiday, csuxushu feels so bored to learn FFT. Because FFT is a complicated algorithm, he need to apply a bit-reversal permutation to a sequence first before DFT which is a part of FFT.

In applied mathematics, a bit-reversal permutation is a permutation of a sequence of n items, where n = 2^k is a power of two. It is defined by indexing the elements of the sequence by the numbers from 0 to n ? 1 and then reversing the binary representations of each of these numbers (padded so that each of these binary numbers has length exactly k). Each item is then mapped to the new position given by this reversed value.

Because all fellows in CSU(California State University ) can apply FFT, NTT or even FWT, it is a shame that he even doesn’t know how to take the first step. As one of the best computer programmer in CSU, can you help him?

You may think this problem is too hard to solve. In fact, it is a piece of cake to you. Remember to see the hint :-)

## Input

The first line of the input gives the number of test cases T(T≤10); T test cases follow.Each test case contains a number sequence.
In each case, the first line is a number N(1≤N≤10^5), the number of elements in the following sequence.The second line is the sequence.Its length may not be exactly a power of two, so you can append some zeros to make it the minimal power of two larger than or equal to N.

## Output

For each test case, output the sequence from input in bit-reversal order.

## Sample Input

1
6
21 58 96 12 45 65

## Sample Output

21 45 96 0 58 65 12 0

2017年8月月赛

## 思路

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int t,n;
int a[100005];
int b[100005];
int btc[100005];
int len;
int reserval(int x)
{
int ans = 0;
for(int i = len-1 ; i >= 0 ; i--)
{
if((x & (1 << len-1-i)) == 0)
continue;
ans = ans | (1 << i);
}
return ans;
}
int main()
{
int num = 1;
for(int i = 2 ; i <= 100000 ; i *= 2)
b[i] = num++;
num = 1;
for(int i = 1 ; i <= 100000 ; i++)
if(!b[i]) btc[i] = num;
else btc[i] = num++;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
len = btc[n];
int nn  = 1 << btc[n];
for(int i = 0 ; i < nn ; i++)
printf("%d%c",reserval(i) >= n ? 0:a[reserval(i)]," \n"[i == nn-1]);
}
return 0;
}

## 位运算交换 (FFT模板节选)

// 原地快速bit reversal
for(i = 0, j = 0; i < n; i++) {
if(j > i) swap(a[i], a[j]);
int k = n;
while(j & (k >>= 1)) j &= ~k;
j |= k;
}

• 本文已收录于以下专栏：

## Permutation Descent Counts CSU 1968 （动态规划）

Description Given a positive integer, N, a permutation of order N is a one-to-one (and thus onto) f...
• lwgkzl
• 2017年07月24日 17:39
• 84

## 2014北京邀请赛 Happy Reversal

H. Happy Reversal 64-bit integer IO format: %lld      Java class name: Main Elfness is study...

## 算法百题006：单词逆序输出（Word Reversal）

For each list of words, output a line with each word reversed without changing the order of the word...
• fduan
• 2011年05月11日 10:34
• 3023

## Implementation of card reversal

Implementation of card reversal

## Word Reversal

For each list of words, output aline with each word reversed without changing the order of the words...

## Dixit&amp;Stiglitz_1977_Monopolistic Competition and Optimum Product Diversity

• 2014年07月26日 15:03
• 1.42MB
• 下载

## FDCT 1977 chen

• 2013年09月22日 21:39
• 433KB
• 下载

举报原因： 您举报文章：CSU 1977: Bit-reversal Permutation 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)