AT_birthday0410_x/AT678
#include<bits/stdc++.h>
#define uint unsigned int
#define LL long long
#define ull unsigned long long
#define lll __int128
#define Rtype int
#define gc getchar
#define pc putchar
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define priq priority_queue
// #define DEBUG
// #define file
using namespace std;
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
//mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<Rtype>Random(1,n);
template<class T>T Abs(T a){return a<0?-a:a;}
template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;}
template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;}
template<class T,class ...T2>T mmin(T a,T2 ...b){return mmin(a,mmin(b...));}
template<class T,class ...T2>T mmax(T a,T2 ...b){return mmax(a,mmax(b...));}
template<class T>T gcd(T a,T b){if(!b||!a)return a+b;if(a<0)a=-a;while(b^=a^=b^=a%=b);return a;}
template<class T,class T2>T gcd(T a,T2 b){if(!b||!a)return a+b;if(a<0)a=-a;while(b^=a^=b^=a%=b);return a;}
typedef pair<int,int> pi;
typedef pair<int,pair<int,int>> pii;
void red(){}
template<class T1,class ...T2>
void red(T1 &x,T2&... rest)
{
x=0;bool f=0;char ch=gc();
while(ch<'0'||ch>'9')f|=ch=='-',ch=gc();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=gc();
x=f?-x:x;
red(rest...);
}
const int N=70,M=39,MAXW=9e3+5,INF=0x7f7f7f7f,Mod=1e9+7;
const double eps=1e-8,Pi=acos(-1.0);
/* -----------Initialize-------------- */
// Num : 65*38
// Data : (4~58)*(5~34)
// White : '.' Black : '#'
int T,W,H;
bool Ori_Map[N][MAXW],vis[N][MAXW],New[N][MAXW];
int dx[]={0,0,1,-1,1,1,-1,-1};
int dy[]={1,-1,0,0,1,-1,1,-1};
uniform_real_distribution<>RandM(0.9,1.0);
uniform_real_distribution<>RandR(-15.0,15.0);
uniform_real_distribution<>RandSx(-0.1,0.1);
double Randm(){return RandM(gen);}
double Randr(){return RandR(gen);}
double Randsx(){return RandSx(gen);}
//double Rand(double Le,double Ri){uniform_real_distribution<>Random(Le,Ri);return Random(gen);}
double To_int(double x){return (int)(x+0.5);}
char Num[]="0123456789()+-*/";
vector<char>s;
const int Split_X=5,Split_Y=5;
int Num_Data[16][66]={// 11*6
{0,0,31457280,15728640,0,0,16777216,33553296,33554431,33554431,33537249,1048576,32404248,33554431,1279,17311,33554431,15965283,33553374,7585263,0,0,30309342,33537519,33554431,7576807,0,0,30307228,33554431,32505855,15965415,0,0,32404380,16777215,30309342,33553903,1048576,16777216,33554398,7585263,17176,32505855,33554407,33554428,16777215,1123,0,540,31743,16383,39,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,15728640,0,0,25690112,33550336,33554424,33554431,0,0,17326872,4194303,30310399,33554431,0,0,0,0,30307228,33554431,0,0,0,0,30307228,33554431,0,0,0,0,30307228,33554431,0,0,0,0,30307228,33554431,0,0,30307072,33554400,33554428,33554431,33554400,16236000,796,1023,1023,1023,1023,495,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,32505856,15728640,0,0,30302720,33554428,33554431,33554431,33537249,0,30307228,16236543,63,17327007,33554431,3247137,25500,15855,0,25969168,33554431,1150051,0,0,25690112,33554396,16777215,33,0,25690112,33554396,16777215,1127,0,25690112,33554396,16777215,1127,30307224,16236007,32472028,33554431,33554407,33554400,33554428,16236015,926,1023,1023,1023,1023,495,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,32505856,32505856,0,0,25977344,33554430,33554431,33554431,33553891,3178496,25977624,33554431,63,17183,33554431,7576803,16,31,0,25690112,33554431,3251431,0,0,33554430,33554431,33538047,1048577,0,0,30,799,32505855,16227555,25165824,1048576,0,16777216,33553374,16236015,30372764,33554431,33554400,33554424,33554431,1150183,28,25599,32767,16383,111,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,29360128,3145728,0,0,0,30162944,33554430,7576807,0,0,25690112,33554396,33554431,7576807,0,16777216,33553308,3391487,32472031,7576807,0,33518360,33030143,32505891,33520606,32742631,32505856,33554431,33554431,33554431,33554431,33554431,33554431,0,0,0,33554431,7576807,0,0,25977344,33554400,33554431,33554407,16236000,0,536,1023,1023,1023,495,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,17318416,33554431,33554431,33554431,33554431,3247201,17318416,33554431,1082431,31,31,1,25977360,33554431,33547297,33553408,32736256,0,25977624,33554431,2097151,26214399,33554431,7572577,0,6,0,16,32472063,16236007,25690112,3145728,0,16777216,33553374,7847407,30372764,33554431,33554400,33554428,33554431,36071,24,17407,32767,16383,111,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,31457280,3145728,0,25690112,33553152,33554430,33554431,7576803,17301504,33554396,8388607,4095,127,3,30307096,16777215,25165859,32505856,1048576,0,32472028,33554415,33554430,33554431,33554415,7572512,32472030,16252927,39,16,30309375,33553903,30307230,33537519,1048576,0,33518492,16777215,16920,32505855,33554407,33554424,33554431,1154543,0,540,29695,32767,255,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,30307228,33554431,33554431,33554431,33554431,33554415,30307228,16236031,31,17301535,33554431,7585263,26112924,7847407,0,30302992,33554431,1082467,0,0,0,33553372,7847935,0,0,0,25969168,33554431,1150183,0,0,0,32469916,16252927,1,0,0,17301504,33554431,3251439,0,0,0,528,31743,1057,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,31457280,32505856,0,0,16777216,33553296,33554431,33554431,33553891,3178496,25977360,33554431,1084671,17183,33554431,7576675,17326872,33554431,7439393,25690112,33554431,3251431,0,33453023,33554431,33554431,33013759,1048577,32404240,16777215,127,799,32505855,16227555,32472030,33537519,0,0,33553374,16236015,17589148,33554431,33554403,33554424,33554431,1150183,0,927,32767,32767,255,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,32505856,15728640,0,0,25690112,33554392,33554431,33554431,33537249,1048576,32469916,16777215,1151,17183,32505855,7576673,32472030,33013231,0,0,33520606,16236007,17589148,33554431,33553504,33554192,33554431,16236015,0,17311,32767,3583,33520607,7577071,0,0,16777216,33513984,33554431,1084519,17317888,33554400,33554431,33554431,105983,0,528,32767,2047,39,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,33513984,33012960,0,0,0,33518352,33554431,1163263,0,0,30171664,33554431,1154559,0,0,0,33553372,7847935,0,0,0,17317888,33554431,3251431,0,0,0,17318416,33554431,3247203,0,0,0,16912,33554431,7572579,0,0,0,0,32472063,33537511,0,0,0,0,17327004,33554431,15961185,0,0,0,0,17593311,33554431,33528929,0,0,0,0,553951,2342911,0},
{0,33553280,33000480,0,0,0,0,17595391,33554431,33004641,0,0,0,0,17593311,33554431,7441440,0,0,0,0,30374911,33537511,0,0,0,0,25981852,33554431,1082368,0,0,0,25977624,33554431,1082401,0,0,0,30303000,33554431,1057,0,0,0,33553372,16236543,0,0,0,33452816,33554431,1084647,0,0,33551120,33554431,1154559,0,0,0,9337855,40447,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,30307224,16236007,0,0,0,0,30307228,16236015,0,0,30307228,33554431,33554431,33554431,33554431,16236015,924,1023,30307327,16236543,1023,495,0,0,30307228,16236015,0,0,0,0,25500,7663,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32472028,33554431,33554431,33554431,33554431,16236007,926,1023,1023,1023,1023,495,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,25165824,3145728,0,0,0,0,30374812,16236007,0,0,30302976,33537248,33354652,29072615,33553152,7576672,25500,819199,32505855,16777215,131071,3303,0,32469504,16252927,32472063,33004641,0,0,26115071,1154279,811932,3670015,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,32404248,16236003,0,0,0,17301504,33554430,1150183,0,0,0,30307096,16777215,1,0,0,16777216,33554398,3251439,0,0,0,30302992,16777215,33,0,0,0,33553374,3382511,0,0,0,30171664,33554431,1059,0,0,0,33553372,3382767,0,0,0,25969168,33554431,33891,0,0,0,30374812,7585279,0,0,0,0,24,7,0,0,0,0}
};
struct Dot{
double x,y;
Dot operator +(const Dot &a)const{
return (Dot){x+a.x,y+a.y};
}
Dot operator -(const Dot &a)const{
return (Dot){x-a.x,y-a.y};
}
Dot operator *(const Dot &a)const{
return (Dot){x*a.x-y*a.y,x*a.y+y*a.x};
}
Dot rotate(double deg)
{
return (*this)*(Dot){cos(deg),-sin(deg)};
}
};
struct Map{
int n,m;
vector<vector<bool>>v;
void Reset()
{
v.clear();
v.resize(n);
for(auto &V:v)V.resize(m);
}
Map(int n=65,int m=38):n(n),m(m){Reset();}
void Show()
{
for(auto row:v)
{
for(auto cur:row)
printf(cur?"#":".");
puts("");
}
}
void Init(vector<Dot>Dotlist)
{
double x=0,y=0;
int len=Dotlist.size();
for(auto &Point:Dotlist)Point.x=To_int(Point.x),Point.y=To_int(Point.y);
for(auto &Point:Dotlist)x+=Point.x,y+=Point.y;
double Delx=(2.0*x+len)/(2.0*len),Dely=(2.0*y+len)/(2.0*len);
for(auto &Point:Dotlist)Point.x-=Delx,Point.y-=Dely;
n=65;m=38;
Reset();
for(auto &Point:Dotlist)
{
double xx=To_int(Point.x+n/2.0),yy=To_int(Point.y+m/2.0);
if(xx<0||xx>=n||yy<0||yy>=m)continue;
v[xx][yy]=true;
}
}
double Match(Map &Obj)
{
double p=0,q=0;
for(int i=0;i<mmin(n,Obj.n);i++)
for(int j=0;j<mmin(m,Obj.m);j++)
q++,p+=(v[i][j]==Obj.v[i][j]);
return 1.0*p/q;
}
}Char[16];
vector<Map>Characters;
int n=65,m=38;
pi Pos(int i,int j)
{
int Addx=j/Split_Y+3,Addy=4+j%Split_Y;
int x=i/6*5,y=i%6*5;
return mp(Addx+x,Addy+y);
}
void Init_Char()
{
for(int i=0;i<16;i++)
{
vector<Dot>List;List.clear();
for(int j=0;j<66;j++)
for(int k=0;k<25;k++)//Le->Ri Up->Down
if((Num_Data[i][j]>>k)&1)
{
auto pos=Pos(j,k);
List.pb(Dot{(double)pos.fi,(double)pos.se});
}
Char[i].Init(List);
}
}
const int Sample_SUM=80;
vector<Map>Sample[16];
void RandomSample()
{
Init_Char();
Dot Del=Dot{65.0/2.0,38.0/2.0};
for(int cur=0;cur<16;cur++)
{
for(int t=1;t<=Sample_SUM;t++)
{
double M=Randm();
double Mh=Randm(),Mw=Randm();
double R,P,Sx,Sy;
if(T<30)
{
R=Randr();
Sx=Sy=P=0.0;
}
if(30<=T&&T<90)
{
R=Randr();
Sx=Randsx();Sy=Randsx();
P=0.05;
}
if(T>=90)
{
R=Randr();
Sx=Randsx();Sy=Randsx();
P=0.05;
}
vector<Dot>List;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(Char[cur].v[i][j])
{
Dot Point=(Dot){i+0.5,j+0.5};
Point=Point-Del;
Point.x*=M*Mh;
Point.y*=M*Mw;
Point=Point.rotate(R/180.0*Pi);
Dot pre=Point;
Point.x=pre.x+pre.y*Sy;
Point.y=pre.y+pre.x*Sx;
Point=Point+Del;
List.pb(Point);
}
Map Cur_Sample;
Cur_Sample.Init(List);
Sample[cur].pb(Cur_Sample);
}
}
}
/* -----------Analyze----------------- */
vector<pi>vis_List;
void dfs(int x,int y)
{
vis[x][y]=true;
vis_List.pb(x,y);
for(int i=0;i<4;i++)
{
int xx=x+dx[i],yy=y+dy[i];
if(Ori_Map[xx][yy]&&!vis[xx][yy])dfs(xx,yy);
}
}
void ReduceNoice_and_Split()
{
int Limit=20;
for(int i=1;i<=H;i++)
{
for(int j=1;j<=W;j++)
{
int p=0,q=0;
for(int k=0;k<8;k++)
{
int xx=i+dx[k],yy=j+dy[k];
if(xx<1||xx>H||yy<1||yy>W)continue;
if(Ori_Map[xx][yy])p++;
else q++;
}
New[i][j]=p>q;
}
}
for(int i=1;i<=H;i++)
for(int j=1;j<=W;j++)
Ori_Map[i][j]=New[i][j];
for(int j=1;j<=W;j++)
for(int i=1;i<=H;i++)
{
if(Ori_Map[i][j]&&!vis[i][j])
{
vis_List.clear();
dfs(i,j);
if((int)vis_List.size()>Limit)
{
vector<Dot>opt;
for(auto &Point:vis_List)opt.pb((Dot){double(Point.fi),double(Point.se)});
Map NewChar;
NewChar.Init(opt);
Characters.pb(NewChar);
}
}
}
}
void Analyze_Char()
{
for(auto cur:Characters)
{
int _find=-1;
double maxp=-1.0;
for(int i=0;i<16;i++)
{
double curp=0;
for(auto &Samp:Sample[i])curp=mmax(curp,cur.Match(Samp));
if(curp>maxp)maxp=curp,_find=i;
}
s.pb(Num[_find]);
}
}
/* -----------Calc Result------------- */
int Priority(char x)
{
if(x=='('||x==')')return 0;
if(x=='+'||x=='-')return 1;
if(x=='*'||x=='/')return 2;
return -1;
}
int Calc()
{
vector<char>Expression;
vector<char>Stack;
for(auto x:s)
{
if(x>='0'&&x<='9')Expression.pb(x);
else if(x=='(')Stack.pb(x);
else if(x==')')
{
while(Stack.back()!='(')Expression.pb(Stack.back()),Stack.pop_back();
Stack.pop_back();
}
else
{
while(!Stack.empty()&&(Priority(Stack.back())>=Priority(x)&&x!='^'))Expression.pb(Stack.back()),Stack.pop_back();
Stack.pb(x);
}
}
while(!Stack.empty())Expression.pb(Stack.back()),Stack.pop_back();
vector<int>cur;
for(auto x:Expression)
{
if(x>='0'&&x<='9')cur.pb(x-'0');
else
{
int a=cur[cur.size()-2],b=cur[cur.size()-1];
cur.pop_back();cur.pop_back();
if(x=='+')cur.pb(a+b);
if(x=='-')cur.pb(a-b);
if(x=='*')cur.pb(a*b);
if(x=='/')cur.pb(a/b);
}
}
return cur.back();
}
int main()
{
#ifdef file
freopen("AT678.in","r",stdin);
freopen("my.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin>>T>>W>>H;
char x;
for(int i=1;i<=H;i++)
for(int j=1;j<=W;j++)
cin>>x,Ori_Map[i][j]=(x=='#');
RandomSample();
ReduceNoice_and_Split();
Analyze_Char();
cout<<Calc()<<endl;
return 0;
}