algorithm - how can i apply fft in non multiplication problem?
问题描述
hello i'm trying to apply fft on this problem
i'm tring to optimize O(n^2) operation that found every combination of x[i] and y[j] (0 <= i,j <= N) by fft
but after spending short time on studing fft
but since it is not a multiplication operation,
i wonder whether i can apply fft on these kind of problem
would you give me any way to solve this problem?
i'm sorry for my poor english.
below is wrong answer by me, it spent O(N^2) , just help for understand problem
#include <iostream>
#include <cmath>
#define MAX 262145
using namespace std;
typedef long long LL;
int N;
LL ans[MAX] = {0,};
LL c[19][2][2] = {0,}; // c [2^i][xi][yi]
int x[] = {0,0,1,1};
int y[] = {0,1,0,1};
LL A[MAX] = {0,}; //Set A
LL B[MAX] = {0,}; //Set B
LL power[18] = {0,}; //power[i] == pow(2,i)
LL cal(LL a,LL b){ // find the index number for input pair(a,b)
LL res = 0;
for(int i = 0 ; i < N ; i++){
res += c[i][(a&(1<<i)) == 0 ? 0 : 1][(b&(1<<i)) == 0 ? 0 : 1] * power[i];
}
return res; // the result will point the index of f(x,y)
}
int main() {
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
cin >> N;
string str;
int mv = pow(2,N);
for(int i = 0 ; i < N ; i++){
cin >> str;
power[i] = pow(2,i);
for(LL j = 0 ; j < 4 ; j++){
c[i][x[j]][y[j]] = (int)(str[j] - '0');
}
}
for(int i = 0 ; i < mv ; i++){
cin >> A[i];
}
for(int i = 0 ; i < mv ; i++){
cin >> B[i];
if(B[i] != 0){
for(int j = 0 ; j < mv ; j++){
if(A[j] != 0){
ans[cal(j,i)] += A[j]*B[i]; //
}
}
}
} // in this loop we use O(N^2)
for(int i = 0 ; i < mv ; i++){
cout << ans[i] << " ";
}
return 0;
}
problem: enter image description here
解决方案
推荐阅读
- vba - 工作文档中的 Word VBA ActiveDocument.PageWidth = 9999999
- c# - 如何在 C# 中实现 Twilio Conference HangUpOnStar
- excel - 基于列表的VBA搜索数据
- c# - 在美国文化中显示特定时间和日期的温度的格式
- c# - VS2017 社区 - windows 通用项目不支持页面
- angular - 如何在HttpClient GET调用whit responseType arraybuffer中获得完整响应(包括标头响应)
- angular - angular2 meteor ionic - 不能使用命名空间 Observable 作为类型
- java - 我可以使用解析器分离双打吗?爪哇
- python - 当我不想调用函数时,我无法阻止它们被调用
- wolfram-mathematica - Mathematica - 从表格或数据列表创建波特图