spoj Fast Multiplication
题意:乘法
要用nlogn的fft乘法。
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
using namespace std;
typedef long long lon;
//#define ll long long
//#define inf 1000000000
//#define mod 1000000007
#define N 350000
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
const lon SZ=,INF=0x7FFFFFFF;
const double pi = acos(-);
char s1[N>>],s2[N>>];
double rea[N],ina[N],reb[N],inb[N],ret[N],intt[N];
int i,len1,len2,lent,lenres,len;
int res[N>>];
void Swap(double &x,double &y)
{double t = x; x = y; y = t;}
int rev(int x,int len)
{
int ans = ,i;
fo(i,,len) {ans<<=; ans|=x&; x>>=;}
return ans;
}
void FFT(double *reA,double *inA,int n,int flag)
{
int s,i,j,k; int lgn = log((double)n) / log((double));
fo(i,,n-)//数组重排
{
j = rev(i,lgn);
if (j > i) {Swap(reA[i],reA[j]); Swap(inA[i],inA[j]);}
} fo(s,,lgn)
{
int m = ( << s);
double reWm = cos(*pi/m) , inWm = sin(*pi/m);
if (flag) inWm = -inWm;
for (k = ;k < n; k += m)
{
double reW = 1.0 , inW = 0.0;
fo(j,,m/-)
{
int tag = k + j + m / ;
double reT = reW * reA[tag] - inW * inA[tag];
double inT = reW * inA[tag] + inW * reA[tag];
double reU = reA[k+j] , inU = inA[k+j];
reA[k+j] = reU + reT; inA[k+j] = inU + inT;
reA[tag] = reU - reT; inA[tag] = inU - inT;
double reWt = reW * reWm - inW * inWm;
double inWt = reW * inWm + inW * reWm;
reW = reWt; inW = inWt;
}
} } }
void work()
{
scanf(" %s %s",s1,s2);
memset(res, , sizeof(res));
memset(rea, , sizeof(rea));
memset(ina, , sizeof(ina));
memset(reb, , sizeof(reb));
memset(inb, , sizeof(inb));
len1 = strlen(s1); len2 = strlen(s2);
lent = (len1 > len2 ? len1 : len2); len = ;
while (len < lent) len <<= ; len <<= ;
fo(i,,len-)
{
if (i < len1) rea[i] = (double) s1[len1-i-] - '';
if (i < len2) reb[i] = (double) s2[len2-i-] - '';
ina[i] = inb[i] = 0.0;
}
FFT(rea,ina,len,); FFT(reb,inb,len,);//求出a、b的点值表示法
fo(i,,len-)//求出c的点值表示法
{
//printf("%.5lf %.5lf\n",rea[i],ina[i]);
double rec = rea[i] * reb[i] - ina[i] * inb[i];
double inc = rea[i] * inb[i] + ina[i] * reb[i];
rea[i] = rec; ina[i] = inc;
}
FFT(rea,ina,len,);
fo(i,,len-) {rea[i] /= len; ina[i] /= len;} fo(i,,len-) res[i] = (int)(rea[i] + 0.5);
fo(i,,len-) res[i+] += res[i] / , res[i] %= ; lenres = len1 + len2 + ;
while (res[lenres] == && lenres > ) lenres--;
fd(i,lenres,) printf("%d",res[i]); printf("\n");
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","w",stdout);
lon casenum;
cin>>casenum;
for(lon time=;time<=casenum;++time)
//for(;scanf("%d",&n)!=EOF;)
{
work();
//cout<<mul(m1,m2)<<endl;
}
return ;
}
spoj Fast Multiplication的更多相关文章
- SPOJ VFMUL - Very Fast Multiplication (FFT)
题目链接:VFMUL - Very Fast Multiplication Description Multiply the given numbers. Input n [the number of ...
- SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...
- Hamming Weight的算法分析(转载)
看代码时遇到一个求32bit二进制数中1的个数的问题,感觉算法很奇妙,特记录学习心得于此,备忘. 计算一个64bit二进制数中1的个数. 解决这个问题的算法不难,很自然就可以想到,但是要给出问题的最优 ...
- SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)
题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围: 1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...
- SPOJ 4110 Fast Maximum Flow (最大流模板)
题目大意: 无向图,求最大流. 算法讨论: Dinic可过.终于我的常数还是太大.以后要注意下了. #include <cstdio> #include <cstring> # ...
- SPOJ QTREE 系列解题报告
题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...
- $\mathcal{FFT}$·$\mathcal{Fast \ \ Fourier \ \ Transformation}$快速傅立叶变换
\(2019.2.18upd:\) \(LINK\) 之前写的比较适合未接触FFT的人阅读--但是有几个地方出了错,大家可以找一下233 啊-本来觉得这是个比较良心的算法没想到这么抽搐这个算法真是将一 ...
- Lintcode: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long
In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...
- 数字图像处理实验(5):PROJECT 04-01 [Multiple Uses],Two-Dimensional Fast Fourier Transform 标签: 图像处理MATLAB数字图像处理
实验要求: Objective: To further understand the well-known algorithm Fast Fourier Transform (FFT) and ver ...
随机推荐
- 20155239 2016-2017-2 《Java程序设计》第5周学习总结
教材内容学习 第八章 JAVA异常架构 Java异常是Java提供的一种识别及响应错误的一致性机制. Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅,并提高程序健壮性 ...
- Impala SQL 语言元素(翻译)[转载]
原 Impala SQL 语言元素(翻译) 本文来源于http://my.oschina.net/weiqingbin/blog/189413#OSC_h2_2 摘要 http://www.cloud ...
- 20165207 Exp0 Kali安装
Exp0 Kali安装 选哪个 在打开www.kali.org的网页之后,我进入其下载页面,可供下载的kali版本有很多 对于选择哪个版本,在实验楼我用过xfce桌面.下面还有直接下下来就可以在vm里 ...
- Applying the Kappa architecture in the telco industry
https://www.oreilly.com/ideas/applying-the-kappa-architecture-in-the-telco-industry Kappa architectu ...
- schema与catalog的理解
sql环境中Catalog和Schema都属于抽象概念,主要用来解决命名冲突问题.一个数据库系统包含多个Catalog,每个Catalog包含多个Schema,每个Schema包含多个数据库对象(表. ...
- WindowsServer-性能计数器
https://jingyan.baidu.com/article/59703552e764e48fc00740dd.html
- tomcat热启动没问题, 访问项目报500解决办法
新建maven项目 添加热启动 启动访问项目报错 报错提示 解决办法 思路:包冲突 在pom.xml中添加servlet <dependency> <groupId>javax ...
- Poisson Blending(Seamless clone)研究和实现
Poisson Blending 实现了非常棒的效果,可以看 <自己动手,实现“你的名字”滤镜> http://www.cnblogs.com/jsxyhelu/p/7216795.htm ...
- Duilib 实现右下角弹出像QQ新闻窗口,3秒后窗口透明度渐变最后关闭,若在渐变过程中鼠标放到窗口上,窗口恢复最初状态(二)
效果: 1.定义两个个定时器ID #define ID_TIMER_DISPLAY_DELAY 30 #define ID_TIMER_DISPLAY_CLOSE 40 2.添加一个成员函数和成员变量 ...
- Mac安装和卸载HomeBrew
安装方法: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/ ...