大数相乘(hdu 1402)
------------------题目链接---------------------
题目没啥说的,两个数相乘,fft,一发模板就AC,kuangbin模板大法好,不懂原理的小白也能体验AC。
个人学的比较菜,不懂原理,把学习资料整合一下。存一下模板。
大佬的讲解 另一个讲解
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <complex>
using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const double eps=0.0000000001;
void fre()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
}
#define MSET(a,b) memset(a,b,sizeof(a))
const double PI=acos(-1.0);
typedef complex<double> Complex;
void change(Complex y[],int len)
{
int i,j,k;
for(i=1,j=len/2; i<len-1; i++)
{
if(i<j)
swap(y[i],y[j]);
k=len/2;
while(j>=k)
{
j-=k;
k/=2;
}
if(j<k) j+=k;
}
}
void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h=2; h<=len; h<<=1)
{
Complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h));
for(int j=0; j<len; j+=h)
{
Complex w(1,0);
for(int k=j; k<j+h/2; k++)
{
Complex u=y[k];
Complex t=w*y[k+h/2];
y[k]=u+t;
y[k+h/2]=u-t;
w=w*wn;
}
}
}
if(on==-1)
{
for(int i=0; i<len; i++)
y[i].real(y[i].real()/len);
}
}
/******************************************************/
const int MAXN = 200010;
Complex x1[MAXN], x2[MAXN];
char str1[MAXN / 2], str2[MAXN / 2];
int sum[MAXN];
int main()
{
while (scanf("%s%s", str1, str2) == 2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = 1;
while (len < len1 * 2 || len < len2 * 2) //2^n<
len <<= 1;
for (int i = 0; i < len1; i++)
x1[i] = Complex(str1[len1 - 1 - i] - '0', 0);
for (int i = len1; i < len; i++)
x1[i] = Complex(0, 0);
for (int i = 0; i < len2; i++)
x2[i] = Complex(str2[len2 - 1 - i] - '0', 0);
for (int i = len2; i < len; i++)
x2[i] = Complex(0, 0);
//求DFT
fft(x1, len, 1);
fft(x2, len, 1);
for (int i = 0; i < len; i++)
x1[i] = x1[i] * x2[i];
fft(x1, len, -1);
for (int i = 0; i < len; i++)
sum[i] = (int)(x1[i].real() + 0.5);
for (int i = 0; i < len; i++)
{
sum[i + 1] += sum[i] / 10;
sum[i] %= 10;
}
len = len1 + len2 - 1;
while (sum[len] <= 0 && len > 0)
len--;
for (int i = len; i >= 0; i--)
printf("%c", sum[i] + '0');
printf("\n");
}
return 0;
}
大数相乘(hdu 1402)的更多相关文章
- HDU 1402 FFT 大数乘法
$A * B$ FFT模板题,找到了一个看起来很清爽的模板 /** @Date : 2017-09-19 22:12:08 * @FileName: HDU 1402 FFT 大整数乘法.cpp * ...
- 两个大数相乘 - 高精度FFT
HDU 1402 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- POJ 2389 Bull Math(水~Java -大数相乘)
题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...
- 大数相乘算法C++版
#include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...
- java版大数相乘
在搞ACM的时候遇到大数相乘的问题,在网上找了一下,看到了一个c++版本的 http://blog.csdn.net/jianzhibeihang/article/details/4948267 用j ...
- Linux C/C++ 编程练手 --- 大数相加和大数相乘
最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- Karatsuba乘法--实现大数相乘
Karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.此算法主要用于两个大数相乘 ...
- leetcode 43 Multiply Strings 大数相乘
感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...
随机推荐
- javascript 函数初探 (六)--- 闭包初探#2
首先,我们需要声明一个全局函数的占位符.尽管这种占位符不是必须的,但最好还是声明一下,然后我们重新将函数F()定义一下: var inner; var F = fucntion(){ var b = ...
- Android开发——内存优化 图片处理
8. 用缓存避免内存泄漏 很常见的一个例子就是图片的三级缓存结构,分别为网络缓存,本地缓存以及内存缓存.在内存缓存逻辑类中,通常会定义这样的集合类. private HashMap<Strin ...
- Direct2D教程(一)Direct2D已经来了,谁是GDI的终结者?
什么是Direct2D 一言以蔽之,就是Windows 7平台上的一个2D图形API,可以提供高性能,高质量的2D渲染.大多数人对Direct2D可能都比较陌生,以至于我之前在论坛上提到这个词的时候, ...
- .Net ToString Format [转]
源文 :http://blog.csdn.net/luyifeiniu/article/category/25663/2 stringstr1 =string.Format("{0:N1}& ...
- Android 适配器教程 (六)
我们的适配器学习已经接近尾声了.尽管这不是一个大问题,可是确实是值得学习的一块知识,回忆一下之前五讲的知识.我们已经学到了非常多东西了. 在之前五讲中.我们已经由浅入深的认识了适配器,从最简单的Lis ...
- C结构体对齐
函数模板针对仅参数类型不同的函数? http://blog.csdn.net/renrenhappy/article/details/5931457 计算结构体的大小就要考虑数据对齐问题. ...
- 服务管理-Nginx
nginx优势 select,epoll模型 对于一次IO访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间.所以说.当一个read ...
- caffe搭建以及初步学习--win7-vs2013-gtx650tiboost-cuda8.0-cifar10训练和测试-2-完整解决方案cifar10_full_solver.prototxt
首先总结前一节的内容. 简单的讲,就是训练并测试了快速解决方案. 转换数据格式: convert_cifar_data.exe data/cifar10 examples/cifar10 lmdb 计 ...
- dsoframer注冊说明及在VC2010使用
一.dsoframer在XP.win7和win8中的注冊方法. 从微软站点下载DsoFrmaer_KB311765_x86.exe,双击解开后得到的dsoframer.ocx等文件. (一)XP注冊 ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...