Submission #631226

# Submission time Handle Problem Language Result Execution time Memory
631226 2022-08-17T21:04:20 Z inksamurai Osmosmjerka (COCI17_osmosmjerka) C++17
160 / 160
2311 ms 154804 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3phCa4T ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

//snuke's mod int
template <ll mod>
struct modint{
	ll x; // typedef long long ll;
	modint(ll x=0):x((x%mod+mod)%mod){}
	modint operator-()const{return modint(-x);}
	modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
	modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
	modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
	modint operator+(const modint a)const{modint res(*this); return res+=a;}
	modint operator-(const modint a)const{modint res(*this); return res-=a;}
	modint operator*(const modint a)const{modint res(*this); return res*=a;}
	modint pow(ll n)const{
		modint res=1,x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modint inv()const{return pow(mod-2);}
};

using mi0=modint<1000000007>;
using mi1=modint<805306457>;
const mi0 p0=50331653;
const mi1 p1=10007;

using qii=pair<mi0,mi1>;
const int m=30;
qii spr[m+1][250001];

signed main(){
_3phCa4T;
	int h,w,n;
	cin>>h>>w>>n;
	vec(vec(char)) a(h,vec(char)(w));
	rep(i,h){
		rep(j,w){
			cin>>a[i][j];
		}
	}
	const int di[]={1,1,1,0,-1,-1,-1,0};
	const int dj[]={1,0,-1,1,1,0,-1,-1};
	auto ok=[&](int i,int j){
		return i>=0 and j>=0 and i<h and j<w;
	};
	vec(mi0) pot0(m+1);
	vec(mi1) pot1(m+1);
	rep(k,m){
		pot0[k]=p0.pow(1<<k);
		pot1[k]=p1.pow(1<<k);
	}
	std::map<pii,int> mp;
	rep(dir,8){
		rep(i,h){
			rep(j,w){
				rep(k,m){
					spr[k][i*w+j]={0,0};
				}
				int x=a[i][j]-'a';
				spr[0][i*w+j]={x,x};
			}
		}
		rng(k,1,m){
			rep(i,h){
				rep(j,w){
					int ni=i+di[dir]*(1ll<<(k-1)),nj=j+dj[dir]*(1ll<<(k-1));
					ni+=h*((1ll<<k)+2);
					nj+=w*((1ll<<k)+2);
					ni%=h;
					nj%=w;
					qii q=spr[k-1][i*w+j];
					qii nq=spr[k-1][ni*w+nj];
					q.fi*=pot0[k-1];
					q.se*=pot1[k-1];
					q.fi+=nq.fi;
					q.se+=nq.se;
					spr[k][i*w+j]=q;
				}
			}
		}
		rep(si,h){
			rep(sj,w){
				int i=si,j=sj;
				qii q={0,0};
				rep(k,m){
					if(n>>k&1){
						int ni=i+di[dir]*(1ll<<k),nj=j+dj[dir]*(1ll<<k);
						ni%=h;
						nj%=w;
						ni+=h*((1ll<<k)+2);
						nj+=w*((1ll<<k)+2);
						ni%=h;
						nj%=w;
						q.fi*=pot0[k];
						q.se*=pot1[k];
						q.fi+=spr[k][i*w+j].fi;
						q.se+=spr[k][i*w+j].se;
						i=ni,j=nj;
					}
				}
				mp[pii(q.fi.x,q.se.x)]+=1;
			}
		}
	}
	int hi=0,lo=h*w*h*w*64;
	for(auto p:mp){
		hi+=p.se*p.se;
	}
	int g=gcd(hi,lo);
	hi/=g;
	lo/=g;
	cout<<hi<<"/"<<lo<<"\n";
}

Compilation message

osmosmjerka.cpp: In function 'int main()':
osmosmjerka.cpp:66:7: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
   66 |  auto ok=[&](int i,int j){
      |       ^~
# Verdict Execution time Memory Grader output
1 Correct 47 ms 121548 KB Output is correct
2 Correct 48 ms 121524 KB Output is correct
3 Correct 49 ms 121612 KB Output is correct
4 Correct 49 ms 121676 KB Output is correct
5 Correct 56 ms 121704 KB Output is correct
6 Correct 98 ms 124364 KB Output is correct
7 Correct 706 ms 140700 KB Output is correct
8 Correct 2311 ms 154804 KB Output is correct