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 ...
随机推荐
- FastDFS单节点安装 & FastDFS+Nginx整合
安装环境 FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastdfs/files/) fastdfs-nginx-module ...
- js dom 操作技巧
1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...
- js 数组操作
toString():把数组转换成一个字符串 toLocaleString():把数组转换成一个字符串 join():把数组转换成一个用符号连接的字符串 shift():将数组头部的一个元素移出 un ...
- Gatling新一代压力测试工具,新一代服务器性能测试工具Gatling
Gatling新一代压力测试工具新一代服务器性能测试工具Gatlinghttp://www.infoq.com/cn/articles/new-generation-server-testing-to ...
- MySQL Crash Course #09# Chapter 17. Combining Queries: UNION
INDEX UNION Rules WHERE VS. UNION UNION VS. UNION ALL Sorting Combined Query Results UNION Rules As ...
- 查询oracle数据库里面所有的表名
如果是当前用户,"select * from tab"即可
- [省选模拟]Rhyme
考的时候脑子各种短路,用个SAM瞎搞了半天没搞出来,最后中午火急火燎的打了个SPFA才混了点分. 其实这个可以把每个模式串长度为$K-1$的字符串看作一个状态,这个用字符串Hash实现,然后我们发现这 ...
- 过滤Windows文件名中的非法字符
转载:http://blog.csdn.net/infoworld/article/details/42033097 场景: 1. 通常生成文件时需要一个文件名,而生成文件名的方式可能是通过用户输入的 ...
- 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 1 章 答案
判断对错1.计算机科学是计算机的研究.2.CPU 是计算机的“大脑”.3.辅助存储器也称为 RAM.4.计算机当前正在处理的所有信息都存储在主存储器中.5.语言的语法是它的意思,语义是它的形式.6.函 ...
- div转svg svg转canvas svg生成图片及图片下载 分享
链接来自:http://blog.csdn.net/u010081689/article/details/50728854