1. 程式人生 > >高精度乘法(轉載待完善)

高精度乘法(轉載待完善)

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<complex>
using namespace std;
int n;
typedef complex<double> cd;
#define maxl 10003
#define PI 3.14159265358979
char s1[maxl],s2[maxl];
cd a[maxl];
cd b[maxl];
int rev[maxl];
void get_rev(int
bit) { for(int i=0;i<(1<<bit);i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(bit-1)); } void fft(cd *a,int n,int dft) { for(int i=0;i<n;i++) if(i<rev[i]) swap(a[i],a[rev[i]]); for(int step=1;step<n;step<<=1) { cd wn=exp(cd(0,dft*PI/step)); for
(int j=0;j<n;j+=step<<1) { cd wnk(1,0); for(int k=j;k<j+step;k++) { cd x=a[k]; cd y=wnk*a[k+step]; a[k]=x+y; a[k+step]=x-y; wnk*=wn; } } } if
(dft==-1) for(int i=0;i<n;i++) a[i]/=n; } char output[10000003]; int main() { //freopen("fft.in","r",stdin); while(~scanf("%s%s",s1,s2)){ memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(rev, 0, sizeof(rev)); memset(output, 0, sizeof(output)); long l1=strlen(s1); long l2=strlen(s2); int s=2,bit=1; for(bit=1;(1<<bit)<l1+l2-1;bit++)s<<=1;//maybe wiping the"-1" is better for(int i=0;i<l1;i++) a[i]=(double)(s1[l1-i-1]-'0'); for(int i=0;i<l2;i++) b[i]=(double)(s2[l2-i-1]-'0'); //for(int i=0;i<8;i++) printf("%d %d\n",i,rev[i]); get_rev(bit); fft(a,s,1); fft(b,s,1); for(int i=0;i<s;i++) a[i]*=b[i]; fft(a,s,-1); for(int i=0;i<s;i++) { output[i]+=(int)(a[i].real()+0.5);//取實數四捨五入,此時虛數部分應當為0或由於浮點誤差接近0 output[i+1]+=output[i]/8; output[i]%=8; } int i; for(i=l1+l2;!output[i]&&i>=0;i--); if(i==-1) printf("0"); for(;i>=0;i--) printf("%d",output[i]); putchar('\n'); } }

相關推薦

精度乘法轉載完善

#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<complex> using na

精度乘法精乘C語言實現

原始碼&註釋 #include <stdio.h> #include <string.h> char s[10000],ss[10000]; int a[10000],b[10000],c[10000]; int len,l

精度乘法1174:大整數乘法

#include<cstdio> #include<cstring> using namespace std; int main() { char a[205],b[205]; int aa[205]={0},bb[205]={0},c[205]=

極客編程日歷2018桌面壁紙轉載完善

base tps 官方文檔 site 軟件 報錯 編寫 left .exe 轉載聲明 本文轉載自簡書文章【極客編程日歷2018桌面壁紙】,並進行了補充。 圖靈社區出版了一本極客編程日歷“Happy Hacking 2018” ,實體已經售罄,但是提供了PDF電子版下載

精度模板不定時更新

void 支持 rev == memset spa -s -- code 以前寫高精度基本都是抄別人的……這次要改變一下了…… 現在的高精度模板還是很簡陋的,只支持高精加,減,乘,高精除低精,高精模低精,高精快速冪,高精比較大小,沒了。 或許以後會不定期更新一下……畢竟這個

索引原理和SQL優化轉載整理

索引的本質 MySQL官方對索引的定義為:索引(Index)是幫助MySQL高效獲取資料的資料結構。提取句子主幹,就可以得到索引的本質:索引是資料結構。 我們知道,資料庫查詢是資料庫的最主要功能之一。我們都希望查詢資料的速度能儘可能的快,因此資料庫系統的設計者會從查詢演算法的角度進行優化。最

精度加法A+B問題

A+B,作為演算法競賽中最難的一題(霧),總是吸引著廣大程式設計師想盡一切演算法想要去AC它: 這裡提供一種高精度的做法: #include <iostream> #define maxn 10000000 + 5 using namespace std;

精度加法進位制

1 /* 2 高精度進位制加法 3 n為進位制(n<=36) 4 */ 5 #include<bits/stdc++.h> 6 using namespace std; 7 const int maxn=10000; 8 int n; 9 stru

Dev c++ 精度加法500位以內

思路很簡單,把a,b兩個數當作字串來處理,然後對應每一位相加(使用ASCII碼運算),在處理一下進位以及最終數值和的總位數的細節 ok #include <bits/stdc++.h> #define LEN 505 using namespace std;

精度加減法大數相加 減

題目來自學校的OJ平臺  Description 程式設計實現15位至25位整數的加減法。 Input 第一個數 +或者- 第二個數 Output 運算結果,注意結果的正負 Sample Input 100000000000000000

c++精度運算加、乘

#include <stdio.h> #include <iostream> #include <memory.h> #include <math.h> using namespace std; #define MI 10

精度小數中國大學MOOCC語言

題目內容:由於計算機內部表達方式的限制,浮點運算都有精度問題,為了得到高精度的計算結果,就需要自己設計實現方法。(0,1)之間的任何浮點數都可以表達為兩個正整數的商,為了表達這樣兩個數的商,可以將相除的結果以多個整數來表示,每個整數表示結果的一位。即商的第一位用一個整數來表示

洛谷 P1303 A*B Problem精度乘法 題解

正文 題目 names printf 精度 bool return max org 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接 :https://www.luogu.org/problem/show?pid=1303 題目描述

POJ 1001精度乘法

在POJ上,測試通過(因為我在CSDN中很多不能通過的,特別宣告) 一,首先,說說我的解題思路,就是模仿乘法的運算過程,再來用字串存結果;(這個不是很難); 二,這個題目對於我來說,難的是沒有考慮全面, a,  第一次使用完變數,前面都要初始化,不然容易出現 rinning

精度乘法入門詳解精乘

高精度乘法。輸入兩個正整數,求它們的積。 【演算法分析】 類似加法,可以用豎式求乘法。在做乘法運算時,同樣也有進位,同時對每一位進行乘法運算時,必須進行錯位相加,如圖3、圖4。 分析c陣列

【bzoj2179】FFT快速傅裏葉變換優化精度乘法

efi swa 快速傅裏葉變換 得到 urn lse can sin %s #include<bits/stdc++.h> using namespace std; #define pi acos(-1) typedef complex<double&g

C語言精度乘法

題目描述 這道題很簡單,只需要計算a*b即可,唯一有點麻煩的就是a和b的長度可能有點長,什麼int啊,long long啊,double啊都可能從不下,怎麼辦?啟明星軟體組組長給了我一堆資料讓我幫他計算結果,你們誰來幫幫我。。。 輸入 輸入有多組資料。對於每組測

精度乘法 【C++版簡單模擬版和FFT快速版和java版】

高精度乘法C++版 簡單模擬版(N^2複雜度): #include <cstdio> #include <cstring> #include <cstdlib> #include <memory.h> using names

Bull Math精度——乘法

Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer Joh

精度四則運算改進

struct bign {  int len,s[20005],res[20005];  bign ()  {   memset(s,0,sizeof(s));   memset(res,0,sizeof(res));   len=1;  }  void rea