[hdu1402]A * B Problem Plus(NTT)
解题关键:快速数论变换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)的更多相关文章
- FFT/NTT模板 既 HDU1402 A * B Problem Plus
@(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...
- 【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 ...
- luoguP1919 A*B Problem升级版 ntt
luoguP1919 A*B Problem升级版 链接 luogu 思路 ntt模板题 代码 #include <bits/stdc++.h> #define ll long long ...
- XJTUOJ wmq的A×B Problem FFT/NTT
wmq的A×B Problem 发布时间: 2017年4月9日 17:06 最后更新: 2017年4月9日 17:07 时间限制: 3000ms 内存限制: 512M 描述 这是一个非常简 ...
- 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 ...
- HDU1402 A * B Problem Plus
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 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 ...
- HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...
- HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...
随机推荐
- Python基础(1)_python介绍、简单运算符
Python执行一个程序分为三个阶段 阶段一:先启动python解释器 阶段二:python解释器把硬盘中的文件读入到内存中 阶段三:python解释器解释执行刚刚读入内存的代码 二.编程语言的分类: ...
- 实用篇:说说我在JavaScript项目中使用的工具类
在JavaScript的开发中,我们都会写一些工具类来帮我们简化一些业务操作的逻辑,一下就貼几个我在项目开发过程中常用的工具类.表达能力有限,各位看官还是看源码吧. 一.日期处理工具类. /** * ...
- 07 09&10
0709: 排名还是不高,毕竟没切出来题. 第一题dalao: 要求你做一个三维数点,只回答最终有多少个点对的状态是完全小于(可比?)的.(n<=2000000) 特殊限制是三维都是随机排列. ...
- 常用CSS设置
主要内容: 一.容器类 二.文本类 三.特效类 一.容器类 1.background-image:url('img/02.gif'); 设置背景图(可以是动态图) 2.background-col ...
- 什么是shell【TLCL】
常用命令 date cal df——report file system disk space usage free——display amount of free and used memory i ...
- cookie对比localStorage哪个适合作为网站皮肤存储
cookie对比localStorage哪个适合作为网站皮肤存储 cookie cookie : 一般由服务器生成,可设置失效时间.如果在浏览器生成,默认是关闭浏览器之后失效 存储大小:4k 每次都会 ...
- ArrayList中存储数组时需要注意到的问题
因为数组的地址是不会发生变化的,每次在数组中的内容改变后,将数组添加到ArrayList中时,会导致ArrayList中的每个内容都是最后添加进去的数据.案例如下所示: Object []objs = ...
- Java 学习摘要
//导入 import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; Date dt= ...
- cin.get(),cin.getline(),getline(),gets(),getchar()
1.cin.get() (1).cin.get()-------提取单个字符,可以提取回车.空格 a=cin.get(); (2) 同(1)---------------提取单个字符,可以提取回车.空 ...
- javascript笔记(二)
concat() 连接多个字符串,返回合并后的字符串. 1 var s1="a"; 2 var s2="b"; 3 var s3="c"; ...