解题关键:快速数论变换NTT模板。

注意$ans$数组的$ans[n]$一定要注意置$0$,或者结果从$n-1$开始遍历,这里很容易出错。

代码1:ACdreamer 的板子。

为什么要reverse序列至今没证明出来。=,=有懂的聚聚可以告诉本渣一下,万分感谢!!~~

经过聚聚们的指导,还是不太懂,最终从wiki上找到了比较易懂的证明~

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=;
ll G=,P=,a[*N],b[*N],wn[];//P是费马素数
char s[N],t[N];
ll mod_pow(ll x,ll n,ll p) {
ll res=;x%=p;
while(n){
if(n&) res=res*x%p;
x=x*x%p;
n>>=;
}
return res;
}
void getwn(){ for(int i=;i<;i++) wn[i]=mod_pow(G,(P-)/(<<i),P);}//预处理w
void NTT(ll x[],int n,int rev){
ll w,u,v;
for(int i=,j=;i!=n;i++){//构造逆序表
if(i>j) swap(x[i],x[j]);
for(int l=n>>;(j^=l)<l;l>>=);
}
for(int i=,ds=;i<n;i<<=,ds++)
for(int j=;j<n;j+=i){
w=;
for(int k=j;k<j+i/;k++,w=w*wn[ds]%P){//蝴蝶操作
u=x[k]%P;v=w*x[k+i/]%P;
x[k]=(u+v)%P;
x[k+i/]=(u-v+P)%P;
}
}
if(rev==-){
for(int i=;i<n/;i++) swap(x[i],x[n-i]);//把开始的wn求逆元合并到这里来了,通过什么性质至今没搞懂。
w=mod_pow(n,P-,P);//乘n的逆元
for(int i=;i<n;i++) x[i]=x[i]*w%P;
}
}
void solve(){
int i,n=,les=(int)strlen(s),let=(int)strlen(t);
while(n<les+let) n<<=;
for(i=;i<les;i++) a[i]=s[les-i-]-'';
for(i=les;i<=n;i++) a[i]=;
for(i=;i<let;i++) b[i]=t[let-i-]-'';
for(i=let;i<=n;i++) b[i]=;
NTT(a,n,);NTT(b,n,);
for (i=;i<n;i++) a[i]=a[i]*b[i]%P;
NTT(a,n,-);
for(i=;i<n;i++)
if(a[i]>=){//if可以不带
a[i+]+=a[i]/;
a[i]%=;
}
while(n>&&!a[n]) n--;
for(;n>=;n--) printf("%lld",a[n]);
printf("\n");
}
int main(){
getwn();
while(scanf("%s%s",s,t)!=EOF) solve();
return ;
}

代码2:

完全按照fft转化而来

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=;
ll G=,P=,a[*N],b[*N],wn[],iwn[];//P是费马素数
char s[N],t[N];
ll mod_pow(ll x,ll n,ll p) {
ll res=;x%=p;
while(n){
if(n&) res=res*x%p;
x=x*x%p;
n>>=;
}
return res;
}
void getwn(){
for(int i=;i<;i++) wn[i]=mod_pow(G,(P-)/(<<i),P);
for(int i=;i<;i++) iwn[i]=mod_pow(wn[i],P-,P);
}//预处理w
void NTT(ll x[],int n,int rev){
ll w,u,v,wm;
for(int i=,j=;i!=n;i++){//构造逆序表
if(i>j) swap(x[i],x[j]);
for(int l=n>>;(j^=l)<l;l>>=);
}
for(int i=,ds=;i<n;i<<=,ds++)
for(int j=;j<n;j+=i){
w=;
if(rev==-) wm=iwn[ds];
else wm=wn[ds];
for(int k=j;k<j+i/;k++,w=w*wm%P){//蝴蝶操作
u=x[k]%P;v=w*x[k+i/]%P;
x[k]=(u+v)%P;
x[k+i/]=(u-v+P)%P;
}
}
if(rev==-){
w=mod_pow(n,P-,P);//乘n的逆元
for(int i=;i<n;i++) x[i]=x[i]*w%P;
}
}
void solve(){
int i,n=,les=(int)strlen(s),let=(int)strlen(t);
while(n<les+let) n<<=;
for(i=;i<les;i++) a[i]=s[les-i-]-'';
for(i=les;i<=n;i++) a[i]=;
for(i=;i<let;i++) b[i]=t[let-i-]-'';
for(i=let;i<=n;i++) b[i]=;
NTT(a,n,);NTT(b,n,);
for (i=;i<n;i++) a[i]=a[i]*b[i]%P;
NTT(a,n,-);
for(i=;i<n;i++)
if(a[i]>=){//if可以不带
a[i+]+=a[i]/;
a[i]%=;
}
while(n>&&!a[n]) n--;
for(;n>=;n--) printf("%lld",a[n]);
printf("\n");
}
int main(){
getwn();
while(scanf("%s%s",s,t)!=EOF) solve();
return ;
}

FFT常用素数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

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

  1. FFT/NTT模板 既 HDU1402 A * B Problem Plus

    @(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...

  2. 【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 ...

  3. luoguP1919 A*B Problem升级版 ntt

    luoguP1919 A*B Problem升级版 链接 luogu 思路 ntt模板题 代码 #include <bits/stdc++.h> #define ll long long ...

  4. XJTUOJ wmq的A×B Problem FFT/NTT

    wmq的A×B Problem 发布时间: 2017年4月9日 17:06   最后更新: 2017年4月9日 17:07   时间限制: 3000ms   内存限制: 512M 描述 这是一个非常简 ...

  5. 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 ...

  6. HDU1402 A * B Problem Plus

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. HDU1402 A * B Problem Plus(FFT)

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 初学FFT. http://www.cnblogs.com/WABoss/p/FFT_Note.html ...

  8. HDU1402 A * B Problem Plus FFT

    分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...

  9. HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...

随机推荐

  1. shell一些方法

    字符串截取转自原文地址:http://www.jb51.net/article/56563.htm 一:字符串截取 有var变量: var=http://www.aaa.com/123.htm 1. ...

  2. Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times

    http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...

  3. Data Structure Array: Find the minimum distance between two numbers

    http://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/ #include <iostream> ...

  4. 《程序员代码面试指南》第二章 链表问题 删除中间节点和a/b处节点

    题目 例如 1-2-3-4 删除2,1-2-3-4-5 删除3 例如 a=1,b =2 java代码 /** * @Description:删除中间节点和a/b处节点 * @Author: lizho ...

  5. ie6不认识

    1.   ie6 不认识这样连续写的类选择 .class1.class2{  } 2.   ie6 下不解析 css  input[type="text"] 等等类别的选择

  6. Elipse 快捷键

    1. eclipse里面如何快速收缩当前类文件里面的所有方法和注释收缩:ctrl+shift+/展开:ctrl+shift+*注意:这个/和*要是数字键盘上的/和*.   2. shift+enter ...

  7. HDU5371 Hotaru's problem

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. BZOJ 2243 [SDOI2011]染色:树剖【维护路径上颜色段】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2243 题意: 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径 ...

  9. 英语发音规则---Z字母

    英语发音规则---Z字母 一.总结 一句话总结:字母Z的名称zed /zed/,美式英语的称zee /zi:/,少数方言(如香港)读izzard /'izəɹd/. 1.字母Z在单词中发[z]? pu ...

  10. linux下安装rpm格式的mysql

    1.下载安装包官网下载.rpm格式安装包,需要下面两个文件: MySQL-server-5.0.26-0.i386.rpm MySQL-client-5.0.26-0.i386.rpm 注:官网下载时 ...