[hdu1402]A * B Problem Plus(NTT)

解题关键:NTT模板

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
const int N=50010;
typedef long long ll;
ll G=3,P=469762049,a[3*N],b[3*N],wn[25];
char s[N],t[N];
ll mod_pow(ll x,ll n,ll p) {
    ll res=1;x%=p;
    while(n){
        if(n&1) res=res*x%p;
        x=x*x%p;
        n>>=1;
    }
    return res;
}
void getwn(){ for(int i=0;i<25;i++) wn[i]=mod_pow(G,(P-1)/(1<<i),P);}

void NTT(ll x[],int n,int rev){
    int i,j,k,ds;
    ll w,u,v;
    for(i=1,j=n>>1,k=n>>1;i<n-1;i++,k=n>>1){//Rader
        if (i<j) swap(x[i],x[j]);
        while (j>=k){ j-=k;k>>=1; }
        if (j<k) j+=k;
    }
    for(i=2,ds=1;i<=n;i<<=1,ds++)
        for(j=0;j<n;j+=i){
            w=1;
            for(k=j;k<j+i/2;k++){
                u=x[k]%P;v=w*x[k+i/2]%P;
                x[k]=(u+v)%P;
                x[k+i/2]=(u-v+P)%P;
                w=w*wn[ds]%P;
            }
        }
    if(rev==-1){
        for(i=1;i<n/2;i++) swap(x[i],x[n-i]);
        w=mod_pow(n,P-2,P);
        for(i=0;i<n;i++) x[i]=x[i]*w%P;
    }
}
void solve(){
    int n=1,les=strlen(s),let=strlen(t);
    while(n<les+let) n<<=1;
    for(int i=0;i<les;i++) a[i]=s[les-i-1]-‘0‘;
    for(int i=les;i<n;i++) a[i]=0;
    for(int i=0;i<let;i++) b[i]=t[let-i-1]-‘0‘;
    for(int i=let;i<n;i++) b[i]=0;
    NTT(a,n,1);NTT(b,n,1);
    for (int i=0;i<n;i++) a[i]=a[i]*b[i]%P;
    NTT(a,n,-1);
    for(int i=0;i<n-1;i++) a[i+1]+=a[i]/10,a[i]%=10;
    while(n>1&&!a[n-1]) n--;
    for(n--;n>=0;n--) printf("%lld",a[n]);
    printf("\n");
}
int main(){
    getwn();
    while (scanf("%s%s",s,t)!=EOF) solve();
    return 0;
}
时间: 2024-08-01 06:11:51

[hdu1402]A * B Problem Plus(NTT)的相关文章

【NTT】hdu1402 A * B Problem Plus

r·2^k+1 r k g 3 1 1 2 5 1 2 2 17 1 4 3 97 3 5 5 193 3 6 5 257 1 8 3 7681 15 9 17 12289 3 12 11 40961 5 13 3 65537 1 16 3 786433 3 18 10 5767169 11 19 3 7340033 7 20 3 23068673 11 21 3 104857601 25 22 3 167772161 5 25 3 469762049 7 26 3 998244353 119

hdu----(1402)A * B Problem Plus(FFT模板)

A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12665    Accepted Submission(s): 2248 Problem Description Calculate A * B. Input Each line will contain two integers A and B. P

【FFT】hdu1402 A * B Problem Plus

FFT板子. 将大整数看作多项式,它们的乘积即多项式的乘积在x=10处的取值. #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define EPS 1e-8 const double PI = acos(-1.0); struct Complex{ double real,image; Complex(double _r

HDU1402 A * B Problem Plus FFT

分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来解决数据规模较大时的办法,可以达到nlogn的效率,大体原理就是运用了n次单位复根的折半引理 具体可以看算法导论 高度仰慕kuangbin大神的模板,实在是不想自己写 对于这个题,我们10^k的系数看成多项式系数,然后求卷积,进位就好了 #include <stdio.h> #include &l

[HDU1402]A * B Problem Plus(FFT)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意:大数乘法. 数太大,O(n^2)的不行,得用fft对乘法加速. 手推了一遍FFT的公式,感觉欧拉和分治很强,纪念我的第一发FFT. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const double PI = acos(-1.0); 5 //复数结构体 6 typedef struct Complex { 7 doubl

[HDU1402] A * B Problem Plus

题意:高精度乘法 Solution: 将两个十进制数看作数列,卷积后暴力进位即可. int main() { ios::sync_with_stdio(false); string x,y; while(cin>>x>>y) { poly a,b; a.c.resize(x.length()); b.c.resize(y.length()); int la=x.length(),lb=y.length(); for(int i=0;i<la;i++) a.c[i]=x[la-

一步一步的无障碍理解快速傅立叶变换

/////////////////////////////////////////////////////////////////////////////////////////////////////// 作者:tt2767 声明:本文遵循以下协议自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0 查看本文更新与讨论请点击:http://blog.csdn.net/tt2767 链接被删请百度: CSDN tt2767 ///////////////

【HDU1402】【FFT】A * B Problem Plus

Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to end of file. Note: the length of each integer will not exceed 50000. Output For each case, output A * B in one line. Sample Input 1 2 1000 2 Sample Out

【HDU1402】【FNT版】A * B Problem Plus

Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to end of file. Note: the length of each integer will not exceed 50000. Output For each case, output A * B in one line. Sample Input 1 2 1000 2 Sample Out