提交时间:2024-02-29 15:06:43

运行 ID: 26985

#include<bits/stdc++.h> using namespace std; const int dx[]={0,0,-1,1}; const int dy[]={1,-1,0,0}; int n,m,mod,du[1005][405],c[105],tag[105],g[1<<16],v[1<<16]; vector<pair<int,int>>a,b; typedef unsigned long long u64; u64 f[2][1<<16]; inline u64 barret(u64 x){ // static unsigned __int128 ttt=(-1ull)/mod; // return x-((u64)(ttt*x>>64))*mod; } int main(){ // freopen("rainbow.in","r",stdin); // freopen("rainbow.out","w",stdout); cin>>n>>m>>mod; for(int i=0;i<=400;i++) for(int j=0;j<=400;j++) if(i*i+j*j<(n+m)*(n+m)&&i*i+j*j>=n*n) a.emplace_back(i,j); sort(a.begin(),a.end(),[&](auto X,auto Y){ int ax,ay,bx,by;tie(ax,ay)=X,tie(bx,by)=Y; int now=ax*by-bx*ay; return now==0?(ax*bx<0?ax>0:abs(ax)+ay<abs(bx)+by):now>0; }); for(int i=n;i<n+m;i++)du[400][i+1]++; for(auto &now:a){ now.first+=400;now.second++;int x,y;tie(x,y)=now; for(int i=0;i<4;i++)if(x!=400||dx[i]!=0)du[x+dx[i]][y+dy[i]]++; } int mx=1,cur=0,t=1;f[0][0]=1; for(auto now:a){ int x,y;tie(x,y)=now; for(int i=0;i<4;i++)if(x!=400||dx[i]!=0)du[x+dx[i]][y+dy[i]]--; auto tmp=b;b.clear(); int cnt=0; for(int i=0;i<tmp.size();i++){ if(du[tmp[i].first][tmp[i].second]>0){ c[i]=1<<(cnt++); b.push_back(tmp[i]); }else c[i]=0; } b.push_back(now); for(int i=0;i<tmp.size();i++){ if(abs(tmp[i].first-x)+abs(tmp[i].second-y)<=1&&(tmp[i].first!=x||x!=400))tag[i]=1; else tag[i]=0; } fill(f[cur^1],f[cur^1]+(2<<cnt),0); f[cur^1][1<<cnt]=f[cur^1][0]=!t?barret(f[cur][0]):f[cur][0]; g[0]=v[0]=0; for(int s=1;s<mx;s++){ int x=__builtin_ctz(s),gs=g[s]=g[s^(1<<x)]^c[x],vs=v[s]=v[s^(1<<x)]+tag[x]; u64 y=!t?barret(f[cur][s]):f[cur][s]; f[cur^1][gs]+=y;f[cur^1][gs^(1<<cnt)]+=y<<vs; }cur^=1;mx=2<<cnt;t=(t+1)&7; } int ans=0; for(int i=0;i<mx;i++){ f[cur][i]%=mod; int cnt=__builtin_popcount(i&(i<<1)); u64 now=f[cur][i]*f[cur][i]%mod; ans=(ans+(now<<cnt))%mod; } cout<<ans<<'\n'; return 0; }