SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法

Posted ckxkexing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 << "
";
#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 ‘
‘
 
#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;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
// const int mod = 10007;
const double esp = 1e-8;
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=0;int f=0;char ch=getchar();
    while (ch<0||ch>9) f|=(ch==-),ch=getchar();
    while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x=f?-x:x;
}
 
 
/*-----------------------showtime----------------------*/
            const int maxn = 2*600009;                  //注意不能和len1+len2的和刚刚好,因为n是最近的2的阶乘
            cp a[maxn], b[maxn], omg[maxn], inv[maxn];
            int n=1;
            void init(){
                memset(omg,0,sizeof(omg));
                memset(inv,0,sizeof(inv));
                for(int i = 0; i < n; i++){
                    omg[i] = cp(cos(2 * PI * i / n), sin(2 * PI * i / n));
                    inv[i] = conj(omg[i]);
                }
            }
            void fft(cp *a, cp *omg){
                int lim = 0;
                while((1 << lim) < n) lim++;
                for(int i = 0; i < n; i++){
                    int t = 0;
                    for(int j = 0; j < lim; j++)
                        if((i >> j) & 1) t |= (1 << (lim - j - 1));
                    if(i < t) swap(a[i], a[t]); 
                }
                for(int l = 2; l <= n; l *= 2){
                int m = l / 2;
                for(cp *p = a; p != a + n; p += l)
                    for(int i = 0; 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 = 1;
                scanf("%s%s", s1, s2);
                int len1 = strlen(s1),len2 = strlen(s2);
                while(n < len1 + len2) n <<= 1;
                memset(a, 0 ,sizeof(a));
                memset(b, 0 ,sizeof(b));
                for(int i=0; i<len1; i++){
                    a[len1 - i - 1].real( s1[i] - 0);
                }
                for(int i=0; i<len2; i++){
                    b[len2 - i - 1].real( s2[i] - 0);
                }
                init();
                fft(a, omg);
                fft(b, omg);
                for(int i=0; i<n; i++){
                    a[i] *= b[i];
                }
                fft(a,inv);
                memset(res,0,sizeof(res));
                for(int i=0; i<n; i++){
                    res[i] += double(a[i].real() / n + 0.5);
                    res[i+1] += res[i] / 10;
                    res[i] = res[i] % 10;
                }
                int s = len1 + len2 - 1;
                // debug(s);
                while(res[s] == 0 && s > 0)s--;
                for(int i = s; i>=0; i--){
                    putchar(0 + res[i]);
                }
                printf("
");
            }
            
            return 0;
}
FFT

 

以上是关于SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法的主要内容,如果未能解决你的问题,请参考以下文章

Query on a tree IV SPOJ - QTREE4

C# 分层 三层架构 Very Very Good!

Adidas sneaker outlet very own eloquence along with his very

c语言 very good

语法的几个问题 帮看下 thank!!very

awesome-very-deep-learning