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 huangmingyi @ 2023-09-26 21:13:56
《"悬棺" 》
by yang2_0 @ 2023-10-07 16:46:34
《入门》
by zouyinghan @ 2023-10-10 13:48:49
#include<bits/stdc++.h>
using namespace std;
int a,b;
int main(){
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
cin>>a>>b;
cout<<a+b;
return 0;
}
不明白为啥要那么多啊
by James__ji @ 2023-10-22 09:09:15
@李翊辰2012 define用的…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
by 李翊辰2012 @ 2023-12-16 20:34:10
特短代码:print(sum(map(int,input().split())))
\
只有一行
by dulinfan2023 @ 2023-12-23 16:01:27
真·萌新
by Stars_visitor_tyw @ 2024-01-05 17:10:18
没那么麻烦,超级快读get
#include<assert.h>
#include<ctype.h>
#include<errno.h>
#include<float.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<wchar.h>
#include<wctype.h>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
using namespace std;
namespace Fast_I {
char *_Buf, *_Start_ptr, *_End_ptr;
std::streambuf* inbuf;
unsigned int Size;
bool _Ok;
struct Fast_Istream {
operator bool() { return _Ok; }
Fast_Istream(std::streambuf*, unsigned int);
Fast_Istream(unsigned int);
Fast_Istream(const char*, unsigned int);
Fast_Istream& operator>>(char&);
Fast_Istream& operator>>(char*);
Fast_Istream& operator>>(bool&);
Fast_Istream& operator>>(short&);
Fast_Istream& operator>>(int&);
Fast_Istream& operator>>(long&);
Fast_Istream& operator>>(long long&);
Fast_Istream& operator>>(unsigned short&);
Fast_Istream& operator>>(unsigned int&);
Fast_Istream& operator>>(unsigned long&);
Fast_Istream& operator>>(unsigned long long&);
Fast_Istream& operator>>(float&);
Fast_Istream& operator>>(double&);
Fast_Istream& operator>>(long double&);
Fast_Istream& operator>>(std::string&);
template <typename Typex>
void operator()(Typex& _Val) { *this >> _Val; }
template <typename Typex, typename... More>
void operator()(Typex&, More&...);
std::streambuf* rdbuf() { return inbuf; }
void rdbuf(std::streambuf* _inbuf) { inbuf = _inbuf; }
void rdbuf(const char*);
void pop();
char peek();
};
} // namespace Fast_I
namespace Fast_O {
std::string buf;
std::streambuf* outbuf;
struct Fast_Ostream {
Fast_Ostream(std::streambuf*, unsigned int);
Fast_Ostream(std::streambuf* out) { outbuf = out; }
Fast_Ostream(const char*, unsigned int);
Fast_Ostream(unsigned int);
void flush();
~Fast_Ostream();
void endl() { buf.push_back('\n'); }
template <typename Typex>
void endl(const Typex& _Val);
template <typename Typex, typename... More>
void endl(const Typex&, const More&...);
template <typename Typex>
void operator()(const Typex& _Val);
template <typename Typex, typename... More>
void operator()(const Typex&, const More&...);
Fast_Ostream& operator<<(char);
Fast_Ostream& operator<<(const char*);
Fast_Ostream& operator<<(const std::string&);
Fast_Ostream& operator<<(bool);
Fast_Ostream& operator<<(short);
Fast_Ostream& operator<<(int);
Fast_Ostream& operator<<(long);
Fast_Ostream& operator<<(long long);
Fast_Ostream& operator<<(unsigned short);
Fast_Ostream& operator<<(unsigned int);
Fast_Ostream& operator<<(unsigned long);
Fast_Ostream& operator<<(unsigned long long);
std::streambuf* rdbuf() { return outbuf; }
void rdbuf(std::streambuf* _outbuf) { outbuf = _outbuf; }
void rdbuf(const char*);
};
} // namespace Fast_O
namespace Fast_IO {
Fast_I::Fast_Istream fin(std::cin.rdbuf(), 1048576); // 1 MB
Fast_O::Fast_Ostream fout(std::cout.rdbuf()); // ∞
} // namespace Fast_IO
#define cin Fast_IO::fin
#define cout Fast_IO::fout
/* ------------------------------------- */
signed main() {
int a, b;
cin(a, b); // cin >> a >> b;
cout(a + b, '\n'); // cout << a + b << '\n'; // cout.endl(a + b);
return 0;
}
/* ------------------------------------- */
namespace Fast_I {
Fast_Istream::Fast_Istream(std::streambuf* in, unsigned int Sz) {
_Ok = 1;
Fast_I::Size = Sz;
inbuf = in;
_Start_ptr = _End_ptr = _Buf = new char[Sz];
}
Fast_Istream::Fast_Istream(const char* in, unsigned int Sz) {
_Ok = 1;
Fast_I::Size = Sz;
rdbuf(in);
_Start_ptr = _End_ptr = _Buf = new char[Sz];
}
Fast_Istream::Fast_Istream(unsigned int Sz) {
_Ok = 1;
Fast_I::Size = Sz;
_Start_ptr = _End_ptr = _Buf = new char[Sz];
}
void Fast_Istream::rdbuf(const char* File) {
static std::ifstream __In__(File);
rdbuf(__In__.rdbuf());
}
void Get_Char(char& _Val) {
if (_Start_ptr == _End_ptr) {
_Start_ptr = _Buf;
_End_ptr = _Buf + inbuf->sgetn(_Buf, Size);
}
if (_Start_ptr == _End_ptr) {
_Val = -1;
_Ok = 0;
} else {
_Val = *_Start_ptr++;
}
}
Fast_Istream& Fast_Istream::operator>>(char& _Val) {
if (_Ok) {
Get_Char(_Val);
while (_Val == 32 || _Val == 10 || _Val == 13 || _Val == 8 || _Val == 9 ||
_Val == 7 || _Val == 12 || _Val == 11) {
Get_Char(_Val);
}
}
return *this;
}
Fast_Istream& Fast_Istream::operator>>(char* _Val) {
if (_Ok) {
Get_Char(*_Val);
while (*_Val == 32 || *_Val == 10 || *_Val == 13 || *_Val == 8 ||
*_Val == 9 || *_Val == 7 || *_Val == 12 || *_Val == 11) {
Get_Char(*_Val);
}
while (*_Val != 32 && *_Val != 10 && *_Val && *_Val != -1 && *_Val != 9 &&
*_Val != 11 && *_Val != 12) {
Get_Char(*++_Val);
}
*_Val = 0;
--_Start_ptr;
}
return *this;
}
Fast_Istream& Fast_Istream::operator>>(std::string& _Val) {
if (_Ok) {
char c;
Get_Char(c);
while (c == 32 || c == 10 || c == 13 || c == 8 || c == 9 || c == 7 ||
c == 12 || c == 11) {
Get_Char(c);
}
for (_Val.clear();
c != 32 && c != 10 && c && c != -1 && c != 9 && c != 11 && c != 12;
Get_Char(c)) {
_Val.push_back(c);
}
--_Start_ptr;
}
return *this;
}
template <typename Typex>
void Get_Int(Typex& _Val) {
if (_Ok) {
char ch;
bool _F = 0;
for (Get_Char(ch); (ch < 48 || ch > 57) && ch != -1; Get_Char(ch)) {
_F = ch == 45;
}
for (_Val = 0; ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
_Val = _Val * 10 + (ch ^ 48);
}
if (_F) {
_Val = ~_Val + 1;
}
--_Start_ptr;
}
}
template <typename Typex>
void Get_Unsigned(Typex& _Val) {
if (_Ok) {
char ch;
Get_Char(ch);
while ((ch < 48 || ch > 57) && ch != -1) {
Get_Char(ch);
}
for (_Val = 0; ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
_Val = _Val * 10 + (ch ^ 48);
}
--_Start_ptr;
}
}
template <typename Typex>
void Get_Double(Typex& _Val) {
if (_Ok) {
char ch;
bool _F = 0;
for (Get_Char(ch); (ch < 48 || ch > 57) && ch != -1; Get_Char(ch)) {
_F = ch == 45;
}
for (_Val = 0; ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
_Val = _Val * 10 + (ch ^ 48);
}
if (ch == 46) {
unsigned long long _Pow = 1;
for (Get_Char(ch); ch > 47 && ch < 58 && ch != -1; Get_Char(ch)) {
_Val += Typex((ch ^ 48) * 1.0 / (_Pow *= 10));
}
}
if (_F) {
_Val = -_Val;
}
--_Start_ptr;
}
}
Fast_Istream& Fast_Istream::operator>>(bool& _Val) {
if (_Ok) {
char ch;
Get_Char(ch);
while (ch == 32 || ch == 10 || ch == 13 || ch == 8 || ch == 9 || ch == 7 ||
ch == 12 || ch == 11) {
Get_Char(ch);
}
while (ch != 32 && ch != 10 && ch && ch != -1 && ch != 9 && ch != 11 &&
ch != 12) {
_Val |= ch != 48;
Get_Char(ch);
}
--_Start_ptr;
}
return *this;
}
Fast_Istream& Fast_Istream::operator>>(short& _Val) {
Get_Int(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(int& _Val) {
Get_Int(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(long& _Val) {
Get_Int(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(long long& _Val) {
Get_Int(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(unsigned short& _Val) {
Get_Unsigned(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(unsigned int& _Val) {
Get_Unsigned(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(unsigned long& _Val) {
Get_Unsigned(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(unsigned long long& _Val) {
Get_Unsigned(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(float& _Val) {
Get_Double(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(double& _Val) {
Get_Double(_Val);
return *this;
}
Fast_Istream& Fast_Istream::operator>>(long double& _Val) {
Get_Double(_Val);
return *this;
}
template <typename Typex, typename... More>
void Fast_Istream::operator()(Typex& _Val, More&... _More) {
*this >> _Val;
operator()(_More...);
}
void Fast_Istream::pop() {
char ch;
Get_Char(ch);
}
char Fast_Istream::peek() {
if (_Start_ptr == _End_ptr) {
_Start_ptr = _Buf;
_End_ptr = _Buf + inbuf->sgetn(_Buf, Size);
}
if (_Start_ptr == _End_ptr) {
_Ok = 0;
return -1;
} else {
return *_Start_ptr;
}
}
} // namespace Fast_I
namespace Fast_O {
Fast_Ostream::Fast_Ostream(std::streambuf* out, unsigned int Size) {
buf.reserve(Size);
outbuf = out;
}
Fast_Ostream::Fast_Ostream(const char* File, unsigned int Size) {
buf.reserve(Size);
rdbuf(File);
}
void Fast_Ostream::rdbuf(const char* File) {
static std::ofstream __Out__(File);
rdbuf(__Out__.rdbuf());
}
Fast_Ostream::Fast_Ostream(unsigned int Size) {
buf.reserve(Size);
}
void Fast_Ostream::flush() {
outbuf->sputn(buf.data(), buf.size());
buf.clear();
}
Fast_Ostream::~Fast_Ostream() {
flush();
}
Fast_Ostream& Fast_Ostream::operator<<(char _Val) {
buf.push_back(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(const char* _Val) {
while (*_Val) {
buf.push_back(*_Val++);
}
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(const std::string& _Val) {
for (auto&& i : _Val) {
buf.push_back(i);
}
return *this;
}
template <typename Typex>
void Put_Unsigned(Typex _Val) {
char* _Stack = (char*)malloc(sizeof(Typex) * 3);
unsigned S_top = 0;
while (_Val) {
_Stack[++S_top] = (_Val % 10) ^ 48;
_Val /= 10;
}
if (!S_top) {
buf.push_back('0');
}
while (S_top) {
buf.push_back(_Stack[S_top--]);
}
free(_Stack);
}
void Put_Int(long long _Val) {
if (_Val < 0) {
buf.push_back('-');
Put_Unsigned(~_Val + 1);
} else {
Put_Unsigned(_Val);
}
}
Fast_Ostream& Fast_Ostream::operator<<(bool _Val) {
buf.push_back(_Val ? '1' : '0');
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(short _Val) {
Put_Int(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(int _Val) {
Put_Int(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(long _Val) {
Put_Int(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(long long _Val) {
Put_Int(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(unsigned short _Val) {
Put_Unsigned(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(unsigned int _Val) {
Put_Unsigned(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(unsigned long _Val) {
Put_Unsigned(_Val);
return *this;
}
Fast_Ostream& Fast_Ostream::operator<<(unsigned long long _Val) {
Put_Unsigned(_Val);
return *this;
}
template <typename Typex>
void Fast_Ostream::endl(const Typex& _Val) {
*this << _Val << '\n';
}
template <typename Typex, typename... More>
void Fast_Ostream::endl(const Typex& _Val, const More&... _More) {
*this << _Val;
endl(_More...);
}
template <typename Typex>
void Fast_Ostream::operator()(const Typex& _Val) {
*this << _Val;
}
template <typename Typex, typename... More>
void Fast_Ostream::operator()(const Typex& _Val, const More&... _More) {
*this << _Val;
operator()(_More...);
}
}
int n,ml,md,a,b,c,fst[10100],nex[50010],v[50010],w[50010],cnt,vis[10100],dis[10100],tim[10100];
queue<int> q;
void add(int a,int b,int c)
{
nex[++cnt]=fst[a];
fst[a]=cnt;
v[cnt]=b;
w[cnt]=c;
return ;
}
int spfa(int k)
{
memset(dis,0x7f/3,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(tim,0,sizeof(tim));
q.push(k);
dis[k]=0;
vis[k]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
tim[u]++;
vis[u]=0;
if(tim[u]>n)
return -1;
for(int i=fst[u];i!=-1;i=nex[i])
{
if(dis[v[i]]>dis[u]+w[i])
{
dis[v[i]]=dis[u]+w[i];
if(!vis[v[i]])
{
q.push(v[i]);
vis[v[i]]=1;
}
}
}
}
if(dis[n]>1e8)
return -2;
return dis[n];
}
by see_htdls @ 2024-01-06 20:13:48
@taoyiwei17_cfynry 我也认为你的代码不麻烦,当然,是你写
by Stars_visitor_tyw @ 2024-01-06 21:33:19
@3_15_good 超级快读板子不是我写的,有大佬发了云剪贴板
by tangqizhang @ 2024-01-20 09:48:17
一共只要写九行,你写了亿行。666