1977:Bit-reversal Permutation
来源:互联网 发布:gal 知乎 编辑:程序博客网 时间:2024/06/16 04:19
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
Hint
中文提示:可以看到,我们最终处理的系数从左至右的编号的二进制形式分别为000,100,010,110,001,101,011,111,若将其二进制反序,可得000,001,010,011,100,101,110,111,这些反序的二进制编码是从小到大排列的。也就是说,我们可以按照每个下标的二进制编码来确定处理系数的顺序。这种方法就称为位逆序置换(Bit-reversal permutation)。
评价:这个题要注意数组开的大小,我开数据量的3倍过了,2倍没过,输入scanf,用cin超时了。
AC代码
#include<iostream>#include<cstdio>#include<cmath>#include<string.h>using namespace std;long long a[300100];int b[20];int c[300010];int N;int Reverse(int x){ int i=0; for(;x;x>>=1) b[i++]=x&1; int k=i; int y=0; for(int j=0;j<k;j++) y+=b[j]*pow(2,(--i)); return y;}int main(){ int T; cin>>T; while(T--) { cin>>N; memset(a,0,sizeof(a)); for(int i=0;i<N;i++) scanf("%lld",&a[i]); int k=1; while((1<<k)<N) k++; N=1<<k; for(int i=N/2;i<N;i++) c[i]=Reverse(i); int m=N/2; for(int i=0;i<m;i++) c[i]=c[i+m]-1; for(int i=0;i<N-1;i++) printf("%lld ",a[c[i]]); printf("%lld\n",a[c[N-1]]); } return 0;}
- 1977:Bit-reversal Permutation
- 1977: Bit-reversal Permutation(递归)
- CSU 1977: Bit-reversal Permutation
- Bit-reversal permutation
- Bit Reversal and Permutation
- COJ1977-Bit-reversal Permutation
- CSU1977: Bit-reversal Permutation
- CSU1977-Bit-reversal Permutation-模拟
- EPI 5.3 BIT REVERSAL
- Word Reversal
- ZOJ_1151_Word Reversal
- Word Reversal
- Word Reversal
- Word Reversal
- Word Reversal
- Word Reversal
- Happy Reversal
- HUST1339-Reversal
- C语言入门:输出学生成绩(要求动态分配)
- 前后端分离,动态添加文本框以及回填数据
- KMP算法next数组的计算
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解 (2)
- 实时操作系统-与QNX比较-qnx系统优势-qnx性能分析-qnx系统性能分析
- 1977:Bit-reversal Permutation
- Hbase无法启动
- 存储持续性、作用域和链接性
- 算法之选择排序
- 编程挑战1——求逆置回文最少转换次数
- TPshop学习(9)银联支付
- leetcode Path Sum II
- [JS] jQuery与table
- 2018校招网易笔试——最长01交错子串