SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
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加速高精度乘法的更多相关文章
- SPOJ VFMUL - Very Fast Multiplication (FFT)
题目链接:VFMUL - Very Fast Multiplication Description Multiply the given numbers. Input n [the number of ...
- P1919 FFT加速高精度乘法
P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...
- FFT实现高精度乘法
你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...
- BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法
Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...
- 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 ...
- HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...
- convolution,fft, 加速
零零星星挖坑几个了,都没填土,实在是欠账太多,闲话少说吧,还是多记录总结一下.今天的主题是围绕convolution和加速 记得之前看过lecun他们组的一篇文章,是fft加速convolution的 ...
- 51nod 算法马拉松 34 Problem D 区间求和2 (FFT加速卷积)
题目链接 51nod 算法马拉松 34 Problem D 在这个题中$2$这个质数比较特殊,所以我们先特判$2$的情况,然后仅考虑大于等于$3$的奇数即可. 首先考虑任意一个点对$(i, j)$ ...
- 高精度乘法(FFT)
学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...
随机推荐
- Thinkphp 5.1.7 parseData缺陷导致insert/update注入 分析
目录 环境搭建 分析 参考 环境搭建 $ composer create-project topthink/think thinkphp-5.1.7 修改composer.json 5.1.* =&g ...
- win10去除快捷方式小箭头
切忌删除注册表项: HKEY_CLASSES_ROOT -> lnkfile -> IsShortcut 这个方法以前是可以的,但是在2018年之后更新的系统就会出现任务栏图标打不开的情况 ...
- MySql数据库操作命令
数据库 database 命令 ********************************* DATABASE 创建数据库 CREATE DATABASE haodlDB DEFAULT CHA ...
- bucket list 函数解析
cls_bucket_list 函数 librados::IoCtx index_ctx; // key - oid (for different shards if there is any) ...
- Echarts图表插件(4.x版本)使用(二、带分类筛选的多个图表/实例化多个ECharts,以关系图/force为例)
导读 如果想在一个页面里实例化带分类筛选的多个Echarts该怎么做呢? 曾探讨了带分类选择的关系图显示为自定义图片的需求实现,传送门ECharts图表插件(4.x版本)使用(一.关系图force节点 ...
- 2.PHP利用PDO连接方式连接mysql数据库
代码如下 <?php$serverName = "这里填IP地址";$dbName = "这里填数据库名";$userName = "这里填用户 ...
- Duilib的圆环形 进度条 实现(网易云信版本)
/** @file CircleProgress.h* @brief 圆环型进度条控件,圆环中间可以有文本(如85%)* @copyright (c) 2019-2022, NetEase Inc. ...
- html5标签整理
html元素 基础标签 <!DOCTYPE> 定义文档类型(e.g <!DOCTYPE html>) <html>定义一个HTML文档</html> ...
- mysql5.7绿色版配置以及找不到 mysql服务问题解决
一.下载软件 1. 进入mysql官网,登陆自己的Oracle账号(没有账号的自己注册一个),下载Mysql-5.7.17,下载地址:http://dev.mysql.com/downloads/my ...
- 部分APP无法代理抓包的原因及解决方法
引言 HTTP应用层的抓包已经成为日常工作测试与调试中的重要一环,最近接触新项目突然之间发现之前的抓包手段都不好使了,顿时模块与模块之间的前端与服务之间的交互都变成了不可见,整个人都好像被蒙住了眼睛. ...