标签:
题意:大数乘法
思路:FFT模板
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | /* ******************************************************************************** */#include <iostream>                                                                 //#include <cstdio>                                                                   //#include <cmath>                                                                    //#include <cstdlib>                                                                  //#include <cstring>                                                                  //#include <vector>                                                                   //#include <ctime>                                                                    //#include <deque>                                                                    //#include <queue>                                                                    //#include <algorithm>                                                                //#include <map>                                                                      //#include <cmath>                                                                    //usingnamespacestd;                                                                //                                                                                    //#define pb push_back                                                                //#define mp make_pair                                                                //#define X first                                                                     //#define Y second                                                                    //#define all(a) (a).begin(), (a).end()                                               //#define foreach(a, i) for (typeof(a.begin()) i = a.begin(); i != a.end(); ++ i)     //#define foreach(a, n, i) for(typeof(*a) *i = a; i < a + n; i ++)                    //#define fillchar(a, x) memset(a, x, sizeof(a))                                      //                                                                                    //voidRI(vector<int>&a,intn){a.resize(n);for(inti=0;i<n;i++)scanf("%d",&a[i]);}    //voidRI(){}voidRI(int&X){scanf("%d",&X);}template<typename...R>                    //voidRI(int&f,R&...r){RI(f);RI(r...);}voidRI(int*p,int*q){intd=p<q?1:-1;          //while(p!=q){scanf("%d",p);p+=d;}}voidprint(){cout<<endl;}template<typenameT>      //voidprint(constT t){cout<<t<<endl;}template<typenameF,typename...R>              //voidprint(constF f,constR...r){cout<<f<<", ";print(r...);}template<typenameT>   //voidprint(T*p, T*q){intd=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //                                                                                    //typedefpair<int, int> pii;                                                         //typedeflonglongll;                                                               //typedefunsigned longlongull;                                                     //                                                                                    //template<typenameT>boolumax(T&a, constT&b){returnb>a?false:(a=b,true);}         //template<typenameT>boolumin(T&a, constT&b){returnb<a?false:(a=b,true);}         //template<typenameT>                                                                //voidV2A(T a[],constvector<T>&b){for(inti=0;i<b.size();i++)a[i]=b[i];}            //template<typenameT>                                                                //voidA2V(vector<T>&a,constT b[]){for(inti=0;i<a.size();i++)a[i]=b[i];}            //                                                                                    //constdoublePI = acos(-1);                                                         //                                                                                    ///* -------------------------------------------------------------------------------- */namespaceFFT {    conststaticintmaxn = 5e4 + 7;    #define L(x) (1 << (x))    doubleax[maxn << 2], ay[maxn << 2], bx[maxn << 2], by[maxn << 2];//需要四倍空间    intrevv(intx, intbits) {        intret = 0;        for(inti = 0; i < bits; i++) {            ret <<= 1;            ret |= x & 1;            x >>= 1;        }        returnret;    }    voidfft(double* a, double* b, intn, boolrev) {        intbits = 0;        while(1 << bits < n) ++bits;        for(inti = 0; i < n; i++) {            intj = revv(i, bits);            if(i < j)                swap(a[i], a[j]), swap(b[i], b[j]);        }        for(intlen = 2; len <= n; len <<= 1) {            inthalf = len >> 1;            doublewmx = cos(2 * PI / len), wmy = sin(2 * PI / len);            if(rev) wmy = -wmy;            for(inti = 0; i < n; i += len) {                doublewx = 1, wy = 0;                for(intj = 0; j < half; j++) {                    doublecx = a[i + j], cy = b[i + j];                    doubledx = a[i + j + half], dy = b[i + j + half];                    doubleex = dx * wx - dy * wy, ey = dx * wy + dy * wx;                    a[i + j] = cx + ex, b[i + j] = cy + ey;                    a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;                    doublewnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;                    wx = wnx, wy = wny;                }            }        }        if(rev) {            for(inti = 0; i < n; i++)                a[i] /= n, b[i] /= n;        }    }    intsolve(inta[], intna, intb[], intnb, intans[]) {        intlen = max(na, nb), ln;        for(ln = 0; L(ln) < len; ++ln);        len = L(++ln);        for(inti = 0; i < len ; ++i) {            if(i >= na) ax[i] = 0, ay[i] = 0;            elseax[i] = a[i], ay[i] = 0;        }        fft(ax, ay, len, 0);        for(inti = 0; i < len; ++i) {            if(i >= nb) bx[i] = 0, by[i] = 0;            elsebx[i] = b[i], by[i] = 0;        }        fft(bx, by, len, 0);        for(inti = 0; i < len; ++i) {            doublecx = ax[i] * bx[i] - ay[i] * by[i];            doublecy = ax[i] * by[i] + ay[i] * bx[i];            ax[i] = cx, ay[i] = cy;        }        fft(ax, ay, len, 1);        for(inti = 0; i < len; ++i)            ans[i] = (int)(ax[i] + 0.5);        returnlen;    }    #undef L(x)}constintmaxn = 5e4 + 7;chars1[maxn], s2[maxn];intx[maxn], y[maxn], ans[maxn << 2];intmain() {#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);#endif // ONLINE_JUDGE    while(~scanf("%s", s1)) {        scanf("%s", s2);        intlen1 = strlen(s1), len2 = strlen(s2);        for(inti = 0; i < len1; i ++) x[i] = s1[len1 - i - 1] - ‘0‘;        for(inti = 0; i < len2; i ++) y[i] = s2[len2 - i - 1] - ‘0‘;        fillchar(ans, 0);        intlen = FFT::solve(x, len1, y, len2, ans), i;        for(i = 0; i < len || ans[i] >= 10; i ++) {            ans[i + 1] += ans[i] / 10;            ans[i] %= 10;        }        len = i;        while(ans[len] <= 0 && len) len --;        for(inti = len; i >= 0; i --) putchar(ans[i] + ‘0‘);        puts("");    }    return0;}/* ******************************************************************************** */ | 
标签:
原文地址:http://www.cnblogs.com/jklongint/p/4693001.html