当前位置: 首页 > news >正文

SCUT - 271 - CC 非诚勿扰 - FFT

https://scut.online/p/271
第一次遇到没这么裸的,其实感觉到是卷积但是不知道怎么化。看来以后要多注意下标。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN = 2e6;
const double PI = acos(-1.0);

struct Complex {
    double x, y;
    Complex() {}
    Complex(double x, double y): x(x), y(y) {}
    friend Complex operator+(const Complex &a, const Complex &b) {
        return Complex(a.x + b.x, a.y + b.y);
    }
    friend Complex operator-(const Complex &a, const Complex &b) {
        return Complex(a.x - b.x, a.y - b.y);
    }
    friend Complex operator*(const Complex &a, const Complex &b) {
        return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
    }
} A[MAXN + 5], B[MAXN + 5];

void FFT(Complex a[], int n, int op) {
    for(int i = 1, j = n >> 1; i < n - 1; ++i) {
        if(i < j)
            swap(a[i], a[j]);
        int k = n >> 1;
        while(k <= j) {
            j -= k;
            k >>= 1;
        }
        j += k;
    }
    for(int len = 2; len <= n; len <<= 1) {
        Complex wn(cos(2.0 * PI / len), sin(2.0 * PI / len)*op);
        for(int i = 0; i < n; i += len) {
            Complex w(1.0, 0.0);
            for(int j = i; j < i + (len >> 1); ++j) {
                Complex u = a[j], t = a[j + (len >> 1)] * w ;
                a[j] = u + t, a[j + (len >> 1)] = u - t;
                w = w * wn;
            }
        }
    }
    if(op == -1) {
        for(int i = 0; i < n; ++i)
            a[i].x = (int)(a[i].x / n + 0.5);
    }
}

int pow2(int x) {
    int res = 1;
    while(res < x)
        res <<= 1;
    return res;
}

void convolution(Complex A[], Complex B[], int Asize, int Bsize) {
    int n = pow2(Asize + Bsize - 1);
    for(int i = 0; i < n; ++i) {
        A[i].y = 0.0;
        B[i].y = 0.0;
    }
    for(int i = Asize; i < n; ++i)
        A[i].x = 0;
    for(int i = Bsize; i < n; ++i)
        B[i].x = 0;
    FFT(A, n, 1);
    FFT(B, n, 1);
    for(int i = 0; i < n; ++i)
        A[i] = A[i] * B[i];
    FFT(A, n, -1);
    return;
}

ll ans[MAXN+5],ans0;

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i) {
        scanf("%lf", &A[i].x);
    }
    for(int i = 0; i < n; ++i) {
        scanf("%lf", &B[i].x);
    }
    reverse(B,B+n);
    convolution(A, B, n, n);
    for(int i=0;i<(n<<1);++i){
        ans[i]=(ll)round(A[i].x);
        //printf("%lld\n",ans[i]);
    }
    while(m--){
        int k;
        scanf("%d",&k);
        if(k==0)
            printf("%lld\n",ans[n-1]);
        else
            printf("%lld\n",ans[n-1-k]+ans[n-1+k]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/Yinku/p/11280795.html

相关文章:

  • Silverlight 2.5D RPG游戏“.NET研究”技巧与特效处理:(六)流光追影
  • EdoJs之时间显示与转换
  • Eclipse开发Android应用程序入“.NET研究”门:重装上阵
  • 实现局部或全部页面内容不能选中的效果
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • Android使用自定义AlertDialog(退出提示框)
  • 如何在三个月掌握三年的经验
  • 利用vue-meta管理头部标签
  • Codeforces - 1198D - Rectangle Painting 1 - dp
  • 强化网络互连设备安全配置脚本
  • Codeforces - 1198C - Matching vs Independent Set - 贪心
  • 如何编写实施方案
  • 智能家居-思维的又一次跳跃
  • nginx配置虚拟主机,代理服务器
  • 去除HTML代码得函数
  • [LeetCode] Wiggle Sort
  • 2017-09-12 前端日报
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • github指令
  • hadoop集群管理系统搭建规划说明
  • JavaScript 奇技淫巧
  • JavaScript的使用你知道几种?(上)
  • Java程序员幽默爆笑锦集
  • maya建模与骨骼动画快速实现人工鱼
  • overflow: hidden IE7无效
  • PermissionScope Swift4 兼容问题
  • python大佬养成计划----difflib模块
  • quasar-framework cnodejs社区
  • React组件设计模式(一)
  • tab.js分享及浏览器兼容性问题汇总
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • vue-cli3搭建项目
  • vuex 学习笔记 01
  • 给第三方使用接口的 URL 签名实现
  • 关于Java中分层中遇到的一些问题
  • 技术:超级实用的电脑小技巧
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 前端相关框架总和
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 源码安装memcached和php memcache扩展
  • 找一份好的前端工作,起点很重要
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​Java并发新构件之Exchanger
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • #NOIP 2014# day.2 T2 寻找道路
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (七)Knockout 创建自定义绑定
  • (三)c52学习之旅-点亮LED灯
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (转)如何上传第三方jar包至Maven私服让maven项目可以使用第三方jar包
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • .naturalWidth 和naturalHeight属性,
  • @reference注解_Dubbo配置参考手册之dubbo:reference