#
i
n
c
l
u
d
e
<
q
u
e
u
e
>
\#include<queue>
#include<queue>
#
i
n
c
l
u
d
e
<
s
t
d
i
o
.
h
>
\#include<stdio.h>
#include<stdio.h>
#
i
n
c
l
u
d
e
<
s
t
r
i
n
g
.
h
>
\#include<string.h>
#include<string.h>
#
i
n
c
l
u
d
e
<
a
l
g
o
r
i
t
h
m
>
\#include<algorithm>
#include<algorithm>
#
d
e
f
i
n
e
N
4010
\#define\ N\ 4010
#define N 4010
u
s
i
n
g
n
a
m
e
s
p
a
c
e
s
t
d
;
using\ namespace\ std;
using namespace std;
s
t
r
u
c
t
V
{
i
n
t
x
,
y
,
d
;
}
z
;
struct\ V\{\ int\ x,y,d;\ \}\ z;
struct V{ int x,y,d; } z;
c
h
a
r
A
[
N
]
,
B
[
N
]
;
i
n
t
n
,
m
,
l
[
N
]
,
f
[
N
]
[
N
]
;
q
u
e
u
e
<
V
>
q
;
char\ A[N],B[N];\ int\ n,m,l[N],f[N][N];\ queue<V>\ q;
char A[N],B[N]; int n,m,l[N],f[N][N]; queue<V> q;
n
a
m
e
s
p
a
c
e
S
A
M
{
namespace\ SAM\{
namespace SAM{
i
n
t
s
[
N
]
[
26
]
,
m
x
[
N
]
,
f
[
N
]
,
c
n
t
=
1
,
l
s
t
=
1
;
\quad int\ s[N][26],mx[N],f[N],cnt=1,lst=1;
int s[N][26],mx[N],f[N],cnt=1,lst=1;
i
n
l
i
n
e
i
n
t
e
x
t
e
n
d
(
i
n
t
c
)
{
\quad inline\ int\ extend(int\ c)\{
inline int extend(int c){
i
n
t
p
=
l
s
t
,
n
p
=
l
s
t
=
+
+
c
n
t
,
q
,
n
q
;
\quad \quad int\ p=lst,np=lst=++cnt,q,nq;
int p=lst,np=lst=++cnt,q,nq;
m
x
[
n
p
]
=
m
x
[
p
]
+
1
;
\quad \quad mx[np]=mx[p]+1;
mx[np]=mx[p]+1;
f
o
r
(
;
p
&
&
!
s
[
p
]
[
c
]
;
p
=
f
[
p
]
)
s
[
p
]
[
c
]
=
n
p
;
\quad \quad for(;p\&\&!s[p][c];p=f[p])\ s[p][c]=np;
for(;p&&!s[p][c];p=f[p]) s[p][c]=np;
i
f
(
!
p
)
r
e
t
u
r
n
f
[
n
p
]
=
1
;
\quad \quad if(!p)\ return\ f[np]=1;
if(!p) return f[np]=1;
q
=
s
[
p
]
[
c
]
;
\quad \quad q=s[p][c];
q=s[p][c];
i
f
(
m
x
[
q
]
=
=
m
x
[
p
]
+
1
)
f
[
n
p
]
=
q
;
\quad \quad if(mx[q]==mx[p]+1)\ f[np]=q;
if(mx[q]==mx[p]+1) f[np]=q;
e
l
s
e
{
\quad \quad else\{
else{
n
q
=
+
+
c
n
t
;
\quad \quad \quad nq=++cnt;
nq=++cnt;
m
x
[
n
q
]
=
m
x
[
p
]
+
1
;
\quad \quad \quad mx[nq]=mx[p]+1;
mx[nq]=mx[p]+1;
f
[
n
q
]
=
f
[
q
]
;
f
[
q
]
=
f
[
n
p
]
=
n
q
;
\quad \quad \quad f[nq]=f[q];\ f[q]=f[np]=nq;
f[nq]=f[q]; f[q]=f[np]=nq;
m
e
m
c
p
y
(
s
[
n
q
]
,
s
[
q
]
,
26
<
<
2
)
;
\quad \quad \quad memcpy(s[nq],s[q],26<<2);
memcpy(s[nq],s[q],26<<2);
f
o
r
(
;
p
&
&
s
[
p
]
[
c
]
=
=
q
;
p
=
f
[
p
]
)
s
[
p
]
[
c
]
=
n
q
;
\quad \quad \quad for(;p\&\&s[p][c]==q;p=f[p])\ s[p][c]=nq;
for(;p&&s[p][c]==q;p=f[p]) s[p][c]=nq;
}
\quad \quad \}
}
}
\quad \}
}
i
n
l
i
n
e
v
o
i
d
m
a
t
c
h
(
)
{
\quad inline\ void\ match()\{
inline void match(){
f
o
r
(
i
n
t
x
=
1
,
i
=
1
,
c
=
0
;
i
<
=
n
;
+
+
i
)
{
\quad \quad for(int\ x=1,i=1,c=0;i<=n;++i)\{
for(int x=1,i=1,c=0;i<=n;++i){
f
o
r
(
;
x
>
1
&
&
!
s
[
x
]
[
A
[
i
]
−
′
a
′
]
;
x
=
f
[
x
]
)
;
\quad \quad \quad for(;x>1\ \&\&\ !s[x][A[i]-'a'];x=f[x]);
for(;x>1 && !s[x][A[i]−′a′];x=f[x]);
c
=
m
x
[
x
]
;
\quad \quad \quad c=mx[x];
c=mx[x];
i
f
(
!
s
[
x
]
[
A
[
i
]
−
′
a
′
]
)
l
[
i
]
=
0
;
e
l
s
e
{
\quad \quad \quad if(!s[x][A[i]-'a'])\ l[i]=0;\ else\{
if(!s[x][A[i]−′a′]) l[i]=0; else{
x
=
s
[
x
]
[
A
[
i
]
−
′
a
′
]
;
l
[
i
]
=
+
+
c
;
\quad \quad \quad \quad x=s[x][A[i]-'a'];\ l[i]=++c;
x=s[x][A[i]−′a′]; l[i]=++c;
}
\quad \quad \quad \}
}
}
\quad \quad \}
}
}
\quad \}
}
}
\}
}
n
a
m
e
s
p
a
c
e
A
A
M
{
namespace\ AAM\{
namespace AAM{
i
n
t
s
[
N
]
[
26
]
;
\quad int\ s[N][26];
int s[N][26];
i
n
l
i
n
e
v
o
i
d
b
u
i
l
d
(
)
{
\quad inline\ void\ build()\{
inline void build(){
f
o
r
(
i
n
t
i
=
1
;
i
<
=
n
;
+
+
i
)
\quad \quad for(int\ i=1;i<=n;++i)
for(int i=1;i<=n;++i)
f
o
r
(
i
n
t
j
=
i
−
1
;
;
−
−
j
)
{
\quad \quad \quad for(int\ j=i-1;;--j)\{
for(int j=i−1;;−−j){
s
[
j
]
[
A
[
i
]
−
′
a
′
]
=
i
;
\quad \quad \quad \quad s[j][A[i]-'a']=i;
s[j][A[i]−′a′]=i;
i
f
(
A
[
j
]
=
=
A
[
i
]
∣
∣
!
j
)
b
r
e
a
k
;
\quad \quad \quad \quad if(A[j]==A[i]\ ||\ !j)\ break;
if(A[j]==A[i] ∣∣ !j) break;
}
\quad \quad \quad \}
}
}
\quad \}
}
}
\}
}
n
a
m
e
s
p
a
c
e
B
A
M
{
namespace\ BAM\{
namespace BAM{
i
n
t
s
[
N
]
[
26
]
;
\quad int\ s[N][26];
int s[N][26];
i
n
l
i
n
e
v
o
i
d
b
u
i
l
d
(
)
{
\quad inline\ void\ build()\{
inline void build(){
f
o
r
(
i
n
t
i
=
1
;
i
<
=
m
;
+
+
i
)
\quad \quad for(int\ i=1;i<=m;++i)
for(int i=1;i<=m;++i)
f
o
r
(
i
n
t
j
=
i
−
1
;
;
−
−
j
)
{
\quad \quad \quad for(int\ j=i-1;;--j)\{
for(int j=i−1;;−−j){
s
[
j
]
[
B
[
i
]
−
′
a
′
]
=
i
;
\quad \quad \quad \quad s[j][B[i]-'a']=i;
s[j][B[i]−′a′]=i;
i
f
(
B
[
j
]
=
=
B
[
i
]
∣
∣
!
j
)
b
r
e
a
k
;
\quad \quad \quad \quad if(B[j]==B[i]\ ||\ !j)\ break;
if(B[j]==B[i] ∣∣ !j) break;
}
\quad \quad \quad \}
}
}
\quad \}
}
}
\}
}
i
n
l
i
n
e
v
o
i
d
d
f
s
(
i
n
t
x
,
i
n
t
y
,
i
n
t
d
)
{
inline\ void\ dfs(int\ x,int\ y,int\ d)\{
inline void dfs(int x,int y,int d){
f
o
r
(
i
n
t
i
=
0
;
i
<
26
;
+
+
i
)
\quad for(int\ i=0;i<26;++i)
for(int i=0;i<26;++i)
i
f
(
A
A
M
:
:
s
[
x
]
[
i
]
&
&
!
S
A
M
:
:
s
[
y
]
[
i
]
)
{
\quad \quad if(AAM::s[x][i]\&\&!SAM::s[y][i])\{
if(AAM::s[x][i]&&!SAM::s[y][i]){
∗
l
=
m
i
n
(
∗
l
,
d
+
1
)
;
r
e
t
u
r
n
;
\quad \quad \quad *l=min(*l,d+1);\ return;
∗l=min(∗l,d+1); return;
}
\quad \quad \}
}
f
o
r
(
i
n
t
i
=
0
;
i
<
26
;
+
+
i
)
\quad for(int\ i=0;i<26;++i)
for(int i=0;i<26;++i)
i
f
(
A
A
M
:
:
s
[
x
]
[
i
]
&
&
S
A
M
:
:
s
[
y
]
[
i
]
)
\quad \quad if(AAM::s[x][i]\&\&SAM::s[y][i])
if(AAM::s[x][i]&&SAM::s[y][i])
d
f
s
(
A
A
M
:
:
s
[
x
]
[
i
]
,
S
A
M
:
:
s
[
y
]
[
i
]
,
d
+
1
)
;
\quad \quad \quad dfs(AAM::s[x][i],SAM::s[y][i],d+1);
dfs(AAM::s[x][i],SAM::s[y][i],d+1);
}
\}
}
i
n
t
m
a
i
n
(
)
{
int\ main()\{
int main(){
s
c
a
n
f
(
"
%
s
%
s
"
,
A
+
1
,
B
+
1
)
;
\quad scanf("\%s\%s",A+1,B+1);
scanf("%s%s",A+1,B+1);
n
=
s
t
r
l
e
n
(
A
+
1
)
;
m
=
s
t
r
l
e
n
(
B
+
1
)
;
\quad n=strlen(A+1);\ m=strlen(B+1);
n=strlen(A+1); m=strlen(B+1);
f
o
r
(
i
n
t
i
=
1
;
i
<
=
n
;
+
+
i
)
S
A
M
:
:
e
x
t
e
n
d
(
B
[
i
]
−
′
a
′
)
;
\quad for(int\ i=1;i<=n;++i)\ SAM::extend(B[i]-'a');
for(int i=1;i<=n;++i) SAM::extend(B[i]−′a′);
S
A
M
:
:
m
a
t
c
h
(
)
;
\quad SAM::match();
SAM::match();
A
A
M
:
:
b
u
i
l
d
(
)
;
B
A
M
:
:
b
u
i
l
d
(
)
;
\quad AAM::build();\ BAM::build();
AAM::build(); BAM::build();
∗
l
=
n
;
f
o
r
(
i
n
t
i
=
1
;
i
<
=
n
;
+
+
i
)
i
f
(
l
[
i
]
<
i
)
∗
l
=
m
i
n
(
∗
l
,
l
[
i
]
+
1
)
;
\quad *l=n;\ for(int\ i=1;i<=n;++i)\ if(l[i]<i)\ *l=min(*l,l[i]+1);
∗l=n; for(int i=1;i<=n;++i) if(l[i]<i) ∗l=min(∗l,l[i]+1);
p
r
i
n
t
f
(
"
%
d
\
n
"
,
∗
l
)
;
∗
l
=
n
;
\quad printf("\%d\backslash n",*l);\ *l=n;
printf("%d\n",∗l); ∗l=n;
f
o
r
(
i
n
t
i
=
1
;
i
<
=
n
;
+
+
i
)
{
\quad for(int\ i=1;i<=n;++i)\{
for(int i=1;i<=n;++i){
i
n
t
x
=
0
;
\quad \quad int\ x=0;
int x=0;
f
o
r
(
i
n
t
j
=
i
;
j
<
=
n
;
+
+
j
)
{
\quad \quad for(int\ j=i;j<=n;++j)\{
for(int j=i;j<=n;++j){
x
=
B
A
M
:
:
s
[
x
]
[
A
[
j
]
−
′
a
′
]
;
\quad \quad \quad x=BAM::s[x][A[j]-'a'];
x=BAM::s[x][A[j]−′a′];
i
f
(
!
x
)
{
∗
l
=
m
i
n
(
∗
l
,
j
−
i
+
1
)
;
b
r
e
a
k
;
}
\quad \quad \quad if(!x)\{\ *l=min(*l,j-i+1);\ break;\ \}
if(!x){ ∗l=min(∗l,j−i+1); break; }
}
\quad \quad \}
}
}
\quad \}
}
p
r
i
n
t
f
(
"
%
d
\
n
"
,
∗
l
)
;
d
f
s
(
0
,
1
,
0
)
;
\quad printf("\%d\backslash n",*l);\ dfs(0,1,0);
printf("%d\n",∗l); dfs(0,1,0);
p
r
i
n
t
f
(
"
%
d
\
n
"
,
∗
l
)
;
∗
l
=
n
;
\quad printf("\%d\backslash n",*l);\ *l=n;
printf("%d\n",∗l); ∗l=n;
q
.
p
u
s
h
(
(
V
)
{
0
,
0
,
0
}
)
;
\quad q.push((V)\{0,0,0\});
q.push((V){0,0,0});
f
o
r
(
i
n
t
x
,
y
,
d
;
!
q
.
e
m
p
t
y
(
)
;
q
.
p
o
p
(
)
)
{
\quad for(int\ x,y,d;!q.empty();q.pop())\{
for(int x,y,d;!q.empty();q.pop()){
z
=
q
.
f
r
o
n
t
(
)
;
\quad \quad z=q.front();
z=q.front();
f
o
r
(
i
n
t
i
=
0
;
i
<
26
;
+
+
i
)
i
f
(
x
=
A
A
M
:
:
s
[
z
.
x
]
[
i
]
)
{
\quad \quad for(int\ i=0;i<26;++i)if(x=AAM::s[z.x][i])\{
for(int i=0;i<26;++i)if(x=AAM::s[z.x][i]){
i
f
(
!
(
y
=
B
A
M
:
:
s
[
z
.
y
]
[
i
]
)
)
{
p
r
i
n
t
f
(
"
%
d
\
n
"
,
z
.
d
+
1
)
;
r
e
t
u
r
n
0
;
}
\quad \quad \quad if(!(y=BAM::s[z.y][i]))\{\ printf("\%d\backslash n",z.d+1);\ return\ 0;\ \}
if(!(y=BAM::s[z.y][i])){ printf("%d\n",z.d+1); return 0; }
e
l
s
e
i
f
(
!
f
[
x
]
[
y
]
)
{
f
[
x
]
[
y
]
=
1
;
q
.
p
u
s
h
(
(
V
)
{
x
,
y
,
z
.
d
+
1
}
)
;
}
\quad \quad \quad else\ if(!f[x][y])\{\ f[x][y]=1;\ q.push((V)\{x,y,z.d+1\});\ \}
else if(!f[x][y]){ f[x][y]=1; q.push((V){x,y,z.d+1}); }
}
\quad \quad \}
}
}
\quad \}
}
}
\}
}
#
i
n
c
l
u
d
e
<
c
s
t
d
i
o
>
\#include\ <cstdio>
#include <cstdio>
#
i
n
c
l
u
d
e
<
i
o
s
t
r
e
a
m
>
\#include\ <iostream>
#include <iostream>
#
i
n
c
l
u
d
e
<
a
l
g
o
r
i
t
h
m
>
\#include\ <algorithm>
#include <algorithm>
#
i
n
c
l
u
d
e
<
c
s
t
r
i
n
g
>
\#include\ <cstring>
#include <cstring>
#
i
n
c
l
u
d
e
<
c
s
t
r
i
n
g
>
\#include\ <cstring>
#include <cstring>
#
i
n
c
l
u
d
e
<
c
m
a
t
h
>
\#include\ <cmath>
#include <cmath>
t y p e d e f l o n g l o n g l l ; typedef\ long\ long\ ll; typedef long long ll;
i
n
t
p
o
w
e
r
m
o
d
(
i
n
t
a
,
i
n
t
e
x
p
,
i
n
t
m
o
d
e
r
)
int\ powermod(int\ a,\ int\ exp,\ int\ moder)
int powermod(int a, int exp, int moder)
{
\{
{
i
n
t
r
e
t
=
1
;
\ \ \ \ int\ ret\ =\ 1;
int ret = 1;
f
o
r
(
;
e
x
p
;
e
x
p
>
>
=
1
)
\ \ \ \ for\ (;\ exp;\ exp\ >>=\ 1)
for (; exp; exp >>= 1)
{
\ \ \ \ \{
{
i
f
(
e
x
p
&
1
)
\ \ \ \ \ \ \ \ if\ (exp\ \&\ 1)
if (exp & 1)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
=
1
l
l
∗
r
e
t
∗
a
%
m
o
d
e
r
;
\ \ \ \ \ \ \ \ \ \ \ \ ret\ =\ 1ll\ *\ ret\ *\ a\ \%\ moder;
ret = 1ll ∗ ret ∗ a % moder;
}
\ \ \ \ \ \ \ \ \}
}
a
=
1
l
l
∗
a
∗
a
%
m
o
d
e
r
;
\ \ \ \ \ \ \ \ a\ =\ 1ll\ *\ a\ *\ a\ \%\ moder;
a = 1ll ∗ a ∗ a % moder;
}
\ \ \ \ \}
}
r
e
t
u
r
n
r
e
t
;
\ \ \ \ return\ ret;
return ret;
}
\}
}
v
o
i
d
a
d
d
m
i
n
u
s
(
i
n
t
∗
a
,
i
n
t
∗
b
,
i
n
t
&
l
e
n
g
t
h
a
,
i
n
t
&
l
e
n
g
t
h
b
,
i
n
t
t
y
p
e
)
void\ addminus(int\ *a,\ int\ *b,\ int\ \&lengtha,\ int\ \&lengthb,\ int\ type)
void addminus(int ∗a, int ∗b, int &lengtha, int &lengthb, int type)
{
\{
{
i
n
t
l
e
n
g
t
h
=
s
t
d
:
:
m
a
x
(
l
e
n
g
t
h
a
,
l
e
n
g
t
h
b
)
;
\ \ \ \ int\ length\ =\ std::max(lengtha,\ lengthb);
int length = std::max(lengtha, lengthb);
f
o
r
(
i
n
t
i
=
0
;
i
<
l
e
n
g
t
h
;
+
+
i
)
\ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ length;\ ++i)
for (int i = 0; i < length; ++i)
{
\ \ \ \ \{
{
a
[
i
]
+
=
t
y
p
e
=
=
1
?
b
[
i
]
:
−
b
[
i
]
;
\ \ \ \ \ \ \ \ a[i]\ +=\ type\ ==\ 1\ ?\ b[i]\ :\ -b[i];
a[i] += type == 1 ? b[i] : −b[i];
a
[
i
]
>
=
10
?
(
a
[
i
]
−
=
10
,
+
+
a
[
i
+
1
]
)
:
a
[
i
]
<
0
?
(
a
[
i
]
+
=
10
,
−
−
a
[
i
+
1
]
)
:
0
;
\ \ \ \ \ \ \ \ a[i]\ >=\ 10\ ?\ (a[i]\ -=\ 10,\ ++a[i\ +\ 1])\ :\ a[i]\ <\ 0\ ?\ (a[i]\ +=\ 10,\ --a[i\ +\ 1])\ :\ 0;
a[i] >= 10 ? (a[i] −= 10, ++a[i + 1]) : a[i] < 0 ? (a[i] += 10, −−a[i + 1]) : 0;
}
\ \ \ \ \}
}
f
o
r
(
l
e
n
g
t
h
a
=
l
e
n
g
t
h
+
1
;
l
e
n
g
t
h
a
&
&
!
a
[
l
e
n
g
t
h
a
−
1
]
;
−
−
l
e
n
g
t
h
a
)
;
\ \ \ \ for\ (lengtha\ =\ length\ +\ 1;\ lengtha\ \&\&\ !a[lengtha\ -\ 1];\ --lengtha)\ ;
for (lengtha = length + 1; lengtha && !a[lengtha − 1]; −−lengtha) ;
}
\}
}
s
t
r
u
c
t
B
i
g
I
n
t
e
g
e
r
struct\ BigInteger
struct BigInteger
{
\{
{
c
o
n
s
t
s
t
a
t
i
c
i
n
t
M
A
X
N
=
19
;
\ \ \ \ const\ static\ int\ MAXN\ =\ 19;
const static int MAXN = 19;
c
o
n
s
t
s
t
a
t
i
c
i
n
t
M
O
D
=
(
119
<
<
23
)
+
1
;
/
/
998244353
\ \ \ \ const\ static\ int\ MOD\ =\ (119\ <<\ 23)\ +\ 1;\ \ \ //\ \ 998244353
const static int MOD = (119 << 23) + 1; // 998244353
c
o
n
s
t
s
t
a
t
i
c
i
n
t
r
o
o
t
=
3
;
\ \ \ \ const\ static\ int\ root\ =\ 3;
const static int root = 3;
c
o
n
s
t
s
t
a
t
i
c
i
n
t
i
n
v
r
o
o
t
=
332748118
;
\ \ \ \ const\ static\ int\ invroot\ =\ 332748118;
const static int invroot = 332748118;
i
n
t
a
[
1
<
<
M
A
X
N
]
;
\ \ \ \ int\ a[1\ <<\ MAXN];
int a[1 << MAXN];
i
n
t
l
e
n
g
t
h
,
s
i
g
;
\ \ \ \ int\ length,\ sig;
int length, sig;
B
i
g
I
n
t
e
g
e
r
(
)
\ \ \ \ BigInteger()
BigInteger()
{
\ \ \ \ \{
{
m
e
m
s
e
t
(
a
,
0
,
s
i
z
e
o
f
(
a
)
)
;
\ \ \ \ \ \ \ \ memset(a,\ 0,\ sizeof(a));
memset(a, 0, sizeof(a));
l
e
n
g
t
h
=
s
i
g
=
0
;
\ \ \ \ \ \ \ \ length\ =\ sig\ =\ 0;
length = sig = 0;
}
\ \ \ \ \}
}
v
o
i
d
c
l
e
a
r
(
)
\ \ \ \ void\ clear()
void clear()
{
\ \ \ \ \{
{
m
e
m
s
e
t
(
a
,
0
,
s
i
z
e
o
f
(
i
n
t
)
∗
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ memset(a,\ 0,\ sizeof(int)\ *\ length);
memset(a, 0, sizeof(int) ∗ length);
l
e
n
g
t
h
=
s
i
g
=
0
;
\ \ \ \ \ \ \ \ length\ =\ sig\ =\ 0;
length = sig = 0;
}
\ \ \ \ \}
}
v
o
i
d
r
e
a
d
(
)
\ \ \ \ void\ read()
void read()
{
\ \ \ \ \{
{
c
l
e
a
r
(
)
;
\ \ \ \ \ \ \ \ clear();
clear();
c
h
a
r
c
h
=
g
e
t
c
h
a
r
(
)
;
\ \ \ \ \ \ \ \ char\ ch\ =\ getchar();
char ch = getchar();
f
o
r
(
;
(
c
h
<
′
0
′
∣
∣
c
h
>
′
9
′
)
&
&
c
h
!
=
′
−
′
;
c
h
=
g
e
t
c
h
a
r
(
)
)
;
\ \ \ \ \ \ \ \ for\ (;\ (ch\ <\ '0'\ ||\ ch\ >\ '9')\ \&\&\ ch\ !=\ '-';\ ch\ =\ getchar())\ ;
for (; (ch < ′0′ ∣∣ ch > ′9′) && ch != ′−′; ch = getchar()) ;
c
h
=
=
′
−
′
?
(
s
i
g
=
−
1
,
c
h
=
g
e
t
c
h
a
r
(
)
)
:
s
i
g
=
1
;
\ \ \ \ \ \ \ \ ch\ ==\ '-'\ ?\ (sig\ =\ -1,\ ch\ =\ getchar())\ :\ sig\ =\ 1;
ch == ′−′ ? (sig = −1, ch = getchar()) : sig = 1;
f
o
r
(
;
c
h
>
=
′
0
′
&
&
c
h
<
=
′
9
′
;
c
h
=
g
e
t
c
h
a
r
(
)
)
\ \ \ \ \ \ \ \ for\ (;\ ch\ >=\ '0'\ \&\&\ ch\ <=\ '9';\ ch\ =\ getchar())
for (; ch >= ′0′ && ch <= ′9′; ch = getchar())
{
\ \ \ \ \ \ \ \ \{
{
a
[
l
e
n
g
t
h
+
+
]
=
c
h
−
′
0
′
;
\ \ \ \ \ \ \ \ \ \ \ \ a[length\ ++]\ =\ ch\ -\ '0';
a[length ++] = ch − ′0′;
}
\ \ \ \ \ \ \ \ \}
}
s
t
d
:
:
r
e
v
e
r
s
e
(
a
,
a
+
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ std::reverse(a,\ a\ +\ length);
std::reverse(a, a + length);
f
o
r
(
;
l
e
n
g
t
h
&
&
!
a
[
l
e
n
g
t
h
−
1
]
;
−
−
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ for\ (;\ length\ \&\&\ !a[length\ -\ 1];\ --length)\ ;
for (; length && !a[length − 1]; −−length) ;
s
i
g
=
l
e
n
g
t
h
?
s
i
g
:
0
;
\ \ \ \ \ \ \ \ sig\ =\ length\ ?\ sig\ :\ 0;
sig = length ? sig : 0;
}
\ \ \ \ \}
}
v
o
i
d
w
r
i
t
e
(
)
\ \ \ \ void\ write()
void write()
{
\ \ \ \ \{
{
i
f
(
!
s
i
g
)
\ \ \ \ \ \ \ \ if\ (!sig)
if (!sig)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
(
v
o
i
d
)
p
u
t
c
h
a
r
(
′
0
′
)
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ (void)putchar('0');
return (void)putchar(′0′);
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
s
i
g
<
0
)
\ \ \ \ \ \ \ \ if\ (sig\ <\ 0)
if (sig < 0)
{
\ \ \ \ \ \ \ \ \{
{
p
u
t
c
h
a
r
(
′
−
′
)
;
\ \ \ \ \ \ \ \ \ \ \ \ putchar('-');
putchar(′−′);
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
−
1
;
i
>
=
0
;
i
−
−
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ -\ 1;\ i\ >=\ 0;\ i--)
for (int i = length − 1; i >= 0; i−−)
{
\ \ \ \ \ \ \ \ \{
{
p
u
t
c
h
a
r
(
a
[
i
]
+
′
0
′
)
;
\ \ \ \ \ \ \ \ \ \ \ \ putchar(a[i]\ +\ '0');
putchar(a[i] + ′0′);
}
\ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \}
}
t
e
m
p
l
a
t
e
<
t
y
p
e
n
a
m
e
T
>
\ \ \ \ template\ <typename\ T>
template <typename T>
T
t
o
i
n
t
e
g
e
r
(
)
\ \ \ \ T\ tointeger()
T tointeger()
{
\ \ \ \ \{
{
T
r
e
t
=
0
;
\ \ \ \ \ \ \ \ T\ ret\ =\ 0;
T ret = 0;
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
−
1
;
i
>
=
0
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ -\ 1;\ i\ >=\ 0;\ ++i)
for (int i = length − 1; i >= 0; ++i)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
=
r
e
t
∗
10
+
a
[
i
]
;
\ \ \ \ \ \ \ \ \ \ \ \ ret\ =\ ret\ *\ 10\ +\ a[i];
ret = ret ∗ 10 + a[i];
}
\ \ \ \ \ \ \ \ \}
}
r
e
t
u
r
n
r
e
t
∗
s
i
g
;
\ \ \ \ \ \ \ \ return\ ret\ *\ sig;
return ret ∗ sig;
}
\ \ \ \ \}
}
b
o
o
l
e
q
u
a
l
(
c
o
n
s
t
B
i
g
I
n
t
e
g
e
r
&
p
)
\ \ \ \ bool\ equal(const\ BigInteger\ \&p)
bool equal(const BigInteger &p)
{
\ \ \ \ \{
{
i
f
(
s
i
g
!
=
p
.
s
i
g
∣
∣
l
e
n
g
t
h
!
=
p
.
l
e
n
g
t
h
)
\ \ \ \ \ \ \ \ if\ (sig\ !=\ p.sig\ ||\ length\ !=\ p.length)
if (sig != p.sig ∣∣ length != p.length)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
f
a
l
s
e
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ false;
return false;
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
i
n
t
i
=
0
;
i
<
l
e
n
g
t
h
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ length;\ ++i)
for (int i = 0; i < length; ++i)
{
\ \ \ \ \ \ \ \ \{
{
i
f
(
a
[
i
]
!
=
p
.
a
[
i
]
)
\ \ \ \ \ \ \ \ \ \ \ \ if\ (a[i]\ !=\ p.a[i])
if (a[i] != p.a[i])
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
f
a
l
s
e
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ false;
return false;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \ \ \ \ \}
}
r
e
t
u
r
n
t
r
u
e
;
\ \ \ \ \ \ \ \ return\ true;
return true;
}
\ \ \ \ \}
}
b
o
o
l
g
r
e
a
t
e
r
(
c
o
n
s
t
B
i
g
I
n
t
e
g
e
r
&
p
)
\ \ \ \ bool\ greater(const\ BigInteger\ \&p)
bool greater(const BigInteger &p)
{
\ \ \ \ \{
{
i
f
(
s
i
g
!
=
p
.
s
i
g
)
\ \ \ \ \ \ \ \ if\ (sig\ !=\ p.sig)
if (sig != p.sig)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
s
i
g
>
p
.
s
i
g
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ sig\ >\ p.sig;
return sig > p.sig;
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
l
e
n
g
t
h
!
=
p
.
l
e
n
g
t
h
)
\ \ \ \ \ \ \ \ if\ (length\ !=\ p.length)
if (length != p.length)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
l
e
n
g
t
h
>
p
.
l
e
n
g
t
h
∧
s
i
g
=
=
−
1
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ length\ >\ p.length\ \wedge \ sig\ ==\ -1;
return length > p.length ∧ sig == −1;
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
−
1
;
i
>
=
0
;
−
−
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ -\ 1;\ i\ >=\ 0;\ --i)
for (int i = length − 1; i >= 0; −−i)
{
\ \ \ \ \ \ \ \ \{
{
i
f
(
a
[
i
]
>
p
.
a
[
i
]
)
\ \ \ \ \ \ \ \ \ \ \ \ if\ (a[i]\ >\ p.a[i])
if (a[i] > p.a[i])
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
s
i
g
>
0
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ sig\ >\ 0;
return sig > 0;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
e
l
s
e
i
f
(
a
[
i
]
<
p
.
a
[
i
]
)
\ \ \ \ \ \ \ \ \ \ \ \ else\ if\ (a[i]\ <\ p.a[i])
else if (a[i] < p.a[i])
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
s
i
g
<
0
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ sig\ <\ 0;
return sig < 0;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \ \ \ \ \}
}
r
e
t
u
r
n
f
a
l
s
e
;
\ \ \ \ \ \ \ \ return\ false;
return false;
}
\ \ \ \ \}
}
v
o
i
d
l
e
f
t
s
h
i
f
t
(
i
n
t
d
i
s
)
\ \ \ \ void\ leftshift(int\ dis)
void leftshift(int dis)
{
\ \ \ \ \{
{
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
+
d
i
s
−
1
;
i
>
=
d
i
s
;
−
−
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ +\ dis\ -\ 1;\ i\ >=\ dis;\ --i)
for (int i = length + dis − 1; i >= dis; −−i)
{
\ \ \ \ \ \ \ \ \{
{
a
[
i
]
=
a
[
i
−
d
i
s
]
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ =\ a[i\ -\ dis];
a[i] = a[i − dis];
}
\ \ \ \ \ \ \ \ \}
}
m
e
m
s
e
t
(
a
,
0
,
s
i
z
e
o
f
(
i
n
t
)
∗
d
i
s
)
;
\ \ \ \ \ \ \ \ memset(a,\ 0,\ sizeof(int)\ *\ dis);
memset(a, 0, sizeof(int) ∗ dis);
l
e
n
g
t
h
+
=
d
i
s
;
\ \ \ \ \ \ \ \ length\ +=\ dis;
length += dis;
}
\ \ \ \ \}
}
v
o
i
d
r
i
g
h
t
s
h
i
f
t
(
i
n
t
d
i
s
)
\ \ \ \ void\ rightshift(int\ dis)
void rightshift(int dis)
{
\ \ \ \ \{
{
i
f
(
d
i
s
>
=
l
e
n
g
t
h
)
\ \ \ \ \ \ \ \ if\ (dis\ >=\ length)
if (dis >= length)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
c
l
e
a
r
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ clear();
return clear();
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
i
n
t
i
=
0
;
i
<
l
e
n
g
t
h
−
d
i
s
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ length\ -\ dis;\ ++i)
for (int i = 0; i < length − dis; ++i)
{
\ \ \ \ \ \ \ \ \{
{
a
[
i
]
=
a
[
i
+
d
i
s
]
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ =\ a[i\ +\ dis];
a[i] = a[i + dis];
}
\ \ \ \ \ \ \ \ \}
}
m
e
m
s
e
t
(
a
+
l
e
n
g
t
h
−
d
i
s
,
0
,
s
i
z
e
o
f
(
i
n
t
)
∗
d
i
s
)
;
\ \ \ \ \ \ \ \ memset(a\ +\ length\ -\ dis,\ 0,\ sizeof(int)\ *\ dis);
memset(a + length − dis, 0, sizeof(int) ∗ dis);
l
e
n
g
t
h
=
l
e
n
g
t
h
−
d
i
s
>
0
?
l
e
n
g
t
h
−
d
i
s
:
0
;
\ \ \ \ \ \ \ \ length\ =\ length\ -\ dis\ >\ 0\ ?\ length\ -\ dis\ :\ 0;
length = length − dis > 0 ? length − dis : 0;
}
\ \ \ \ \}
}
v
o
i
d
a
d
d
o
n
e
(
)
\ \ \ \ void\ addone()
void addone()
{
\ \ \ \ \{
{
s
i
g
>
=
0
?
+
+
a
[
0
]
:
−
−
a
[
0
]
;
\ \ \ \ \ \ \ \ sig\ >=\ 0\ ?\ ++\ a[0]\ :\ --a[0];
sig >= 0 ? ++ a[0] : −−a[0];
f
o
r
(
i
n
t
i
=
0
;
i
<
l
e
n
g
t
h
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ length;\ ++i)
for (int i = 0; i < length; ++i)
{
\ \ \ \ \ \ \ \ \{
{
i
f
(
a
[
i
]
<
10
&
&
a
[
i
]
>
=
0
)
\ \ \ \ \ \ \ \ \ \ \ \ if\ (a[i]\ <\ 10\ \&\&\ a[i]\ >=\ 0)
if (a[i] < 10 && a[i] >= 0)
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
b
r
e
a
k
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ break;
break;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
a
[
i
]
>
=
10
?
(
a
[
i
]
−
=
10
,
+
+
a
[
i
+
1
]
)
:
(
a
[
i
]
+
=
10
,
−
−
a
[
i
+
1
]
)
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ >=\ 10\ ?\ (a[i]\ -=\ 10,\ ++a[i\ +\ 1])\ :\ (a[i]\ +=\ 10,\ --a[i\ +\ 1]);
a[i] >= 10 ? (a[i] −= 10, ++a[i + 1]) : (a[i] += 10, −−a[i + 1]);
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
a
[
l
e
n
g
t
h
]
)
\ \ \ \ \ \ \ \ if\ (a[length])
if (a[length])
{
\ \ \ \ \ \ \ \ \{
{
+
+
l
e
n
g
t
h
;
\ \ \ \ \ \ \ \ \ \ \ \ ++length;
++length;
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
!
a
[
l
e
n
g
t
h
−
1
]
)
\ \ \ \ \ \ \ \ if\ (!a[length\ -\ 1])
if (!a[length − 1])
{
\ \ \ \ \ \ \ \ \{
{
−
−
l
e
n
g
t
h
;
\ \ \ \ \ \ \ \ \ \ \ \ --length;
−−length;
}
\ \ \ \ \ \ \ \ \}
}
s
i
g
=
l
e
n
g
t
h
?
(
s
i
g
>
=
0
?
1
:
−
1
)
:
0
;
\ \ \ \ \ \ \ \ sig\ =\ length\ ?\ (sig\ >=\ 0\ ?\ 1\ :\ -1)\ :\ 0;
sig = length ? (sig >= 0 ? 1 : −1) : 0;
}
\ \ \ \ \}
}
v
o
i
d
m
i
n
u
s
o
n
e
(
)
\ \ \ \ void\ minusone()
void minusone()
{
\ \ \ \ \{
{
s
i
g
=
−
s
i
g
;
\ \ \ \ \ \ \ \ sig\ =\ -sig;
sig = −sig;
a
d
d
o
n
e
(
)
;
\ \ \ \ \ \ \ \ addone();
addone();
s
i
g
=
−
s
i
g
;
\ \ \ \ \ \ \ \ sig\ =\ -sig;
sig = −sig;
}
\ \ \ \ \}
}
b
o
o
l
a
b
s
g
r
e
a
t
e
r
e
q
u
a
l
(
B
i
g
I
n
t
e
g
e
r
&
q
)
\ \ \ \ bool\ absgreaterequal(BigInteger\ \&q)
bool absgreaterequal(BigInteger &q)
{
\ \ \ \ \{
{
i
f
(
l
e
n
g
t
h
!
=
q
.
l
e
n
g
t
h
)
\ \ \ \ \ \ \ \ if\ (length\ !=\ q.length)
if (length != q.length)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
l
e
n
g
t
h
>
q
.
l
e
n
g
t
h
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ length\ >\ q.length;
return length > q.length;
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
−
1
;
i
>
=
0
;
−
−
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ -\ 1;\ i\ >=\ 0;\ --\ i)
for (int i = length − 1; i >= 0; −− i)
{
\ \ \ \ \ \ \ \ \{
{
i
f
(
a
[
i
]
>
q
.
a
[
i
]
)
\ \ \ \ \ \ \ \ \ \ \ \ if\ (a[i]\ >\ q.a[i])
if (a[i] > q.a[i])
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
t
r
u
e
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ true;
return true;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
i
f
(
a
[
i
]
<
q
.
a
[
i
]
)
\ \ \ \ \ \ \ \ \ \ \ \ if\ (a[i]\ <\ q.a[i])
if (a[i] < q.a[i])
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
f
a
l
s
e
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ return\ false;
return false;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \ \ \ \ \}
}
r
e
t
u
r
n
t
r
u
e
;
\ \ \ \ \ \ \ \ return\ true;
return true;
}
\ \ \ \ \}
}
v
o
i
d
a
b
s
(
)
\ \ \ \ void\ abs()
void abs()
{
\ \ \ \ \{
{
s
i
g
=
s
t
d
:
:
a
b
s
(
s
i
g
)
;
\ \ \ \ \ \ \ \ sig\ =\ std::abs(sig);
sig = std::abs(sig);
}
\ \ \ \ \}
}
v
o
i
d
n
e
g
(
)
\ \ \ \ void\ neg()
void neg()
{
\ \ \ \ \{
{
s
i
g
=
−
s
i
g
;
\ \ \ \ \ \ \ \ sig\ =\ -sig;
sig = −sig;
}
\ \ \ \ \}
}
v
o
i
d
a
s
s
i
g
n
(
B
i
g
I
n
t
e
g
e
r
&
q
)
\ \ \ \ void\ assign(BigInteger\ \&q)
void assign(BigInteger &q)
{
\ \ \ \ \{
{
m
e
m
s
e
t
(
a
,
0
,
s
i
z
e
o
f
(
i
n
t
)
∗
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ memset(a,\ 0,\ sizeof(int)\ *\ length);
memset(a, 0, sizeof(int) ∗ length);
m
e
m
c
p
y
(
a
,
q
.
a
,
s
i
z
e
o
f
(
i
n
t
)
∗
q
.
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ memcpy(a,\ q.a,\ sizeof(int)\ *\ q.length);
memcpy(a, q.a, sizeof(int) ∗ q.length);
l
e
n
g
t
h
=
q
.
l
e
n
g
t
h
;
\ \ \ \ \ \ \ \ length\ =\ q.length;
length = q.length;
s
i
g
=
q
.
s
i
g
;
\ \ \ \ \ \ \ \ sig\ =\ q.sig;
sig = q.sig;
}
\ \ \ \ \}
}
t
e
m
p
l
a
t
e
<
t
y
p
e
n
a
m
e
T
>
\ \ \ \ template\ <typename\ T>
template <typename T>
v
o
i
d
a
s
s
i
g
n
(
T
q
)
\ \ \ \ void\ assign(T\ q)
void assign(T q)
{
\ \ \ \ \{
{
m
e
m
s
e
t
(
a
,
0
,
s
i
z
e
o
f
(
i
n
t
)
∗
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ memset(a,\ 0,\ sizeof(int)\ *\ length);
memset(a, 0, sizeof(int) ∗ length);
i
f
(
!
q
)
\ \ \ \ \ \ \ \ if\ (!q)
if (!q)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
(
v
o
i
d
)
(
s
i
g
=
l
e
n
g
t
h
=
0
)
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ (void)(sig\ =\ length\ =\ 0);
return (void)(sig = length = 0);
}
\ \ \ \ \ \ \ \ \}
}
q
<
0
?
s
i
g
=
−
1
,
q
=
−
q
:
s
i
g
=
1
;
\ \ \ \ \ \ \ \ q\ <\ 0\ ?\ sig\ =\ -1,\ q\ =\ -q\ :\ sig\ =\ 1;
q < 0 ? sig = −1, q = −q : sig = 1;
l
e
n
g
t
h
=
0
;
\ \ \ \ \ \ \ \ length\ =\ 0;
length = 0;
f
o
r
(
;
q
;
q
/
=
10
)
\ \ \ \ \ \ \ \ for\ (;\ q;\ q\ /=\ 10)
for (; q; q /= 10)
{
\ \ \ \ \ \ \ \ \{
{
a
[
l
e
n
g
t
h
+
+
]
=
q
%
10
;
\ \ \ \ \ \ \ \ \ \ \ \ a[length++]\ =\ q\ \%\ 10;
a[length++] = q % 10;
}
\ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \}
}
v
o
i
d
a
d
d
(
B
i
g
I
n
t
e
g
e
r
&
q
)
\ \ \ \ void\ add(BigInteger\ \&q)
void add(BigInteger &q)
{
\ \ \ \ \{
{
s
t
a
t
i
c
B
i
g
I
n
t
e
g
e
r
a
u
x
;
\ \ \ \ \ \ \ \ static\ BigInteger\ aux;
static BigInteger aux;
i
f
(
!
q
.
s
i
g
)
\ \ \ \ \ \ \ \ if\ (!q.sig)
if (!q.sig)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
!
s
i
g
)
\ \ \ \ \ \ \ \ if\ (!sig)
if (!sig)
{
\ \ \ \ \ \ \ \ \{
{
a
s
s
i
g
n
(
q
)
;
\ \ \ \ \ \ \ \ \ \ \ \ assign(q);
assign(q);
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
s
i
g
=
=
q
.
s
i
g
)
\ \ \ \ \ \ \ \ if\ (sig\ ==\ q.sig)
if (sig == q.sig)
{
\ \ \ \ \ \ \ \ \{
{
a
d
d
m
i
n
u
s
(
a
,
q
.
a
,
l
e
n
g
t
h
,
q
.
l
e
n
g
t
h
,
1
)
;
\ \ \ \ \ \ \ \ \ \ \ \ addminus(a,\ q.a,\ length,\ q.length,\ 1);
addminus(a, q.a, length, q.length, 1);
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
a
b
s
g
r
e
a
t
e
r
e
q
u
a
l
(
q
)
)
\ \ \ \ \ \ \ \ if\ (absgreaterequal(q))
if (absgreaterequal(q))
{
\ \ \ \ \ \ \ \ \{
{
a
d
d
m
i
n
u
s
(
a
,
q
.
a
,
l
e
n
g
t
h
,
q
.
l
e
n
g
t
h
,
−
1
)
;
\ \ \ \ \ \ \ \ \ \ \ \ addminus(a,\ q.a,\ length,\ q.length,\ -1);
addminus(a, q.a, length, q.length, −1);
s
i
g
=
l
e
n
g
t
h
?
s
i
g
:
0
;
\ \ \ \ \ \ \ \ \ \ \ \ sig\ =\ length\ ?\ sig\ :\ 0;
sig = length ? sig : 0;
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
a
u
x
.
a
s
s
i
g
n
(
q
)
;
\ \ \ \ \ \ \ \ aux.assign(q);
aux.assign(q);
a
d
d
m
i
n
u
s
(
q
.
a
,
a
,
q
.
l
e
n
g
t
h
,
l
e
n
g
t
h
,
−
1
)
;
\ \ \ \ \ \ \ \ addminus(q.a,\ a,\ q.length,\ length,\ -1);
addminus(q.a, a, q.length, length, −1);
a
s
s
i
g
n
(
q
)
;
\ \ \ \ \ \ \ \ assign(q);
assign(q);
q
.
a
s
s
i
g
n
(
a
u
x
)
;
\ \ \ \ \ \ \ \ q.assign(aux);
q.assign(aux);
}
\ \ \ \ \}
}
v
o
i
d
m
i
n
u
s
(
B
i
g
I
n
t
e
g
e
r
&
q
)
\ \ \ \ void\ minus(BigInteger\ \&q)
void minus(BigInteger &q)
{
\ \ \ \ \{
{
q
.
n
e
g
(
)
;
\ \ \ \ \ \ \ \ q.neg();
q.neg();
a
d
d
(
q
)
;
\ \ \ \ \ \ \ \ add(q);
add(q);
q
.
n
e
g
(
)
;
\ \ \ \ \ \ \ \ q.neg();
q.neg();
}
\ \ \ \ \}
}
v
o
i
d
N
T
T
(
i
n
t
∗
a
,
i
n
t
l
e
n
g
t
h
,
i
n
t
t
y
p
e
)
\ \ \ \ void\ NTT(int\ *a,\ int\ length,\ int\ type)
void NTT(int ∗a, int length, int type)
{
\ \ \ \ \{
{
i
n
t
l
e
n
=
−
1
;
\ \ \ \ \ \ \ \ int\ len\ =\ -1;
int len = −1;
f
o
r
(
i
n
t
x
=
l
e
n
g
t
h
;
x
;
+
+
l
e
n
,
x
>
>
=
1
)
;
\ \ \ \ \ \ \ \ for\ (int\ x\ =\ length;\ x;\ ++len,\ x\ >>=\ 1)\ ;
for (int x = length; x; ++len, x >>= 1) ;
f
o
r
(
i
n
t
i
=
1
,
j
=
0
;
i
<
l
e
n
g
t
h
−
1
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 1,\ j\ =\ 0;\ i\ <\ length\ -\ 1;\ ++i)
for (int i = 1, j = 0; i < length − 1; ++i)
{
\ \ \ \ \ \ \ \ \{
{
f
o
r
(
i
n
t
s
=
l
e
n
g
t
h
;
j
∧
=
s
>
>
=
1
,
j
&
s
;
)
;
\ \ \ \ \ \ \ \ \ \ \ \ for\ (int\ s\ =\ length;\ j\ \wedge =\ s\ >>=\ 1,\ ~j\ \&\ s;\ )\ ;
for (int s = length; j ∧= s >>= 1, j & s; ) ;
i
f
(
i
<
j
)
\ \ \ \ \ \ \ \ \ \ \ \ if\ (i\ <\ j)
if (i < j)
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
s
t
d
:
:
s
w
a
p
(
a
[
i
]
,
a
[
j
]
)
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ std::swap(a[i],\ a[j]);
std::swap(a[i], a[j]);
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
i
n
t
i
=
1
;
i
<
=
l
e
n
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 1;\ i\ <=\ len;\ ++\ i)
for (int i = 1; i <= len; ++ i)
{
\ \ \ \ \ \ \ \ \{
{
i
n
t
u
n
i
t
=
p
o
w
e
r
m
o
d
(
t
y
p
e
=
=
1
?
r
o
o
t
:
i
n
v
r
o
o
t
,
(
M
O
D
−
1
)
>
>
i
,
M
O
D
)
,
s
z
k
=
1
<
<
(
i
−
1
)
;
\ \ \ \ \ \ \ \ \ \ \ \ int\ unit\ =\ powermod(type\ ==\ 1\ ?\ root\ :\ invroot,\ (MOD\ -\ 1)\ >>\ i,\ MOD),\ szk\ =\ 1\ <<\ (i\ -\ 1);
int unit = powermod(type == 1 ? root : invroot, (MOD − 1) >> i, MOD), szk = 1 << (i − 1);
f
o
r
(
i
n
t
j
=
0
;
j
<
l
e
n
g
t
h
;
j
+
=
1
<
<
i
)
\ \ \ \ \ \ \ \ \ \ \ \ for\ (int\ j\ =\ 0;\ j\ <\ length;\ j\ +=\ 1\ <<\ i)
for (int j = 0; j < length; j += 1 << i)
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
f
o
r
(
i
n
t
k
=
j
,
w
=
1
;
k
<
j
+
s
z
k
;
+
+
k
)
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ for\ (int\ k\ =\ j,\ w\ =\ 1;\ k\ <\ j\ +\ szk;\ ++k)
for (int k = j, w = 1; k < j + szk; ++k)
{
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \{
{
i
n
t
s
=
a
[
k
]
,
t
=
1
l
l
∗
w
∗
a
[
k
+
s
z
k
]
%
M
O
D
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ int\ s\ =\ a[k],\ t\ =\ 1ll\ *\ w\ *\ a[k\ +\ szk]\ \%\ MOD;
int s = a[k], t = 1ll ∗ w ∗ a[k + szk] % MOD;
a
[
k
]
=
s
+
t
>
=
M
O
D
?
s
+
t
−
M
O
D
:
s
+
t
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ a[k]\ =\ s\ +\ t\ >=\ MOD\ ?\ s\ +\ t\ -\ MOD\ :\ s\ +\ t;
a[k] = s + t >= MOD ? s + t − MOD : s + t;
a
[
k
+
s
z
k
]
=
s
−
t
<
0
?
s
−
t
+
M
O
D
:
s
−
t
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ a[k\ +\ szk]\ =\ s\ -\ t\ <\ 0\ ?\ s\ -\ t\ +\ MOD\ :\ s\ -\ t;
a[k + szk] = s − t < 0 ? s − t + MOD : s − t;
w
=
1
l
l
∗
w
∗
u
n
i
t
%
M
O
D
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ w\ =\ 1ll\ *\ w\ *\ unit\ \%\ MOD;
w = 1ll ∗ w ∗ unit % MOD;
}
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
t
y
p
e
=
=
1
)
\ \ \ \ \ \ \ \ if\ (type\ ==\ 1)
if (type == 1)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
i
n
t
i
n
v
=
p
o
w
e
r
m
o
d
(
l
e
n
g
t
h
,
M
O
D
−
2
,
M
O
D
)
;
\ \ \ \ \ \ \ \ int\ inv\ =\ powermod(length,\ MOD\ -\ 2,\ MOD);
int inv = powermod(length, MOD − 2, MOD);
f
o
r
(
i
n
t
i
=
0
;
i
<
l
e
n
g
t
h
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ length;\ ++\ i)
for (int i = 0; i < length; ++ i)
{
\ \ \ \ \ \ \ \ \{
{
a
[
i
]
=
1
l
l
∗
a
[
i
]
∗
i
n
v
%
M
O
D
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ =\ 1ll\ *\ a[i]\ *\ inv\ \%\ MOD;
a[i] = 1ll ∗ a[i] ∗ inv % MOD;
}
\ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \}
}
v
o
i
d
m
u
l
t
(
B
i
g
I
n
t
e
g
e
r
&
q
)
\ \ \ \ void\ mult(BigInteger\ \&q)
void mult(BigInteger &q)
{
\ \ \ \ \{
{
s
t
a
t
i
c
i
n
t
a
u
x
[
1
<
<
M
A
X
N
]
;
\ \ \ \ \ \ \ \ static\ int\ aux[1\ <<\ MAXN];
static int aux[1 << MAXN];
i
f
(
!
s
i
g
∣
∣
!
q
.
s
i
g
)
\ \ \ \ \ \ \ \ if\ (!sig\ ||\ !q.sig)
if (!sig ∣∣ !q.sig)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
c
l
e
a
r
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ clear();
return clear();
}
\ \ \ \ \ \ \ \ \}
}
i
n
t
n
=
l
e
n
g
t
h
+
q
.
l
e
n
g
t
h
;
\ \ \ \ \ \ \ \ int\ n\ =\ length\ +\ q.length;
int n = length + q.length;
i
n
t
l
e
n
g
t
h
a
n
s
=
1
;
\ \ \ \ \ \ \ \ int\ lengthans\ =\ 1;
int lengthans = 1;
f
o
r
(
;
l
e
n
g
t
h
a
n
s
<
n
;
l
e
n
g
t
h
a
n
s
<
<
=
1
)
;
\ \ \ \ \ \ \ \ for\ (;\ lengthans\ <\ n;\ lengthans\ <<=\ 1)\ ;
for (; lengthans < n; lengthans <<= 1) ;
m
e
m
c
p
y
(
a
u
x
,
q
.
a
,
s
i
z
e
o
f
(
i
n
t
)
∗
l
e
n
g
t
h
a
n
s
)
;
\ \ \ \ \ \ \ \ memcpy(aux,\ q.a,\ sizeof(int)\ *\ lengthans);
memcpy(aux, q.a, sizeof(int) ∗ lengthans);
N
T
T
(
a
,
l
e
n
g
t
h
a
n
s
,
1
)
;
\ \ \ \ \ \ \ \ NTT(a,\ lengthans,\ 1);
NTT(a, lengthans, 1);
N
T
T
(
a
u
x
,
l
e
n
g
t
h
a
n
s
,
1
)
;
\ \ \ \ \ \ \ \ NTT(aux,\ lengthans,\ 1);
NTT(aux, lengthans, 1);
f
o
r
(
i
n
t
i
=
0
;
i
<
l
e
n
g
t
h
a
n
s
;
i
+
+
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ lengthans;\ i++)
for (int i = 0; i < lengthans; i++)
{
\ \ \ \ \ \ \ \ \{
{
a
[
i
]
=
1
l
l
∗
a
[
i
]
∗
a
u
x
[
i
]
%
M
O
D
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ =\ 1ll\ *\ a[i]\ *\ aux[i]\ \%\ MOD;
a[i] = 1ll ∗ a[i] ∗ aux[i] % MOD;
}
\ \ \ \ \ \ \ \ \}
}
N
T
T
(
a
,
l
e
n
g
t
h
a
n
s
,
−
1
)
;
\ \ \ \ \ \ \ \ NTT(a,\ lengthans,\ -1);
NTT(a, lengthans, −1);
f
o
r
(
i
n
t
i
=
0
;
i
<
n
−
1
;
i
+
+
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ n\ -\ 1;\ i++)
for (int i = 0; i < n − 1; i++)
{
\ \ \ \ \ \ \ \ \{
{
a
[
i
+
1
]
+
=
a
[
i
]
/
10
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i\ +\ 1]\ +=\ a[i]\ /\ 10;
a[i + 1] += a[i] / 10;
a
[
i
]
%
=
10
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ \%=\ 10;
a[i] %= 10;
}
\ \ \ \ \ \ \ \ \}
}
l
e
n
g
t
h
=
n
;
\ \ \ \ \ \ \ \ length\ =\ n;
length = n;
f
o
r
(
;
l
e
n
g
t
h
&
&
!
a
[
l
e
n
g
t
h
−
1
]
;
−
−
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ for\ (;\ length\ \&\&\ !a[length\ -\ 1];\ --length)\ ;
for (; length && !a[length − 1]; −−length) ;
s
i
g
∗
=
q
.
s
i
g
;
\ \ \ \ \ \ \ \ sig\ *=\ q.sig;
sig ∗= q.sig;
}
\ \ \ \ \}
}
v
o
i
d
m
u
l
t
(
i
n
t
q
)
\ \ \ \ void\ mult(int\ q)
void mult(int q)
{
\ \ \ \ \{
{
i
f
(
!
q
∣
∣
!
s
i
g
)
\ \ \ \ \ \ \ \ if\ (!q\ ||\ !sig)
if (!q ∣∣ !sig)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
c
l
e
a
r
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ clear();
return clear();
}
\ \ \ \ \ \ \ \ \}
}
l
l
x
=
s
t
d
:
:
a
b
s
(
q
)
,
r
e
m
a
i
n
=
0
;
\ \ \ \ \ \ \ \ ll\ x\ =\ std::abs(q),\ remain\ =\ 0;
ll x = std::abs(q), remain = 0;
f
o
r
(
i
n
t
i
=
0
;
i
<
l
e
n
g
t
h
;
+
+
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ 0;\ i\ <\ length;\ ++i)
for (int i = 0; i < length; ++i)
{
\ \ \ \ \ \ \ \ \{
{
r
e
m
a
i
n
+
=
a
[
i
]
∗
x
;
\ \ \ \ \ \ \ \ \ \ \ \ remain\ +=\ a[i]\ *\ x;
remain += a[i] ∗ x;
a
[
i
]
=
r
e
m
a
i
n
%
10
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ =\ remain\ \%\ 10;
a[i] = remain % 10;
r
e
m
a
i
n
/
=
10
;
\ \ \ \ \ \ \ \ \ \ \ \ remain\ /=\ 10;
remain /= 10;
}
\ \ \ \ \ \ \ \ \}
}
a
[
l
e
n
g
t
h
]
=
(
i
n
t
)
r
e
m
a
i
n
;
\ \ \ \ \ \ \ \ a[length]\ =\ (int)remain;
a[length] = (int)remain;
f
o
r
(
;
a
[
l
e
n
g
t
h
]
;
+
+
l
e
n
g
t
h
)
\ \ \ \ \ \ \ \ for\ (;\ a[length];\ ++length)
for (; a[length]; ++length)
{
\ \ \ \ \ \ \ \ \{
{
a
[
l
e
n
g
t
h
+
1
]
=
a
[
l
e
n
g
t
h
]
/
10
;
\ \ \ \ \ \ \ \ \ \ \ \ a[length\ +\ 1]\ =\ a[length]\ /\ 10;
a[length + 1] = a[length] / 10;
a
[
l
e
n
g
t
h
]
%
=
10
;
\ \ \ \ \ \ \ \ \ \ \ \ a[length]\ \%=\ 10;
a[length] %= 10;
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
;
l
e
n
g
t
h
&
&
!
a
[
l
e
n
g
t
h
−
1
]
;
−
−
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ for\ (;\ length\ \&\&\ !a[length\ -\ 1];\ --length)\ ;
for (; length && !a[length − 1]; −−length) ;
s
i
g
∗
=
q
<
0
?
−
1
:
1
;
\ \ \ \ \ \ \ \ sig\ *=\ q\ <\ 0\ ?\ -1\ :\ 1;
sig ∗= q < 0 ? −1 : 1;
}
\ \ \ \ \}
}
v
o
i
d
p
o
w
e
r
(
i
n
t
e
x
p
)
\ \ \ \ void\ power(int\ exp)
void power(int exp)
{
\ \ \ \ \{
{
s
t
a
t
i
c
B
i
g
I
n
t
e
g
e
r
a
u
x
;
\ \ \ \ \ \ \ \ static\ BigInteger\ aux;
static BigInteger aux;
i
f
(
!
s
i
g
)
\ \ \ \ \ \ \ \ if\ (!sig)
if (!sig)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
a
u
x
.
a
s
s
i
g
n
<
i
n
t
>
(
1
)
;
\ \ \ \ \ \ \ \ aux.assign<int>\ (1);
aux.assign<int> (1);
f
o
r
(
;
e
x
p
;
e
x
p
>
>
=
1
)
\ \ \ \ \ \ \ \ for\ (;\ exp;\ exp\ >>=\ 1)
for (; exp; exp >>= 1)
{
\ \ \ \ \ \ \ \ \{
{
i
f
(
e
x
p
&
1
)
\ \ \ \ \ \ \ \ \ \ \ \ if\ (exp\ \&\ 1)
if (exp & 1)
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
a
u
x
.
m
u
l
t
(
∗
t
h
i
s
)
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ aux.mult(*this);
aux.mult(∗this);
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
a
u
x
.
m
u
l
t
(
a
u
x
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux.mult(aux);
aux.mult(aux);
}
\ \ \ \ \ \ \ \ \}
}
a
s
s
i
g
n
(
a
u
x
)
;
\ \ \ \ \ \ \ \ assign(aux);
assign(aux);
}
\ \ \ \ \}
}
v
o
i
d
d
i
v
i
d
e
(
B
i
g
I
n
t
e
g
e
r
&
q
)
\ \ \ \ void\ divide(BigInteger\ \&q)
void divide(BigInteger &q)
{
\ \ \ \ \{
{
s
t
a
t
i
c
B
i
g
I
n
t
e
g
e
r
a
u
x
,
a
u
x
1
;
\ \ \ \ \ \ \ \ static\ BigInteger\ aux,\ aux1;
static BigInteger aux, aux1;
i
f
(
!
s
i
g
∣
∣
!
q
.
s
i
g
)
\ \ \ \ \ \ \ \ if\ (!sig\ ||\ !q.sig)
if (!sig ∣∣ !q.sig)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
l
e
n
g
t
h
<
q
.
l
e
n
g
t
h
)
\ \ \ \ \ \ \ \ if\ (length\ <\ q.length)
if (length < q.length)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
c
l
e
a
r
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ clear();
return clear();
}
\ \ \ \ \ \ \ \ \}
}
b
o
o
l
n
e
g
1
=
s
i
g
=
=
1
,
n
e
g
2
=
q
.
s
i
g
=
=
1
;
\ \ \ \ \ \ \ \ bool\ neg1\ =\ sig\ ==\ 1,\ neg2\ =\ q.sig\ ==\ 1;
bool neg1 = sig == 1, neg2 = q.sig == 1;
a
b
s
(
)
,
q
.
a
b
s
(
)
;
\ \ \ \ \ \ \ \ abs(),\ q.abs();
abs(), q.abs();
i
n
t
n
u
m
=
0
;
\ \ \ \ \ \ \ \ int\ num\ =\ 0;
int num = 0;
f
o
r
(
i
n
t
i
=
q
.
l
e
n
g
t
h
−
1
;
i
>
=
q
.
l
e
n
g
t
h
−
3
;
−
−
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ q.length\ -\ 1;\ i\ >=\ q.length\ -\ 3;\ --i)
for (int i = q.length − 1; i >= q.length − 3; −−i)
{
\ \ \ \ \ \ \ \ \{
{
(
n
u
m
∗
=
10
)
+
=
i
>
=
0
?
q
.
a
[
i
]
:
0
;
\ \ \ \ \ \ \ \ \ \ \ \ (num\ *=\ 10)\ +=\ i\ >=\ 0\ ?\ q.a[i]\ :\ 0;
(num ∗= 10) += i >= 0 ? q.a[i] : 0;
}
\ \ \ \ \ \ \ \ \}
}
n
u
m
=
100000
/
n
u
m
;
\ \ \ \ \ \ \ \ num\ =\ 100000\ /\ num;
num = 100000 / num;
i
n
t
n
o
w
p
r
e
c
i
s
i
o
n
=
1
;
\ \ \ \ \ \ \ \ int\ nowprecision\ =\ 1;
int nowprecision = 1;
a
u
x
.
a
s
s
i
g
n
<
i
n
t
>
(
n
u
m
)
;
\ \ \ \ \ \ \ \ aux.assign<int>\ (num);
aux.assign<int> (num);
f
o
r
(
;
n
o
w
p
r
e
c
i
s
i
o
n
<
=
l
e
n
g
t
h
−
q
.
l
e
n
g
t
h
;
n
o
w
p
r
e
c
i
s
i
o
n
<
<
=
1
)
\ \ \ \ \ \ \ \ for\ (;\ nowprecision\ <=\ length\ -\ q.length;\ nowprecision\ <<=\ 1)
for (; nowprecision <= length − q.length; nowprecision <<= 1)
{
\ \ \ \ \ \ \ \ \{
{
a
u
x
1.
c
l
e
a
r
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.clear();
aux1.clear();
a
u
x
1.
l
e
n
g
t
h
=
(
n
o
w
p
r
e
c
i
s
i
o
n
<
<
1
)
+
3
,
a
u
x
1.
s
i
g
=
1
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.length\ =\ (nowprecision\ <<\ 1)\ +\ 3,\ aux1.sig\ =\ 1;
aux1.length = (nowprecision << 1) + 3, aux1.sig = 1;
f
o
r
(
i
n
t
i
=
q
.
l
e
n
g
t
h
−
a
u
x
1.
l
e
n
g
t
h
;
i
<
q
.
l
e
n
g
t
h
;
+
+
i
)
\ \ \ \ \ \ \ \ \ \ \ \ for\ (int\ i\ =\ q.length\ -\ aux1.length;\ i\ <\ q.length;\ ++i)
for (int i = q.length − aux1.length; i < q.length; ++i)
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
a
u
x
1.
a
[
i
−
q
.
l
e
n
g
t
h
+
a
u
x
1.
l
e
n
g
t
h
]
=
i
>
=
0
?
q
.
a
[
i
]
:
0
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ aux1.a[i\ -\ q.length\ +\ aux1.length]\ =\ i\ >=\ 0\ ?\ q.a[i]\ :\ 0;
aux1.a[i − q.length + aux1.length] = i >= 0 ? q.a[i] : 0;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
a
u
x
1.
m
u
l
t
(
a
u
x
)
,
a
u
x
1.
r
i
g
h
t
s
h
i
f
t
(
n
o
w
p
r
e
c
i
s
i
o
n
+
2
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.mult(aux),\ aux1.rightshift(nowprecision\ +\ 2);
aux1.mult(aux), aux1.rightshift(nowprecision + 2);
a
u
x
1.
m
u
l
t
(
a
u
x
)
,
a
u
x
1.
r
i
g
h
t
s
h
i
f
t
(
n
o
w
p
r
e
c
i
s
i
o
n
+
2
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.mult(aux),\ aux1.rightshift(nowprecision\ +\ 2);
aux1.mult(aux), aux1.rightshift(nowprecision + 2);
a
u
x
.
m
u
l
t
(
2
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux.mult(2);
aux.mult(2);
a
u
x
.
l
e
f
t
s
h
i
f
t
(
n
o
w
p
r
e
c
i
s
i
o
n
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux.leftshift(nowprecision);
aux.leftshift(nowprecision);
a
u
x
.
m
i
n
u
s
(
a
u
x
1
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux.minus(aux1);
aux.minus(aux1);
}
\ \ \ \ \ \ \ \ \}
}
a
u
x
.
m
u
l
t
(
∗
t
h
i
s
)
;
\ \ \ \ \ \ \ \ aux.mult(*this);
aux.mult(∗this);
a
u
x
.
r
i
g
h
t
s
h
i
f
t
(
q
.
l
e
n
g
t
h
+
n
o
w
p
r
e
c
i
s
i
o
n
+
1
)
;
\ \ \ \ \ \ \ \ aux.rightshift(q.length\ +\ nowprecision\ +\ 1);
aux.rightshift(q.length + nowprecision + 1);
a
u
x
1.
a
s
s
i
g
n
(
a
u
x
)
;
\ \ \ \ \ \ \ \ aux1.assign(aux);
aux1.assign(aux);
a
u
x
1.
m
u
l
t
(
q
)
;
\ \ \ \ \ \ \ \ aux1.mult(q);
aux1.mult(q);
m
i
n
u
s
(
a
u
x
1
)
;
\ \ \ \ \ \ \ \ minus(aux1);
minus(aux1);
i
n
t
f
l
a
g
=
a
b
s
g
r
e
a
t
e
r
e
q
u
a
l
(
q
)
?
2
:
s
i
g
<
0
?
1
:
0
;
\ \ \ \ \ \ \ \ int\ flag\ =\ absgreaterequal(q)\ ?\ 2\ :\ sig\ <\ 0\ ?\ 1\ :\ 0;
int flag = absgreaterequal(q) ? 2 : sig < 0 ? 1 : 0;
a
s
s
i
g
n
(
a
u
x
)
;
\ \ \ \ \ \ \ \ assign(aux);
assign(aux);
i
f
(
f
l
a
g
)
\ \ \ \ \ \ \ \ if\ (flag)
if (flag)
{
\ \ \ \ \ \ \ \ \{
{
f
l
a
g
=
=
1
?
m
i
n
u
s
o
n
e
(
)
:
a
d
d
o
n
e
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ flag\ ==\ 1\ ?\ minusone()\ :\ addone();
flag == 1 ? minusone() : addone();
}
\ \ \ \ \ \ \ \ \}
}
i
f
(
!
n
e
g
2
)
\ \ \ \ \ \ \ \ if\ (!neg2)
if (!neg2)
{
\ \ \ \ \ \ \ \ \{
{
q
.
n
e
g
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ q.neg();
q.neg();
}
\ \ \ \ \ \ \ \ \}
}
s
i
g
∗
=
n
e
g
1
∧
n
e
g
2
?
−
1
:
1
;
\ \ \ \ \ \ \ \ sig\ *=\ neg1\ \wedge \ neg2\ ?\ -1\ :\ 1;
sig ∗= neg1 ∧ neg2 ? −1 : 1;
}
\ \ \ \ \}
}
i
n
t
d
i
v
i
d
e
(
i
n
t
q
)
\ \ \ \ int\ divide(int\ q)
int divide(int q)
{
\ \ \ \ \{
{
i
f
(
!
s
i
g
∣
∣
!
q
)
\ \ \ \ \ \ \ \ if\ (!sig\ ||\ !q)
if (!sig ∣∣ !q)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
0
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ 0;
return 0;
}
\ \ \ \ \ \ \ \ \}
}
l
l
r
e
m
a
i
n
=
0
,
x
=
s
t
d
:
:
a
b
s
(
q
)
;
\ \ \ \ \ \ \ \ ll\ remain\ =\ 0,\ x\ =\ std::abs(q);
ll remain = 0, x = std::abs(q);
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
−
1
;
i
>
=
0
;
−
−
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ -\ 1;\ i\ >=\ 0;\ --i)
for (int i = length − 1; i >= 0; −−i)
{
\ \ \ \ \ \ \ \ \{
{
r
e
m
a
i
n
=
r
e
m
a
i
n
∗
10
+
a
[
i
]
;
\ \ \ \ \ \ \ \ \ \ \ \ remain\ =\ remain\ *\ 10\ +\ a[i];
remain = remain ∗ 10 + a[i];
a
[
i
]
=
(
i
n
t
)
(
r
e
m
a
i
n
/
x
)
;
\ \ \ \ \ \ \ \ \ \ \ \ a[i]\ =\ (int)(remain\ /\ x);
a[i] = (int)(remain / x);
r
e
m
a
i
n
%
=
x
;
\ \ \ \ \ \ \ \ \ \ \ \ remain\ \%=\ x;
remain %= x;
}
\ \ \ \ \ \ \ \ \}
}
f
o
r
(
;
l
e
n
g
t
h
&
&
!
a
[
l
e
n
g
t
h
−
1
]
;
−
−
l
e
n
g
t
h
)
;
\ \ \ \ \ \ \ \ for\ (;\ length\ \&\&\ !a[length\ -\ 1];\ --length)\ ;
for (; length && !a[length − 1]; −−length) ;
r
e
m
a
i
n
∗
=
s
i
g
;
\ \ \ \ \ \ \ \ remain\ *=\ sig;
remain ∗= sig;
s
i
g
∗
=
q
<
0
?
−
1
:
1
;
\ \ \ \ \ \ \ \ sig\ *=\ q\ <\ 0\ ?\ -1\ :\ 1;
sig ∗= q < 0 ? −1 : 1;
i
f
(
!
l
e
n
g
t
h
)
\ \ \ \ \ \ \ \ if\ (!length)
if (!length)
{
\ \ \ \ \ \ \ \ \{
{
s
i
g
=
0
;
\ \ \ \ \ \ \ \ \ \ \ \ sig\ =\ 0;
sig = 0;
}
\ \ \ \ \ \ \ \ \}
}
r
e
t
u
r
n
(
i
n
t
)
r
e
m
a
i
n
;
\ \ \ \ \ \ \ \ return\ (int)remain;
return (int)remain;
}
\ \ \ \ \}
}
v
o
i
d
s
q
r
t
(
)
\ \ \ \ void\ sqrt()
void sqrt()
{
\ \ \ \ \{
{
s
t
a
t
i
c
B
i
g
I
n
t
e
g
e
r
a
u
x
,
a
u
x
1
,
a
u
x
2
;
\ \ \ \ \ \ \ \ static\ BigInteger\ aux,\ aux1,\ aux2;
static BigInteger aux, aux1, aux2;
i
f
(
s
i
g
<
=
0
)
\ \ \ \ \ \ \ \ if\ (sig\ <=\ 0)
if (sig <= 0)
{
\ \ \ \ \ \ \ \ \{
{
r
e
t
u
r
n
;
\ \ \ \ \ \ \ \ \ \ \ \ return\ ;
return ;
}
\ \ \ \ \ \ \ \ \}
}
i
n
t
n
u
m
=
0
;
\ \ \ \ \ \ \ \ int\ num\ =\ 0;
int num = 0;
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
−
1
;
i
>
=
l
e
n
g
t
h
−
8
;
−
−
i
)
\ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ -\ 1;\ i\ >=\ length\ -\ 8;\ --i)
for (int i = length − 1; i >= length − 8; −−i)
{
\ \ \ \ \ \ \ \ \{
{
(
n
u
m
∗
=
10
)
+
=
i
>
=
0
?
a
[
i
]
:
0
;
\ \ \ \ \ \ \ \ \ \ \ \ (num\ *=\ 10)\ +=\ i\ >=\ 0\ ?\ a[i]\ :\ 0;
(num ∗= 10) += i >= 0 ? a[i] : 0;
}
\ \ \ \ \ \ \ \ \}
}
l
l
x
=
l
e
n
g
t
h
&
1
?
10000000000000
l
l
:
100000000000000
l
l
;
\ \ \ \ \ \ \ \ ll\ x\ =\ length\ \&\ 1\ ?\ 10000000000000ll\ :\ 100000000000000ll;
ll x = length & 1 ? 10000000000000ll : 100000000000000ll;
n
u
m
=
s
t
d
:
:
s
q
r
t
(
1.0
∗
x
/
n
u
m
)
;
\ \ \ \ \ \ \ \ num\ =\ std::sqrt(1.0\ *\ x\ /\ num);
num = std::sqrt(1.0 ∗ x / num);
i
n
t
n
o
w
p
r
e
c
i
s
i
o
n
=
2
;
\ \ \ \ \ \ \ \ int\ nowprecision\ =\ 2;
int nowprecision = 2;
a
u
x
.
a
s
s
i
g
n
<
i
n
t
>
(
n
u
m
)
;
\ \ \ \ \ \ \ \ aux.assign<int>\ (num);
aux.assign<int> (num);
f
o
r
(
;
n
o
w
p
r
e
c
i
s
i
o
n
<
=
(
l
e
n
g
t
h
>
>
1
)
+
1
;
n
o
w
p
r
e
c
i
s
i
o
n
=
(
n
o
w
p
r
e
c
i
s
i
o
n
<
<
1
)
−
1
)
\ \ \ \ \ \ \ \ for\ (;\ nowprecision\ <=\ (length\ >>\ 1)\ +\ 1;\ nowprecision\ =\ (nowprecision\ <<\ 1)\ -\ 1)
for (; nowprecision <= (length >> 1) + 1; nowprecision = (nowprecision << 1) − 1)
{
\ \ \ \ \ \ \ \ \{
{
a
u
x
1.
c
l
e
a
r
(
)
,
a
u
x
2.
c
l
e
a
r
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.clear(),\ aux2.clear();
aux1.clear(), aux2.clear();
a
u
x
1.
l
e
n
g
t
h
=
(
n
o
w
p
r
e
c
i
s
i
o
n
<
<
1
)
+
1
+
(
l
e
n
g
t
h
&
1
)
,
a
u
x
1.
s
i
g
=
1
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.length\ =\ (nowprecision\ <<\ 1)\ +\ 1\ +\ (length\ \&\ 1),\ aux1.sig\ =\ 1;
aux1.length = (nowprecision << 1) + 1 + (length & 1), aux1.sig = 1;
f
o
r
(
i
n
t
i
=
l
e
n
g
t
h
−
a
u
x
1.
l
e
n
g
t
h
;
i
<
l
e
n
g
t
h
;
+
+
i
)
\ \ \ \ \ \ \ \ \ \ \ \ for\ (int\ i\ =\ length\ -\ aux1.length;\ i\ <\ length;\ ++i)
for (int i = length − aux1.length; i < length; ++i)
{
\ \ \ \ \ \ \ \ \ \ \ \ \{
{
a
u
x
1.
a
[
i
−
l
e
n
g
t
h
+
a
u
x
1.
l
e
n
g
t
h
]
=
i
>
=
0
?
a
[
i
]
:
0
;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ aux1.a[i\ -\ length\ +\ aux1.length]\ =\ i\ >=\ 0\ ?\ a[i]\ :\ 0;
aux1.a[i − length + aux1.length] = i >= 0 ? a[i] : 0;
}
\ \ \ \ \ \ \ \ \ \ \ \ \}
}
a
u
x
1.
m
u
l
t
(
a
u
x
)
,
a
u
x
1.
r
i
g
h
t
s
h
i
f
t
(
n
o
w
p
r
e
c
i
s
i
o
n
+
1
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.mult(aux),\ aux1.rightshift(nowprecision\ +\ 1);
aux1.mult(aux), aux1.rightshift(nowprecision + 1);
a
u
x
1.
m
u
l
t
(
a
u
x
)
,
a
u
x
1.
r
i
g
h
t
s
h
i
f
t
(
n
o
w
p
r
e
c
i
s
i
o
n
+
1
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.mult(aux),\ aux1.rightshift(nowprecision\ +\ 1);
aux1.mult(aux), aux1.rightshift(nowprecision + 1);
a
u
x
1.
d
i
v
i
d
e
(
2
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux1.divide(2);
aux1.divide(2);
a
u
x
2.
l
e
n
g
t
h
=
(
n
o
w
p
r
e
c
i
s
i
o
n
+
1
)
<
<
1
,
a
u
x
2.
s
i
g
=
1
;
\ \ \ \ \ \ \ \ \ \ \ \ aux2.length\ =\ (nowprecision\ +\ 1)\ <<\ 1,\ aux2.sig\ =\ 1;
aux2.length = (nowprecision + 1) << 1, aux2.sig = 1;
a
u
x
2.
a
[
a
u
x
2.
l
e
n
g
t
h
−
1
]
=
1
,
a
u
x
2.
a
[
a
u
x
2.
l
e
n
g
t
h
−
2
]
=
5
;
\ \ \ \ \ \ \ \ \ \ \ \ aux2.a[aux2.length\ -\ 1]\ =\ 1,\ aux2.a[aux2.length\ -\ 2]\ =\ 5;
aux2.a[aux2.length − 1] = 1, aux2.a[aux2.length − 2] = 5;
a
u
x
2.
m
i
n
u
s
(
a
u
x
1
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux2.minus(aux1);
aux2.minus(aux1);
a
u
x
.
m
u
l
t
(
a
u
x
2
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux.mult(aux2);
aux.mult(aux2);
a
u
x
.
r
i
g
h
t
s
h
i
f
t
(
n
o
w
p
r
e
c
i
s
i
o
n
+
2
)
;
\ \ \ \ \ \ \ \ \ \ \ \ aux.rightshift(nowprecision\ +\ 2);
aux.rightshift(nowprecision + 2);
}
\ \ \ \ \ \ \ \ \}
}
a
u
x
.
m
u
l
t
(
∗
t
h
i
s
)
;
\ \ \ \ \ \ \ \ aux.mult(*this);
aux.mult(∗this);
a
u
x
.
r
i
g
h
t
s
h
i
f
t
(
(
l
e
n
g
t
h
>
>
1
)
+
n
o
w
p
r
e
c
i
s
i
o
n
+
1
)
;
\ \ \ \ \ \ \ \ aux.rightshift((length\ >>\ 1)\ +\ nowprecision\ +\ 1);
aux.rightshift((length >> 1) + nowprecision + 1);
a
u
x
1.
a
s
s
i
g
n
(
a
u
x
)
;
\ \ \ \ \ \ \ \ aux1.assign(aux);
aux1.assign(aux);
a
u
x
1.
m
u
l
t
(
a
u
x
1
)
;
\ \ \ \ \ \ \ \ aux1.mult(aux1);
aux1.mult(aux1);
a
u
x
2.
a
s
s
i
g
n
(
∗
t
h
i
s
)
;
\ \ \ \ \ \ \ \ aux2.assign(*this);
aux2.assign(∗this);
a
u
x
2.
m
u
l
t
(
2
)
;
\ \ \ \ \ \ \ \ aux2.mult(2);
aux2.mult(2);
m
i
n
u
s
(
a
u
x
1
)
;
\ \ \ \ \ \ \ \ minus(aux1);
minus(aux1);
i
n
t
f
l
a
g
=
g
r
e
a
t
e
r
(
a
u
x
2
)
?
2
:
s
i
g
<
0
?
1
:
0
;
\ \ \ \ \ \ \ \ int\ flag\ =\ greater(aux2)\ ?\ 2\ :\ sig\ <\ 0\ ?\ 1\ :\ 0;
int flag = greater(aux2) ? 2 : sig < 0 ? 1 : 0;
a
s
s
i
g
n
(
a
u
x
)
;
\ \ \ \ \ \ \ \ assign(aux);
assign(aux);
i
f
(
f
l
a
g
)
\ \ \ \ \ \ \ \ if\ (flag)
if (flag)
{
\ \ \ \ \ \ \ \ \{
{
f
l
a
g
=
=
1
?
m
i
n
u
s
o
n
e
(
)
:
a
d
d
o
n
e
(
)
;
\ \ \ \ \ \ \ \ \ \ \ \ flag\ ==\ 1\ ?\ minusone()\ :\ addone();
flag == 1 ? minusone() : addone();
}
\ \ \ \ \ \ \ \ \}
}
}
\ \ \ \ \}
}
}
;
\};
};
B i g I n t e g e r a , b , c ; BigInteger\ a,\ b,\ c; BigInteger a, b, c;
i
n
t
m
a
i
n
(
)
int\ main()
int main()
{
\{
{
a
.
r
e
a
d
(
)
;
\ \ \ \ a.read();
a.read();
b
.
r
e
a
d
(
)
;
\ \ \ \ b.read();
b.read();
c
.
a
s
s
i
g
n
(
a
)
;
\ \ \ \ c.assign(a);
c.assign(a);
a
.
d
i
v
i
d
e
(
b
)
;
\ \ \ \ a.divide(b);
a.divide(b);
a
.
w
r
i
t
e
(
)
;
\ \ \ \ a.write();
a.write();
p
u
t
c
h
a
r
(
′
\
n
′
)
;
\ \ \ \ putchar('\backslash n');
putchar(′\n′);
a
.
m
u
l
t
(
b
)
;
\ \ \ \ a.mult(b);
a.mult(b);
c
.
m
i
n
u
s
(
a
)
;
\ \ \ \ c.minus(a);
c.minus(a);
c
.
w
r
i
t
e
(
)
;
\ \ \ \ c.write();
c.write();
p
u
t
c
h
a
r
(
′
\
n
′
)
;
\ \ \ \ putchar('\backslash n');
putchar(′\n′);
r
e
t
u
r
n
0
;
\ \ \ \ return\ 0;
return 0;
}
\}
}
$