A * B Problem Plus

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26449    Accepted Submission(s): 6917

Problem Description
Calculate A * B.
 
Input
Each line will contain two integers A and B. Process to end of file.

Note: the length of each integer will not exceed 50000.

 
Output
For each case, output A * B in one line.
 
Sample Input
1
2
1000
2
 
Sample Output
2
2000
 
Author
DOOM III
 代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std; const double PI = acos(-1.0);
//复数结构体
struct complex
{
double r,i;
complex(double _r = 0.0,double _i = 0.0)
{
r = _r; i = _i;
}
complex operator +(const complex &b)
{
return complex(r+b.r,i+b.i);
}
complex operator -(const complex &b)
{
return complex(r-b.r,i-b.i);
}
complex operator *(const complex &b)
{
return complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
/*
* 进行FFT和IFFT前的反转变换。
* 位置i和 (i二进制反转后位置)互换
* len必须去2的幂
*/
void change(complex y[],int len)
{
int i,j,k;
for(i = , j = len/;i < len-; i++)
{
if(i < j)swap(y[i],y[j]);
//交换互为小标反转的元素,i<j保证交换一次
//i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
k = len/;
while( j >= k)
{
j -= k;
k /= ;
}
if(j < k) j += k;
}
}
/*
* 做FFT
* len必须为2^k形式,
* on==1时是DFT,on==-1时是IDFT
*/
void fft(complex y[],int len,int on)
{
change(y,len);
for(int h = ; h <= len; h <<= )
{
complex wn(cos(-on**PI/h),sin(-on**PI/h));
for(int j = ;j < len;j+=h)
{
complex w(,);
for(int k = j;k < j+h/;k++)
{
complex u = y[k];
complex t = w*y[k+h/];
y[k] = u+t;
y[k+h/] = u-t;
w = w*wn;
}
}
}
if(on == -)
for(int i = ;i < len;i++)
y[i].r /= len;
}
const int MAXN = ;
complex x1[MAXN],x2[MAXN];
char str1[MAXN/],str2[MAXN/];
int sum[MAXN];
int main()
{
ios_base::sync_with_stdio();
cin.tie();
while(cin>>str1>>str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = ;
while(len < len1* || len < len2*)len<<=;
for(int i = ;i < len1;i++)
x1[i] = complex(str1[len1--i]-'',);//这里的len1-1-i主要是为了下面的求和
for(int i = len1;i < len;i++)
x1[i] = complex(,);
for(int i = ;i < len2;i++)
x2[i] = complex(str2[len2--i]-'',);
for(int i = len2;i < len;i++)
x2[i] = complex(,);
//求DFT
fft(x1,len,);
fft(x2,len,);
for(int i = ;i < len;i++)
x1[i] = x1[i]*x2[i];
fft(x1,len,-);
for(int i = ;i < len;i++)
sum[i] = (int)(x1[i].r+0.5);
for(int i = ;i < len;i++)
{
sum[i+]+=sum[i]/;
sum[i]%=;
}
len = len1+len2-;
while(sum[len] <= && len > )len--;
for(int i = len;i >= ;i--)
printf("%c",sum[i]+'');
printf("\n");
}
return ;
}

高精度求A*B(FFT)的更多相关文章

  1. C# 高精度求幂 poj1001

    高精度求幂 public static char[] exponentiation(string a,int r) { ]; string b = ""; string c = a ...

  2. Contset Hunter 1102 高精度求卡特兰数

    用递推的方式写的写挂了,而如果用组合数又不会高精度除法,偶然看到了别人的只用高精度乘低精度求组合数的方法,记录一下 #include<bits/stdc++.h> using namesp ...

  3. 多项式求逆/分治FFT 学习笔记

    一.多项式求逆 给定一个多项式 \(F(x)\),请求出一个多项式 \(G(x)\), 满足 \(F(x) * G(x) \equiv 1 ( \mathrm{mod\:} x^n )\).系数对 \ ...

  4. [模板] 多项式: 乘法/求逆/分治fft/微积分/ln/exp/幂

    多项式 代码 const int nsz=(int)4e5+50; const ll nmod=998244353,g=3,ginv=332748118ll; //basic math ll qp(l ...

  5. ACM-ICPC 2018 焦作赛区网络预赛 G Give Candies(高精度求余)

    https://nanti.jisuanke.com/t/31716 题意 n颗糖果n个人,按顺序给每个人任意数目(至少一个)糖果,问分配方案有多少. 分析 插板法或者暴力打表后发现答案就为2^(n- ...

  6. bzoj 3456 城市规划 多项式求逆+分治FFT

    城市规划 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1091  Solved: 629[Submit][Status][Discuss] Desc ...

  7. codevs 4165 ​高精度求阶乘

    时间限制: 1 s  空间限制: 256000 KB  题目等级 : 白银 Silver 题目描述 Description 用高精度计算出S=n! 其中"!"表示阶乘,例如:5!= ...

  8. hdu 5730 Shell Necklace [分治fft | 多项式求逆]

    hdu 5730 Shell Necklace 题意:求递推式\(f_n = \sum_{i=1}^n a_i f_{n-i}\),模313 多么优秀的模板题 可以用分治fft,也可以多项式求逆 分治 ...

  9. 转自 z55250825 的几篇关于FFT的博文(一)

        关于FFT,咱们都会迫不及待地 @  .....(大雾)(貌似被玩坏了...)    .....0.0学习FFT前先orz FFT君.         首先先是更详细的链接(手写版题解点赞0v ...

随机推荐

  1. R语言中的数据分析函数

    数学类函数 在求有缺失值的子集的最大值时候,需要先用na.re=TRUE去掉缺失值. 求几个特定百分位数 round(x,n) n表示保留的小数点位数 分布类函数 rnorm,密度高的数字,生成概率就 ...

  2. pwd - 显示出当前/活动目录的名称

    总览 (SYNOPSIS) pwd [OPTION] 描述 (DESCRIPTION) 显示出 完整的 当前 活动目录 名称. --help 显示 帮助 信息, 然后 退出 --version 显示 ...

  3. HTML基础 有序列表写个人收藏夹

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. phpstorm 开发php入门

    环境:ubuntu  phpstorm apache mysql 一.安装软件 安装apache服务器 https://i.cnblogs.com/EditPosts.aspx?postid=1113 ...

  5. python3输出中文报错的原因,及解决办法(基于pycharm)

    通常python3里面如果有中文,在不连接其他设备和程序的情况下,报错信息大致如下: SyntaxError: Non-UTF-8 code starting with '\xd6' in file ...

  6. SpringBoot+Shiro学习(七):Filter过滤器管理

    SpringBoot+Shiro学习(七):Filter过滤器管理 Hiwayz 关注  0.5 2018.09.06 19:09* 字数 1070 阅读 5922评论 1喜欢 20 先从我们写的一个 ...

  7. Unit Testing of Classes in Java

    Every class can have a main method. That is a handy trick for unit testing of classes. For example, ...

  8. ubuntu安装deb包(dpkg)

    安装 sudo dpkg -i DEB_PACKAGE 卸载 sudo dpkg -r PACKAGE_NAME 重新配置已安装的包 Reconfigure an existing package 例 ...

  9. Spring Boot 2.x:SpringBoot

    几个重要的事件回调机制: 1.配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunlisten ...

  10. python 列表使用

    下面实现的类似于java中的数组: names[-2]表示实现倒数的第2个参数 names[-3,-1]表示实现-3到-1的值不包含-1 增删改查 下面代码实现列表的增删改查功能: 复制copy 深c ...