SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL

这是一道FFT求高精度的模板题。

参考:https://www.cnblogs.com/RabbitHu/p/FFT.html

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <complex>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;
typedef complex<double> cp;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
// const int mod = 10007;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = *; //注意不能和len1+len2的和刚刚好,因为n是最近的2的阶乘
cp a[maxn], b[maxn], omg[maxn], inv[maxn];
int n=;
void init(){
memset(omg,,sizeof(omg));
memset(inv,,sizeof(inv));
for(int i = ; i < n; i++){
omg[i] = cp(cos( * PI * i / n), sin( * PI * i / n));
inv[i] = conj(omg[i]);
}
}
void fft(cp *a, cp *omg){
int lim = ;
while(( << lim) < n) lim++;
for(int i = ; i < n; i++){
int t = ;
for(int j = ; j < lim; j++)
if((i >> j) & ) t |= ( << (lim - j - ));
if(i < t) swap(a[i], a[t]);
}
for(int l = ; l <= n; l *= ){
int m = l / ;
for(cp *p = a; p != a + n; p += l)
for(int i = ; i < m; i++){
cp t = omg[n / l * i] * p[i + m];
p[i + m] = p[i] - t;
p[i] += t;
}
}
} char s1[maxn],s2[maxn];
int res[maxn];
int main(){
int T;scanf("%d", &T);
while(T--){
n = ;
scanf("%s%s", s1, s2);
int len1 = strlen(s1),len2 = strlen(s2);
while(n < len1 + len2) n <<= ;
memset(a, ,sizeof(a));
memset(b, ,sizeof(b));
for(int i=; i<len1; i++){
a[len1 - i - ].real( s1[i] - '');
}
for(int i=; i<len2; i++){
b[len2 - i - ].real( s2[i] - '');
}
init();
fft(a, omg);
fft(b, omg);
for(int i=; i<n; i++){
a[i] *= b[i];
}
fft(a,inv);
memset(res,,sizeof(res));
for(int i=; i<n; i++){
res[i] += double(a[i].real() / n + 0.5);
res[i+] += res[i] / ;
res[i] = res[i] % ;
}
int s = len1 + len2 - ;
// debug(s);
while(res[s] == && s > )s--;
for(int i = s; i>=; i--){
putchar('' + res[i]);
}
printf("\n");
} return ;
}

FFT

SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法的更多相关文章

  1. SPOJ VFMUL - Very Fast Multiplication (FFT)

    题目链接:VFMUL - Very Fast Multiplication Description Multiply the given numbers. Input n [the number of ...

  2. P1919 FFT加速高精度乘法

    P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...

  3. FFT实现高精度乘法

    你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...

  4. BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法

    Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...

  5. HDU 1402 A * B Problem Plus (FFT求高精度乘法)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)

    题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...

  7. convolution,fft, 加速

    零零星星挖坑几个了,都没填土,实在是欠账太多,闲话少说吧,还是多记录总结一下.今天的主题是围绕convolution和加速 记得之前看过lecun他们组的一篇文章,是fft加速convolution的 ...

  8. 51nod 算法马拉松 34 Problem D 区间求和2 (FFT加速卷积)

    题目链接  51nod 算法马拉松 34  Problem D 在这个题中$2$这个质数比较特殊,所以我们先特判$2$的情况,然后仅考虑大于等于$3$的奇数即可. 首先考虑任意一个点对$(i, j)$ ...

  9. 高精度乘法(FFT)

    学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...

随机推荐

  1. Linux升级GCC

    升级原因 测试需要使用DOClever,下载了最新的node8.11,运行node 时候报错 [root@app_test bin]# node www module.js:681 return pr ...

  2. Scala的常用小技巧

    1."RichString.java".stripSuffix(".java") == "RichString" "http:// ...

  3. Java虚拟机学习笔记(一)之初识

    一:特定 跨平台性.安全性.可移植性. 二:体系机构 Java 体系结构包括四个独立但相关的技术: Java程序设计语言 Java Class文件格式 Java 应用编程接口(API) Java 虚拟 ...

  4. Django是如何防止注入攻击-XSS攻击-CSRF攻击

    注入攻击-XSS攻击-CSRF攻击介绍请访问:https://www.cnblogs.com/hwnzy/p/11219475.html Django防止注入攻击 Django提供一个抽象的模型层来组 ...

  5. cogs 1317. 数列操作C 区间修改 区间查询

    1317. 数列操作C ★★★   输入文件:shuliec.in   输出文件:shuliec.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 假设有一个长度为 n( ...

  6. 【React踩坑记二】react项目实现JS路由跳转

    这里使用的是4.31版本的react-router-dom "react-router-dom": "^4.3.1", 直接使用以下代码即可实现路由跳转 thi ...

  7. Log4j 2 配置

    版本区别 Log4j 2 与 log4j 1.x 最大的区别在于,新版本的 log4j 2 只支持 json 与 xml,不再支持以前的 properties 资源文件 下载 log4j 的jar 包 ...

  8. 基于Starling的mask实现

    作为一个从c++转过来的程序员,flash原生的自定义mask实在是太好用,能方便实现各种效果,比如新手引导的高亮.viewport效果等.可惜starling的显示对象并不支持mask特性,查阅go ...

  9. WebService1

    一.什么是WebService(来源百度百科) Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述. ...

  10. Java——反射:运行时的类信息

    RTTI的使用 如果不知道某个对象的确切类型,RTTI会告诉我们,但是有一个限制:这个类型在编译时必须已知,这样才能使用RTTI识别它,并利用这些信息做一些有用的事情.  2.什么情况下需要反射 假设 ...