#include <iostream> #include <map> #define REP(x,n) for(int x=0;x<(n);++x) #define DEBUG(str) //cerr << str << endl using namespace std; const int MAX_N = 3001; //enum //{ // BOARD_NONE = 0, // BOARD_BOARD, // BOARD_NEAR //} board[MAX_N][MAX_N]; struct sth { char lastRound; int index; } board[MAX_N][MAX_N]; int n; struct Point { short x; short y; int ways; bool operator<(const Point& p) const { return x==p.x ? y<p.y : x<p.x; } }; ostream& operator<<(ostream& os, const Point& pt) { return os << "["<<pt.x<<","<<pt.y<<"]"; } struct Buf { int size; Point arr[MAX_N*MAX_N]; void add(const Point& item) { arr[size] = item; ++size; } Buf(): size(0) {} void inline clear() {size=0;} const Point* begin() const { return &arr[0]; } const Point* end() const { return &arr[size]; } } buffer[2]; struct Detail { int count; long long sum; map<int,int> distribution; map<int,int> outdegs; void collect(const Buf& arr) { count = arr.size; sum = 0; DEBUG("arr size "<<arr.size); for(auto& elem : arr) { DEBUG("inside "<<elem<<" - sum ("<<sum<<")"<<elem.ways); sum += elem.ways; DEBUG("sum = "<<sum); ++distribution[elem.ways]; } } } details[5]; void ParseBoard(int maxDepth) { auto& brzegi = buffer[0]; auto& dist1 = buffer[1]; for(const auto& p : brzegi) { if(p.x && board[p.x-1][p.y].lastRound==-1) // left { board[p.x-1][p.y] = {1, dist1.size}; dist1.add({p.x-1,p.y,1}); } if(p.y && board[p.x][p.y-1].lastRound==-1) // top { board[p.x][p.y-1] = {1, dist1.size}; dist1.add({p.x,p.y-1,1}); } if(p.x!=n-1 && board[p.x+1][p.y].lastRound==-1) // right { board[p.x+1][p.y] = {1, dist1.size}; dist1.add({p.x+1,p.y,1}); } if(p.y!=n-1 && board[p.x][p.y+1].lastRound==-1) // bottom { board[p.x][p.y+1] = {1, dist1.size}; dist1.add({p.x,p.y+1,1}); } } details[1].collect(dist1); for(int i=2;i<=maxDepth;++i) { DEBUG("process "<<i); auto& currDist = buffer[(~i)&1]; auto& nextDist = buffer[i&1]; auto& det = details[i]; nextDist.clear(); for(const auto& p : currDist) { int add=0; if(p.x && board[p.x-1][p.y].lastRound!=1) // left { ++add; auto& item = board[p.x-1][p.y]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x-1,p.y,p.ways}); } } if(p.y && board[p.x][p.y-1].lastRound!=1) // top { ++add; auto& item = board[p.x][p.y-1]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x,p.y-1,p.ways}); } } if(p.x!=n-1 && board[p.x+1][p.y].lastRound!=1) // right { ++add; auto& item = board[p.x+1][p.y]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x+1,p.y,p.ways}); } } if(p.y!=n-1 && board[p.x][p.y+1].lastRound!=1) // bottom { ++add; auto& item = board[p.x][p.y+1]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x,p.y+1,p.ways}); } } if(add) ++det.outdegs[add]; } det.collect(nextDist); } } #define CUT(n) ((n)%1000000007LL) long long Result1() { return details[1].count; } long long Result2() { long long temp = details[1].count; DEBUG("1 count = "<<temp); long long res = CUT(temp*(temp-1)/2); DEBUG("2 sum = "<<details[2].sum); res = CUT(res+details[2].sum); return res; } long long Result3(){ long long temp = details[1].count; long long res = temp>2 ? CUT(CUT(temp*(temp-1))*(temp-2)/6) : 0LL; DEBUG(res); for(auto& elem : details[2].distribution) { auto i = elem.first; auto& ways = elem.second; DEBUG("res = "<<res<<" + "<<ways<<" * "<<i<<" * ("<<(temp-1)<< "*" <<(temp-2) << "-"<<(i-1)<<") / 2"); res = CUT(res + CUT(ways * CUT(i*((temp-1)*(temp-2)-i+1)/2))); DEBUG(res); } DEBUG(res<<" += "<<details[3].sum); res = CUT(res+details[3].sum); for(auto& keyVal : details[2].outdegs) res = CUT(res+keyVal.second * (keyVal.first)*(keyVal.first-1)/2); return res; } long long Result4(){ long long temp = details[1].count; long long res; if(temp&1) res = CUT(CUT(temp*(temp-1)/2*(temp-2)/3)*(temp-3)/2); else res = CUT(CUT((temp/2*(temp-2)/2*(temp-1))/3)*(temp-3)); return res; } int main() { ios_base::sync_with_stdio(0); auto& brzegi = buffer[0]; int k; cin>>n>>k; string s; REP(y,n) { cin>>s; int x=-1; for(char& c : s) if(c=='#') { board[++x][y] = {1,brzegi.size}; brzegi.add({x,y}); } else board[++x][y] = {-1,-1}; } ParseBoard(k); DEBUG("k = "<<k); DEBUG("jestem po"); if(k==1) cout<<Result1()<<endl; else if(k==2) cout<<Result2()<<endl; else if(k==3) { DEBUG("wchodze to 3"); cout<<Result3()<<endl; } else if(k==4) cout<<Result4()<<endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #include <iostream> #include <map> #define REP(x,n) for(int x=0;x<(n);++x) #define DEBUG(str) //cerr << str << endl using namespace std; const int MAX_N = 3001; //enum //{ // BOARD_NONE = 0, // BOARD_BOARD, // BOARD_NEAR //} board[MAX_N][MAX_N]; struct sth { char lastRound; int index; } board[MAX_N][MAX_N]; int n; struct Point { short x; short y; int ways; bool operator<(const Point& p) const { return x==p.x ? y<p.y : x<p.x; } }; ostream& operator<<(ostream& os, const Point& pt) { return os << "["<<pt.x<<","<<pt.y<<"]"; } struct Buf { int size; Point arr[MAX_N*MAX_N]; void add(const Point& item) { arr[size] = item; ++size; } Buf(): size(0) {} void inline clear() {size=0;} const Point* begin() const { return &arr[0]; } const Point* end() const { return &arr[size]; } } buffer[2]; struct Detail { int count; long long sum; map<int,int> distribution; map<int,int> outdegs; void collect(const Buf& arr) { count = arr.size; sum = 0; DEBUG("arr size "<<arr.size); for(auto& elem : arr) { DEBUG("inside "<<elem<<" - sum ("<<sum<<")"<<elem.ways); sum += elem.ways; DEBUG("sum = "<<sum); ++distribution[elem.ways]; } } } details[5]; void ParseBoard(int maxDepth) { auto& brzegi = buffer[0]; auto& dist1 = buffer[1]; for(const auto& p : brzegi) { if(p.x && board[p.x-1][p.y].lastRound==-1) // left { board[p.x-1][p.y] = {1, dist1.size}; dist1.add({p.x-1,p.y,1}); } if(p.y && board[p.x][p.y-1].lastRound==-1) // top { board[p.x][p.y-1] = {1, dist1.size}; dist1.add({p.x,p.y-1,1}); } if(p.x!=n-1 && board[p.x+1][p.y].lastRound==-1) // right { board[p.x+1][p.y] = {1, dist1.size}; dist1.add({p.x+1,p.y,1}); } if(p.y!=n-1 && board[p.x][p.y+1].lastRound==-1) // bottom { board[p.x][p.y+1] = {1, dist1.size}; dist1.add({p.x,p.y+1,1}); } } details[1].collect(dist1); for(int i=2;i<=maxDepth;++i) { DEBUG("process "<<i); auto& currDist = buffer[(~i)&1]; auto& nextDist = buffer[i&1]; auto& det = details[i]; nextDist.clear(); for(const auto& p : currDist) { int add=0; if(p.x && board[p.x-1][p.y].lastRound!=1) // left { ++add; auto& item = board[p.x-1][p.y]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x-1,p.y,p.ways}); } } if(p.y && board[p.x][p.y-1].lastRound!=1) // top { ++add; auto& item = board[p.x][p.y-1]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x,p.y-1,p.ways}); } } if(p.x!=n-1 && board[p.x+1][p.y].lastRound!=1) // right { ++add; auto& item = board[p.x+1][p.y]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x+1,p.y,p.ways}); } } if(p.y!=n-1 && board[p.x][p.y+1].lastRound!=1) // bottom { ++add; auto& item = board[p.x][p.y+1]; if(item.lastRound == i) nextDist.arr[item.index].ways += p.ways; else { item = {i, nextDist.size}; nextDist.add({p.x,p.y+1,p.ways}); } } if(add) ++det.outdegs[add]; } det.collect(nextDist); } } #define CUT(n) ((n)%1000000007LL) long long Result1() { return details[1].count; } long long Result2() { long long temp = details[1].count; DEBUG("1 count = "<<temp); long long res = CUT(temp*(temp-1)/2); DEBUG("2 sum = "<<details[2].sum); res = CUT(res+details[2].sum); return res; } long long Result3(){ long long temp = details[1].count; long long res = temp>2 ? CUT(CUT(temp*(temp-1))*(temp-2)/6) : 0LL; DEBUG(res); for(auto& elem : details[2].distribution) { auto i = elem.first; auto& ways = elem.second; DEBUG("res = "<<res<<" + "<<ways<<" * "<<i<<" * ("<<(temp-1)<< "*" <<(temp-2) << "-"<<(i-1)<<") / 2"); res = CUT(res + CUT(ways * CUT(i*((temp-1)*(temp-2)-i+1)/2))); DEBUG(res); } DEBUG(res<<" += "<<details[3].sum); res = CUT(res+details[3].sum); for(auto& keyVal : details[2].outdegs) res = CUT(res+keyVal.second * (keyVal.first)*(keyVal.first-1)/2); return res; } long long Result4(){ long long temp = details[1].count; long long res; if(temp&1) res = CUT(CUT(temp*(temp-1)/2*(temp-2)/3)*(temp-3)/2); else res = CUT(CUT((temp/2*(temp-2)/2*(temp-1))/3)*(temp-3)); return res; } int main() { ios_base::sync_with_stdio(0); auto& brzegi = buffer[0]; int k; cin>>n>>k; string s; REP(y,n) { cin>>s; int x=-1; for(char& c : s) if(c=='#') { board[++x][y] = {1,brzegi.size}; brzegi.add({x,y}); } else board[++x][y] = {-1,-1}; } ParseBoard(k); DEBUG("k = "<<k); DEBUG("jestem po"); if(k==1) cout<<Result1()<<endl; else if(k==2) cout<<Result2()<<endl; else if(k==3) { DEBUG("wchodze to 3"); cout<<Result3()<<endl; } else if(k==4) cout<<Result4()<<endl; return 0; } |