题意:乘法

要用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的更多相关文章

  1. SPOJ VFMUL - Very Fast Multiplication (FFT)

    题目链接:VFMUL - Very Fast Multiplication Description Multiply the given numbers. Input n [the number of ...

  2. SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法

    SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...

  3. Hamming Weight的算法分析(转载)

    看代码时遇到一个求32bit二进制数中1的个数的问题,感觉算法很奇妙,特记录学习心得于此,备忘. 计算一个64bit二进制数中1的个数. 解决这个问题的算法不难,很自然就可以想到,但是要给出问题的最优 ...

  4. SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)

    题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围:  1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...

  5. SPOJ 4110 Fast Maximum Flow (最大流模板)

    题目大意: 无向图,求最大流. 算法讨论: Dinic可过.终于我的常数还是太大.以后要注意下了. #include <cstdio> #include <cstring> # ...

  6. SPOJ QTREE 系列解题报告

    题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...

  7. $\mathcal{FFT}$·$\mathcal{Fast \ \ Fourier \ \ Transformation}$快速傅立叶变换

    \(2019.2.18upd:\) \(LINK\) 之前写的比较适合未接触FFT的人阅读--但是有几个地方出了错,大家可以找一下233 啊-本来觉得这是个比较良心的算法没想到这么抽搐这个算法真是将一 ...

  8. 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 ...

  9. 数字图像处理实验(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 ...

随机推荐

  1. 【转】eclipse反编译插件

    原文地址:http://bbs.csdn.net/topics/390263414 离线安装包下载地址一:http://feeling.sourceforge.net/downloads/org.sf ...

  2. Qt之美(一):D指针/私有实现

    The English version is available at: http://xizhizhu.blogspot.com/2010/11/beauty-of-qt-1-d-pointer-p ...

  3. Python Web学习笔记之并发编程IO模型

    了解新知识之前需要知道的一些知识 同步(synchronous):一个进程在执行某个任务时,另外一个进程必须等待其执行完毕,才能继续执行 #所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调 ...

  4. jQuery 概述

    jQuery 概述 版权声明:未经博主授权,内容严禁分享转载! 什么是 JavaScript 类库 JavaScript 类库是指已经被封装好的一系列 JavaScript 函数,能够实现一些特定的功 ...

  5. CAN/J1850/

    (1)CAN:(差分信号)有信号CANH=3.5V,CANL=1.5V, 没有信号CANH=2.5V,CANL=2.5V 速率:CAN系统又分为高速和低速,高速CAN系统采用硬线是动力型,速度:500 ...

  6. DD-WRT自定义脚本更新花生壳DDNS

    N年以前买了一个tp-link 841n v7,一直用的还算可以吧,除了不定期重启路由器,不然网速慢的龟爬啊!这也是TP原厂固件的通病,于是刷了DD-WRT,话说DD确实很爽,除了功能强大之外,而且很 ...

  7. luogu1049装箱问题

    装箱问题 传送门 一个箱子容量为V//容量 同时有n个物品//n个 体积&&价值 要求n个物品中任取若干个装入箱内,使箱子的剩余空间为最小// v减去价值最大 */ #include& ...

  8. POJ1144 Network(割点)题解

    Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are c ...

  9. FieldOffset

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.G ...

  10. 【安装】Mysql在Linux上安装

    1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:mysql-5.6.37-linux-glibc2.12- ...