hdu1402A * B Problem Plus(NTT)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402

题意:大数乘法

分析:练习下NTT。学习网站:http://blog.csdn.net/acdreamers/article/details/39026505  http://blog.miskcoo.com/2015/04/polynomial-multiplication-and-fast-fourier-transform#i-15

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=50010;
const int MAX=151;
const int mod=100000000;
const int MOD1=100000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
ll G=3,P=469762049,a[3*N],b[3*N],wn[25];
char s[N],t[N];
ll q_pow(ll a,ll b,ll M) {
    ll ret=1;a%=M;
    while (b) {
        if (b&1) ret=ret*a%M;
        a=a*a%M;
        b>>=1;
    }
    return ret;
}
void getwn() {
    for (int i=0;i<25;i++) wn[i]=q_pow(G,(P-1)/(1<<i),P);
}
void NTT(ll x[],int n,int rev) {
    int i,j,k,t,ds,id=0;
    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=q_pow(n,P-2,P);
        for (i=0;i<n;i++) x[i]=x[i]*w%P;
    }
}
void solve() {
    int i,n=1,les=strlen(s),let=strlen(t);
    while (n<les+let) n<<=1;
    for (i=0;i<les;i++) a[i]=s[les-i-1]-'0';
    for (i=les;i<n;i++) a[i]=0;
    for (i=0;i<let;i++) b[i]=t[let-i-1]-'0';
    for (i=let;i<n;i++) b[i]=0;
    NTT(a,n,1);NTT(b,n,1);
    for (i=0;i<n;i++) a[i]=a[i]*b[i]%P;
    NTT(a,n,-1);
    for (i=0;i<n-1;i++) a[i+1]+=a[i]/10,a[i]%=10;
    while (a[n-1]>10) { a[n]+=a[n-1]/10;a[n-1]%=10;n++; }
    while (n>1&&!a[n-1]) n--;
    for (i=n-1;i>=0;i--) printf("%I64d", a[i]);
    printf("\n");
}
int main()
{
    getwn();
    while (scanf("%s%s", s, t)!=EOF) solve();
    return 0;
}

/*
常用费马素数P=r*2^k+1,g为P的原根
P       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
1004535809      479     21      3
2013265921      15      27      31
2281701377      17      27      3
3221225473      3       30      5
75161927681     35      31      3
77309411329     9       33      7
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值