6efd022c37 2013-05-11 kinaba: #include <iostream> 6efd022c37 2013-05-11 kinaba: #include <sstream> 6efd022c37 2013-05-11 kinaba: #include <iomanip> 6efd022c37 2013-05-11 kinaba: #include <vector> 6efd022c37 2013-05-11 kinaba: #include <string> 6efd022c37 2013-05-11 kinaba: #include <map> 6efd022c37 2013-05-11 kinaba: #include <set> 6efd022c37 2013-05-11 kinaba: #include <algorithm> 6efd022c37 2013-05-11 kinaba: #include <numeric> 6efd022c37 2013-05-11 kinaba: #include <iterator> 6efd022c37 2013-05-11 kinaba: #include <functional> 6efd022c37 2013-05-11 kinaba: #include <complex> 6efd022c37 2013-05-11 kinaba: #include <queue> 6efd022c37 2013-05-11 kinaba: #include <stack> 6efd022c37 2013-05-11 kinaba: #include <cmath> 6efd022c37 2013-05-11 kinaba: #include <cassert> 6efd022c37 2013-05-11 kinaba: using namespace std; 6efd022c37 2013-05-11 kinaba: typedef long long LL; 6efd022c37 2013-05-11 kinaba: typedef long double LD; 6efd022c37 2013-05-11 kinaba: typedef complex<LD> CMP; 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: static const unsigned MODVAL = 1000000007; 6efd022c37 2013-05-11 kinaba: struct mint 6efd022c37 2013-05-11 kinaba: { 6efd022c37 2013-05-11 kinaba: unsigned val; 6efd022c37 2013-05-11 kinaba: mint():val(0){} 6efd022c37 2013-05-11 kinaba: mint(int x):val(x%MODVAL) {} 6efd022c37 2013-05-11 kinaba: mint(unsigned x):val(x%MODVAL) {} 6efd022c37 2013-05-11 kinaba: mint(LL x):val(x%MODVAL) {} 6efd022c37 2013-05-11 kinaba: }; 6efd022c37 2013-05-11 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 6efd022c37 2013-05-11 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 6efd022c37 2013-05-11 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 6efd022c37 2013-05-11 kinaba: mint operator+(mint x, mint y) { return x+=y; } 6efd022c37 2013-05-11 kinaba: mint operator-(mint x, mint y) { return x-=y; } 6efd022c37 2013-05-11 kinaba: mint operator*(mint x, mint y) { return x*=y; } 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 6efd022c37 2013-05-11 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 6efd022c37 2013-05-11 kinaba: mint operator/(mint x, mint y) { return x/=y; } 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: vector<mint> FAC_(1,1); 6efd022c37 2013-05-11 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: // nCk :: O(1) time, O(n^2) space. 6efd022c37 2013-05-11 kinaba: vector< vector<mint> > CP_; 6efd022c37 2013-05-11 kinaba: mint C(LL n, LL k) { 6efd022c37 2013-05-11 kinaba: while( CP_.size() <= n ) { 6efd022c37 2013-05-11 kinaba: int nn = CP_.size(); 6efd022c37 2013-05-11 kinaba: CP_.push_back(vector<mint>(nn+1,1)); 6efd022c37 2013-05-11 kinaba: for(int kk=1; kk<nn; ++kk) 6efd022c37 2013-05-11 kinaba: CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk]; 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: return k<0 || n<k ? 0 : CP_[n][k]; 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: struct UnionFind 6efd022c37 2013-05-11 kinaba: { 6efd022c37 2013-05-11 kinaba: vector<int> uf, sz; 6efd022c37 2013-05-11 kinaba: int nc; 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) 6efd022c37 2013-05-11 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } 6efd022c37 2013-05-11 kinaba: int size() 6efd022c37 2013-05-11 kinaba: { return nc; } 6efd022c37 2013-05-11 kinaba: int size(int a) 6efd022c37 2013-05-11 kinaba: { return sz[Find(a)]; } 6efd022c37 2013-05-11 kinaba: int Find(int a) 6efd022c37 2013-05-11 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } 6efd022c37 2013-05-11 kinaba: bool Union(int a, int b) 6efd022c37 2013-05-11 kinaba: { 6efd022c37 2013-05-11 kinaba: a = Find(a); 6efd022c37 2013-05-11 kinaba: b = Find(b); 6efd022c37 2013-05-11 kinaba: if( a != b ) 6efd022c37 2013-05-11 kinaba: { 6efd022c37 2013-05-11 kinaba: if( sz[a] >= sz[b] ) swap(a, b); 6efd022c37 2013-05-11 kinaba: uf[a] = b; 6efd022c37 2013-05-11 kinaba: sz[b] += sz[a]; 6efd022c37 2013-05-11 kinaba: --nc; 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: return (a!=b); 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: }; 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: class GooseInZooDivOne { public: 6efd022c37 2013-05-11 kinaba: int count(vector <string> field, int dist) 6efd022c37 2013-05-11 kinaba: { 6efd022c37 2013-05-11 kinaba: int H = field.size(); 6efd022c37 2013-05-11 kinaba: int W = field[0].size(); 6efd022c37 2013-05-11 kinaba: vector<pair<int,int> > b; 6efd022c37 2013-05-11 kinaba: for(int y=0; y<H; ++y) 6efd022c37 2013-05-11 kinaba: for(int x=0; x<W; ++x) 6efd022c37 2013-05-11 kinaba: if(field[y][x] == 'v') 6efd022c37 2013-05-11 kinaba: b.push_back(make_pair(y,x)); 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: int N = b.size(); 6efd022c37 2013-05-11 kinaba: UnionFind uf(N); 6efd022c37 2013-05-11 kinaba: for(int i=0; i<N; ++i) 6efd022c37 2013-05-11 kinaba: for(int k=i+1; k<N; ++k) { 6efd022c37 2013-05-11 kinaba: int y = b[i].first; 6efd022c37 2013-05-11 kinaba: int x = b[i].second; 6efd022c37 2013-05-11 kinaba: int Y = b[k].first; 6efd022c37 2013-05-11 kinaba: int X = b[k].second; 6efd022c37 2013-05-11 kinaba: if(abs(y-Y)+abs(x-X) <= dist) 6efd022c37 2013-05-11 kinaba: uf.Union(i, k); 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: int num[2]={0,0}; 6efd022c37 2013-05-11 kinaba: for(int i=0; i<N; ++i) 6efd022c37 2013-05-11 kinaba: if(uf.Find(i) == i) 6efd022c37 2013-05-11 kinaba: num[uf.size(i)%2] ++; 6efd022c37 2013-05-11 kinaba: return solve(num[0], num[1]).val; 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: mint solve(int ev, int od) 6efd022c37 2013-05-11 kinaba: { 6efd022c37 2013-05-11 kinaba: mint total = 0; 6efd022c37 2013-05-11 kinaba: for(int k=0; k<=od; k+=2) 6efd022c37 2013-05-11 kinaba: total += C(od, k); 6efd022c37 2013-05-11 kinaba: return POW(2, ev) * total - 1; 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: }; 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: // BEGIN CUT HERE 6efd022c37 2013-05-11 kinaba: #include <ctime> 6efd022c37 2013-05-11 kinaba: double start_time; string timer() 6efd022c37 2013-05-11 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6efd022c37 2013-05-11 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6efd022c37 2013-05-11 kinaba: { os << "{ "; 6efd022c37 2013-05-11 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6efd022c37 2013-05-11 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6efd022c37 2013-05-11 kinaba: void verify_case(const int& Expected, const int& Received) { 6efd022c37 2013-05-11 kinaba: bool ok = (Expected == Received); 6efd022c37 2013-05-11 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6efd022c37 2013-05-11 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6efd022c37 2013-05-11 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6efd022c37 2013-05-11 kinaba: #define END verify_case(_, GooseInZooDivOne().count(field, dist));} 6efd022c37 2013-05-11 kinaba: int main(){ 6efd022c37 2013-05-11 kinaba: 6efd022c37 2013-05-11 kinaba: CASE(0) 6efd022c37 2013-05-11 kinaba: string field_[] = {"vvv"}; 6efd022c37 2013-05-11 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 6efd022c37 2013-05-11 kinaba: int dist = 0; 6efd022c37 2013-05-11 kinaba: int _ = 3; 6efd022c37 2013-05-11 kinaba: END 6efd022c37 2013-05-11 kinaba: CASE(1) 6efd022c37 2013-05-11 kinaba: string field_[] = {"."}; 6efd022c37 2013-05-11 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 6efd022c37 2013-05-11 kinaba: int dist = 100; 6efd022c37 2013-05-11 kinaba: int _ = 0; 6efd022c37 2013-05-11 kinaba: END 6efd022c37 2013-05-11 kinaba: CASE(2) 6efd022c37 2013-05-11 kinaba: string field_[] = {"vvv"}; 6efd022c37 2013-05-11 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 6efd022c37 2013-05-11 kinaba: int dist = 1; 6efd022c37 2013-05-11 kinaba: int _ = 0; 6efd022c37 2013-05-11 kinaba: END 6efd022c37 2013-05-11 kinaba: CASE(3) 6efd022c37 2013-05-11 kinaba: string field_[] = {"v.v..................v............................" 6efd022c37 2013-05-11 kinaba: ,".v......v..................v.....................v" 6efd022c37 2013-05-11 kinaba: ,"..v.....v....v.........v...............v......v..." 6efd022c37 2013-05-11 kinaba: ,".........vvv...vv.v.........v.v..................v" 6efd022c37 2013-05-11 kinaba: ,".....v..........v......v..v...v.......v..........." 6efd022c37 2013-05-11 kinaba: ,"...................vv...............v.v..v.v..v..." 6efd022c37 2013-05-11 kinaba: ,".v.vv.................v..............v............" 6efd022c37 2013-05-11 kinaba: ,"..vv.......v...vv.v............vv.....v.....v....." 6efd022c37 2013-05-11 kinaba: ,"....v..........v....v........v.......v.v.v........" 6efd022c37 2013-05-11 kinaba: ,".v.......v.............v.v..........vv......v....." 6efd022c37 2013-05-11 kinaba: ,"....v.v.......v........v.....v.................v.." 6efd022c37 2013-05-11 kinaba: ,"....v..v..v.v..............v.v.v....v..........v.." 6efd022c37 2013-05-11 kinaba: ,"..........v...v...................v..............v" 6efd022c37 2013-05-11 kinaba: ,"..v........v..........................v....v..v..." 6efd022c37 2013-05-11 kinaba: ,"....................v..v.........vv........v......" 6efd022c37 2013-05-11 kinaba: ,"..v......v...............................v.v......" 6efd022c37 2013-05-11 kinaba: ,"..v.v..............v........v...............vv.vv." 6efd022c37 2013-05-11 kinaba: ,"...vv......v...............v.v..............v....." 6efd022c37 2013-05-11 kinaba: ,"............................v..v.................v" 6efd022c37 2013-05-11 kinaba: ,".v.............v.......v.........................." 6efd022c37 2013-05-11 kinaba: ,"......v...v........................v.............." 6efd022c37 2013-05-11 kinaba: ,".........v.....v..............vv.................." 6efd022c37 2013-05-11 kinaba: ,"................v..v..v.........v....v.......v...." 6efd022c37 2013-05-11 kinaba: ,"........v.....v.............v......v.v............" 6efd022c37 2013-05-11 kinaba: ,"...........v....................v.v....v.v.v...v.." 6efd022c37 2013-05-11 kinaba: ,"...........v......................v...v..........." 6efd022c37 2013-05-11 kinaba: ,"..........vv...........v.v.....................v.." 6efd022c37 2013-05-11 kinaba: ,".....................v......v............v...v...." 6efd022c37 2013-05-11 kinaba: ,".....vv..........................vv.v.....v.v....." 6efd022c37 2013-05-11 kinaba: ,".vv.......v...............v.......v..v.....v......" 6efd022c37 2013-05-11 kinaba: ,"............v................v..........v....v...." 6efd022c37 2013-05-11 kinaba: ,"................vv...v............................" 6efd022c37 2013-05-11 kinaba: ,"................v...........v........v...v....v..." 6efd022c37 2013-05-11 kinaba: ,"..v...v...v.............v...v........v....v..v...." 6efd022c37 2013-05-11 kinaba: ,"......v..v.......v........v..v....vv.............." 6efd022c37 2013-05-11 kinaba: ,"...........v..........v........v.v................" 6efd022c37 2013-05-11 kinaba: ,"v.v......v................v....................v.." 6efd022c37 2013-05-11 kinaba: ,".v........v................................v......" 6efd022c37 2013-05-11 kinaba: ,"............................v...v.......v........." 6efd022c37 2013-05-11 kinaba: ,"........................vv.v..............v...vv.." 6efd022c37 2013-05-11 kinaba: ,".......................vv........v.............v.." 6efd022c37 2013-05-11 kinaba: ,"...v.............v.........................v......" 6efd022c37 2013-05-11 kinaba: ,"....v......vv...........................v........." 6efd022c37 2013-05-11 kinaba: ,"....vv....v................v...vv..............v.." 6efd022c37 2013-05-11 kinaba: ,".................................................." 6efd022c37 2013-05-11 kinaba: ,"vv........v...v..v.....v..v..................v...." 6efd022c37 2013-05-11 kinaba: ,".........v..............v.vv.v.............v......" 6efd022c37 2013-05-11 kinaba: ,".......v.....v......v...............v............." 6efd022c37 2013-05-11 kinaba: ,"..v..................v................v....v......" 6efd022c37 2013-05-11 kinaba: ,".....v.....v.....................v.v......v......."}; 6efd022c37 2013-05-11 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 6efd022c37 2013-05-11 kinaba: int dist = 3; 6efd022c37 2013-05-11 kinaba: int _ = 898961330; 6efd022c37 2013-05-11 kinaba: END 6efd022c37 2013-05-11 kinaba: /* 6efd022c37 2013-05-11 kinaba: CASE(4) 6efd022c37 2013-05-11 kinaba: string field_[] = ; 6efd022c37 2013-05-11 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 6efd022c37 2013-05-11 kinaba: int dist = ; 6efd022c37 2013-05-11 kinaba: int _ = ; 6efd022c37 2013-05-11 kinaba: END 6efd022c37 2013-05-11 kinaba: CASE(5) 6efd022c37 2013-05-11 kinaba: string field_[] = ; 6efd022c37 2013-05-11 kinaba: vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); 6efd022c37 2013-05-11 kinaba: int dist = ; 6efd022c37 2013-05-11 kinaba: int _ = ; 6efd022c37 2013-05-11 kinaba: END 6efd022c37 2013-05-11 kinaba: */ 6efd022c37 2013-05-11 kinaba: } 6efd022c37 2013-05-11 kinaba: // END CUT HERE