JZYZOJ 2043 多项式除法和取余 NTT 多项式
http://172.20.6.3/Problem_Show.asp?id=2043
最开始用了FFT,交上去全tle和wa了(tle的比较多),测了一组数据发现求逆元的过程爆double了(毕竟系数的指数幂也是指数增长的,科学计数法也撑不住)。
然后问了出题人,发现出题人忘了给用来%的P(想用钢丝球刷出题人QnQ),所以其实是ntt,改完ntt加了个快读a了。
题解:http://blog.miskcoo.com/2015/05/polynomial-division
依然是推式子,系数反转求也是很神奇。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<complex>
using namespace std;
#define LL long long
const int maxn=;
const LL P=(LL)**(<<)+;
LL a[maxn],b[maxn],e[maxn],h[maxn],zz[][maxn];
int bel[maxn]={},s,bt,tot=;
inline void getit(){ for(int i=;i<s;++i)bel[i]=((bel[i>>]>>)|((i&)<<(bt-))); }
LL mpow(LL x,LL k){
if(k<){x=mpow(x,P-);k=-k;}
LL z=;
while(k){
if(k&)z=(z*x)%P;
x=(x*x)%P;k/=;
}
return z;
}
inline void fft(LL * c,int n,int dft){
for(int i=;i<n;++i)if(bel[i]>i)swap(c[i],c[bel[i]]);
for(int step=;step<n;step<<=){
LL w=mpow(,((P-)/(step<<))*dft);
for(int j=;j<n;j+=(step<<)){
LL z=;
for(int i=j;i<j+step;++i){
LL x=c[i],y=(c[i+step]*z)%P;
c[i]=(x+y)%P;c[i+step]=((x-y)%P+P)%P;
z=(z*w)%P;
}
}
}
if(dft==-){
LL nm=mpow(n,P-);
for(int i=;i<n;++i)c[i]=(c[i]*nm)%P;
}
}
void dofft(LL *c,LL *d,int x,int y){
int n=x+y-;s=;bt=;
for(;s<n;++bt)s<<=;getit();
fft(c,s,);fft(d,s,);
for(int i=;i<s;++i)c[i]=(c[i]*d[i])%P;
fft(c,s,-);fft(d,s,-);
}
void doit(int n,int m){
if(m==){ ++tot; zz[tot][]=mpow(b[],P-); return; }
int siz=(m+)/; doit(n,siz); ++tot;
for(int i=;i<siz;++i){zz[tot][i]=(zz[tot-][i]*)%P;e[i]=zz[tot-][i];}
for(int i=;i<min(m,n);++i)h[i]=b[i];//cout<<zz[tot-1][0]<<m<<endl;
dofft(zz[tot-],e,siz,siz);siz=siz+siz-;//cout<<zz[tot-1][0]<<m<<endl;
dofft(zz[tot-],h,siz,min(m,n));
for(int i=;i<m;++i)zz[tot][i]=((zz[tot][i]-zz[tot-][i])%P+P)%P;
//for(int i=0;i<m;++i)cout<<zz[tot][i]<<' ';cout<<endl;
}
LL mread(){
LL x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int main(){
//freopen("a.in","r",stdin);
int n,m;scanf("%d%d",&n,&m);
for(int i=;i<n;++i){a[n-i-]=mread();a[n-i-]=(a[n-i-]%P+P)%P;}
for(int i=;i<m;++i){b[m-i-]=mread();b[m-i-]=(b[m-i-]%P+P)%P;}
doit(m,n-m+);
dofft(zz[tot],a,n-m+,n);
for(int i=n-m;i>=;--i){printf("%lld ",zz[tot][i]);}printf("\n");
for(int i=n-m+;i<s;++i)zz[tot][i]=;
for(int i=;i<=n-m;++i){if(i>=n-m-i)break;swap(zz[tot][i],zz[tot][n-m-i]);}
for(int i=;i<n;++i){if(i>=n-i-)break;swap(a[i],a[n-i-]);}
for(int i=;i<m;++i){if(i>=m-i-)break;swap(b[i],b[m-i-]);}
dofft(b,zz[tot],m,n-m+);
for(int i=;i<m-;++i){a[i]=((a[i]-b[i])%P+P)%P;}
for(int i=;i<m-;++i){printf("%lld ",a[i]);}printf("\n");
return ;
}
JZYZOJ 2043 多项式除法和取余 NTT 多项式的更多相关文章
- uva 10494 - If We Were a Child Again 大数除法和取余
uva 10494 - If We Were a Child Again If We Were a Child Again Input: standard inputOutput: standard ...
- (高精度运算4.7.27)UVA 10494 If We Were a Child Again(大数除法&&大数取余)
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class UVA_10494 ...
- Edu CF 103 Div. 2 (A. K-divisible Sum, B. Inflation贪心),被黑客攻了,,惨掉rank, 思维除法与取余, 不太擅长的类型
2021-01-29 题目链接: Educational Codeforces Round 103 (Rated for Div. 2) 题目 A. K-divisible Sum You are g ...
- 【learning】多项式相关(求逆、开根、除法、取模)
(首先要%miskcoo,这位dalao写的博客(这里)实在是太强啦qwq大部分多项式相关的知识都是从这位dalao博客里面学的,下面这篇东西是自己对其博客学习后的一些总结和想法,大部分是按照其博客里 ...
- Re.多项式除法/取模
前言 emmm又是暂无 前置 多项式求逆 多项式除法/取模目的 还是跟之前一样顾名思义] 给定一个多项式F(x),请求出多项式Q(x)和R(x),满足F(x)=Q(x)∗G(x)+R(x),R项数小于 ...
- 洛谷.4512.[模板]多项式除法(NTT)
题目链接 多项式除法 & 取模 很神奇,记录一下. 只是主要部分,更详细的和其它内容看这吧. 给定一个\(n\)次多项式\(A(x)\)和\(m\)次多项式\(D(x)\),求\(deg(Q) ...
- 2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)
题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展g ...
- C++ int型负数除法取余问题
1:关于除法,不管是正数还是负数都是向0取整的:10/4 = 2,10/(-4) = -2 2:负数取余,通过取模来判定 |小| % |大| = |小| 符号同前 |大| % |小| = |余| ...
- BigInteger实现除法取余
BigInteger实现除法取余 BigInteger是什么? Java中,整形的最大范围是64位的long型整数.但是如果我们使用的整数超过了64位呢?这时候就用到了BigInteger.BigIn ...
随机推荐
- HBase笔记之namespace
一.什么是namespace 在RDBMS中有database的概念,用来对table进行分组,那么在HBase中当表比较多的时候如何对表分组呢,就是namespace,可以简单的把namespace ...
- ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含义
ViewGroup.layout(int l, int t, int r, int b)这个方法是确定View的大小和位置的,然后将其绘制出来,里面的四个参数分别是View的四个点的坐标,他的坐标不是 ...
- android MeasureSpec的三个测量模式
1.MeasureSpec含义 其实可以去看MeasureSpec的文档,里面对MeasureSpec的作用介绍得很清楚.MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureS ...
- keepalived vrrp_script脚本不执行解决办法
首先打开日志观察: tail -f /var/log/messages 然后新开一个客户端重启keepalived , systemctl restart keepalived.service 看日志 ...
- /etc/sysctl.conf 调优 & 优化Linux内核参数
from: http://apps.hi.baidu.com/share/detail/15652067 http://keyknight.blog.163.com/blog/static/36637 ...
- Shape Factory
Factory is a design pattern in common usage. Implement a ShapeFactory that can generate correct shap ...
- 【Linux高级驱动】input子系统框架【转】
转自:http://www.cnblogs.com/lcw/p/3802617.html [1.input子系统框架(drivers\input)] 如何得出某个驱动所遵循的框架? 1) 通过网 ...
- springcloud微服务架构搭建:服务调用
spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign. Ribbon是一个基于HTTP和TCP客户端的负载均衡器,类似nginx反向代理,可 ...
- MongoDB 进阶模式设计
原文链接:http://www.mongoing.com/mongodb-advanced-pattern-design 12月12日上午,TJ在开源中国的年终盛典会上分享了文档模型设计的进阶技巧,就 ...
- 195 Tenth Line
Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...