题目描述

给出两个 $n$ 位10进制数x和y,求x*y(详见 洛谷P1919

分析

假设已经学会了FFT/NTT。

高精度乘法只是多项式乘法的特殊情况,相当于$x=10$ 时。

例如n=3,求123*111

$$123 = x^2 + 2x + 3$$

$$111 = x^2 + x +1$$

$$\begin{aligned}123 * 111 &= (x^2 + 2x + 3)(x^2 + x +1)\\  &= x^4 + 3x^3 + 6x^2 + 5x + 3\\  &= 13653\end{aligned}$$

代码:

#include<bits/stdc++.h>
#define rg register
using namespace std; typedef long long ll;
const int mod=,g=;
const int maxn = 6e4 + ; inline int qpow(int x,int k)
{
int ans=;
while(k)
{
if(k&)
ans=(ll)ans*x%mod;
x=(ll)x*x%mod,k>>=;
}
return ans;
} inline int module(int x,int y)
{
x+=y;
if(x>=mod)
x-=mod;
return x;
} int rev[*maxn];
inline void NTT(int*t,int lim,int type)
{
for(rg int i=;i<lim;++i)
if(i<rev[i])
swap(t[i],t[rev[i]]);
for(rg int i=;i<lim;i<<=)
{
int gn=qpow(g,(mod-)/(i<<));
if(type==-)
gn=qpow(gn,mod-);
for(rg int j=;j<lim;j+=(i<<))
{
int gi=;
for(rg int k=;k<i;++k,gi=(ll)gi*gn%mod)
{
int x=t[j+k],y=(ll)gi*t[j+i+k]%mod;
t[j+k]=module(x,y);
t[j+i+k]=module(x,mod-y);
}
}
}
if(type==-)
{
int inv=qpow(lim,mod-);
for(rg int i=;i<lim;++i)
t[i]=(ll)t[i]*inv%mod;
}
} int X[*maxn],Y[*maxn];
inline void mul(int*x, int*y, int n, int m)
{
memset(X,,sizeof(X));
memset(Y,,sizeof(Y));
int lim = , L = ; //L=0必须写,局部变量默认值很可能不是0
while(lim <= n + m) lim <<= , L++; //lim为大于(n+m)的2的幂,所以最多需要4倍空间
for(int i = ; i < lim; i++) rev[i] = (rev[i >> ] >> ) | ((i & ) << (L - ));
for(rg int i=;i<lim;++i) X[i]=x[i],Y[i]=y[i];
NTT(X,lim,);
NTT(Y,lim,);
for(rg int i=;i<lim;++i) X[i]=(ll)X[i]*Y[i]%mod;
NTT(X,lim,-);
for(rg int i=;i<lim;++i) x[i]=X[i];
} int n;
int a[*maxn], b[*maxn];
char s[maxn]; int main()
{
scanf("%d", &n);
scanf("%s", s);
for(int i = ;i < n;i++) a[i] = s[n--i] - '';
scanf("%s", s);
for(int i = ;i < n;i++) b[i] = s[n--i] - '';
mul(a, b, n, n); // for(int i = 0;i < 2*n;i++) printf("%d ", a[i]);
// printf("\n"); int tmp = ; //进位
for(int i = ;i < *n;i++) //
{
a[i] = a[i] + tmp;
tmp = a[i] / ;
a[i] = a[i] % ; } // for(int i = 0;i < 2*n;i++) printf("%d ", a[i]);
// printf("\n"); bool flag = true;
for(int i = *n;i >= ;i--) //逆序输出,去掉前导零
{
if(flag && a[i] == ) continue;
printf("%d", a[i]);
flag = false;
} return ;
}

【模板】A*B Problem升级版(FFT快速傅里叶)的更多相关文章

  1. 洛谷.1919.[模板]A*B Problem升级版(FFT)

    题目链接:洛谷.BZOJ2179 //将乘数拆成 a0*10^n + a1*10^(n-1) + ... + a_n-1的形式 //可以发现多项式乘法就模拟了竖式乘法 所以用FFT即可 注意处理进位 ...

  2. P1919 【模板】A*B Problem升级版 /// FFT模板

    题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...

  3. 【模板】A*B Problem(FFT快速傅里叶)

    题目:给出两个n位10进制整数x和y,你需要计算x*y.($n \leq 60000$) 分析: 两个正整数的相乘可以视为两个多项式的相乘, 例如 $15 \times 16 = 240$, 可写成 ...

  4. 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)

    洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...

  5. FFT快速傅里叶模板

    FFT快速傅里叶模板…… /* use way: assign : h(x) = f(x) * g(x) f(x):len1 g(x):len2 1. len = 1; while(len < ...

  6. luoguP1919 A*B Problem升级版 ntt

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

  7. 【luogu P3803】【模板】多项式乘法(FFT)

    [模板]多项式乘法(FFT) 题目链接:luogu P3803 题目大意 给你两个多项式,要你求这两个多项式乘起来得到的多项式.(卷积) 思路 系数表示法 就是我们一般来表示一个多项式的方法: \(A ...

  8. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  9. FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)

    前言 众所周知,这两个东西都是用来算多项式乘法的. 对于这种常人思维难以理解的东西,就少些理解,多背板子吧! 因此只总结一下思路和代码,什么概念和推式子就靠巨佬们吧 推荐自为风月马前卒巨佬的概念和定理 ...

  10. 2018.08.28 洛谷P3803 【模板】多项式乘法(FFT)

    传送门 fft模板题. 终于学会fft了. 这个方法真是神奇! 经过试验发现手写的complex快得多啊! 代码: #include<iostream> #include<cstdi ...

随机推荐

  1. 将 MathType 公式转换为 Word 自带公式

    以下操作是基于Office 365以及MathType 6.9b平台.有网友留言说第四步没出现「转换为 Office Math」选项,这个我就不清楚了,难道是只有Office 365才支持? 打开Ma ...

  2. diy操作系统 附录:常用命令

    ld -m elf_i386 as --32 gcc -m 16 o

  3. Java中数据存储分配

    (1)内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编 译时就可以给 ...

  4. python实现数字0开始的索引,对应Execl的字母方法

    字母转数字方法: import re col = row = [] # 输入正确格式的定位,A2,AA2有效,AAB2无效 while len(col) == 0 or len(row) == 0 o ...

  5. linux上启动tomcat报错:Failed to read schema document 'http://www.springframework.org/schema/data/mongo/spring-mongo-2.0.xsd

    本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! spring在加载xsd文件时总是先试图在本地查找xs ...

  6. harbor上传镜像

    在harbor服务器 1. 下载测试上传使用的镜像docker pull hello-world2. 打tagdocker tag docker.io/hello-world:latest 172.1 ...

  7. Thread 如何安全结束一个线程 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. ASM实例修改SYS密码

    修改ASM实例中SYS用户密码 How To Change ASM SYS PASSWORD ? (文档 ID 452076.1) Oracle Database - Enterprise Editi ...

  9. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 动态数据的呈现

    https://www.cnblogs.com/cynchanpin/p/7065098.html 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC ...

  10. cnn健康增胖和调理好身体

    吃不胖,其实大部分情况是消化系统不好,大部分食物都没有被身体吸收就被排掉了. 1,改善肠胃消化功能: 每天早上一杯全脂鲜牛奶(或者羊奶), 每天晚上一杯酸奶 ps:白天和鲜牛奶可以激发肠胃的消化能力. ...