ybchenyuyang @ 2023-09-05 23:15:38
萌新初学FFT,想来简单的试一下,代码也没有完全搞懂,照着其他博客的思路打了一遍,结果40pts,有RE,WA
#include<bits/stdc++.h>
using namespace std;
typedef struct node{
double a,b;
node(double x=0,double y=0){
a=x,b=y;
}
node operator+(const node&t)const{
return node(a+t.a,b+t.b);
}
node operator-(const node&t)const{
return node(a-t.a,b-t.b);
}
node operator*(const node&t)const{
return node(a*t.a-b*t.b,a*t.b+b*t.a);
}
}aplusb;
const double pi=2*acos(-1);
char f[20],g[20],c[20];
int n,m,r[20];
aplusb a[20],w[20];
int log_2(int x){
int ans=0;
if(x&0xffff0000){
ans+=16,x>>=16;
}
if(x&0xff00){
ans+=8,x>>=8;
}
if(x&0xf0){
ans+=4,x>>=4;
}
if(x&0xc){
ans+=2,x>>=2;
}
if(x&2){
++ans;
}
return ans;
}
void init(){
m+=n;
int Log_2=log_2(m);
n=1<<Log_2+1;
for(int i=0;i<n;++i){
r[i]=r[i>>1]>>1|(i&1)<<Log_2;
}
aplusb t(cos(pi/n),sin(pi/n));
w[0].a=1;
for(int i=1;i<n;++i){
w[i]=w[i-1]*t;
}
}
void swap(aplusb& a,aplusb& b){
static aplusb t;
t=a,a=b,b=t;
}
void fft(bool type){
for(int i=0;i<n;++i){
if(i<r[i]){
swap(a[i],a[r[i]]);
}
}
static aplusb x,y;
for(int len=2;len<=n;len<<=1){
for(int l=0;l<n;l+=len){
for (int i=0,j=len>>1;j<len;++i,++j){
x=a[l+i],y=w[n/len*i];
if(type){
y=y*a[l+j];
}else{
y=aplusb(y.a,-y.b)*a[l+j];
}
a[l+i]=x+y,a[l+j]=x-y;
}
}
}
}
int main(){
cin>>f>>g;
n=strlen(f)-1;
m=strlen(g)-1;
for(int i=n;~i;--i){
a[i].a=(double)(f[n-i]^48);
}
for(int i=m;~i;--i){
a[i].b=(double)(g[m-i]^48);
}
init();
fft(1);
for(int i=0;i<n;++i){
a[i]=a[i]*a[i];
}
fft(0);
int t=0;
for(int i=0;i<=m;++i){
t+=(int)(a[i].b/n/2.0+0.5);
c[i]=t%10^48;
t/=10;
}
int ans=0;
if(t!=0){
ans=ans*10+t;
}
for(int i=m;i>=0;--i){
ans=ans*10+(c[i]-'0');
}
n=strlen(f)-1;
m=strlen(g)-1;
int a=0,b=0;
for(int i=0;i<=n;++i){
a=a*10+(f[i]-'0');
}
for(int i=0;i<=m;++i){
b=b*10+(g[i]-'0');
}
cout<<ans-a*b+a+b;
return 0;
}
by zhangmingsheng3521 @ 2023-09-05 23:18:02
@cyyyyds857 A了3紫“萌新”
by ybchenyuyang @ 2023-09-05 23:20:16
@zhangmingsheng3521 哦,那是我小号做的,我把他们全部搬运了过来,求大佬改改
by ybchenyuyang @ 2023-09-05 23:21:34
@zhangmingsheng3521 其实那3道都很水的,一个迭代加深,一个模板,一个数位DP
by Clebershao_PCL @ 2023-09-09 13:27:11
A+B那么麻烦
by Clebershao_PCL @ 2023-09-09 13:28:13
https://www.luogu.com.cn/user/807952#activity 你们看这个
by ybchenyuyang @ 2023-09-09 22:33:22
@Clebershao_PCL 放我小号干嘛
by JshGLJ @ 2023-09-13 20:11:32
太6了,八行代码可以解决的你用了亿行代码, 还有,你能编出这么多来,你告诉我你是萌新,《猛礥》(确信)
by penziwang666 @ 2023-09-16 17:47:47
666,连世界最强计算机神也做不出来!!!```c
using namespace std;
int main() { int a,b; cin >> a >> b; cout << a+b; return 0; }
by 李翊辰2012 @ 2023-09-23 20:58:16
啥都不说了
#include <iostream>
#define hello int
#define kkksc03 a
#define chen_zhe b
#define world main
#define say cout
#define an <<
#define meet >>
#define put cin
#define AK return
#define IOI 0
using namespace std;
hello world(){
hello kkksc03;
hello chen_zhe;
put meet kkksc03 meet chen_zhe;
say an kkksc03 + chen_zhe;
AK IOI;
}
萌新+1
by 李翊辰2012 @ 2023-09-23 21:00:13
@penziwang666
望丰展?使mx