4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL MODVAL = 1000000007LL; 4fd800b3a8 2011-02-23 kinaba: LL ADD(LL x, LL y) { return (x+y)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL MUL(LL x, LL y) { return (x*y)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL POW(LL x, LL e) { 4fd800b3a8 2011-02-23 kinaba: LL v = 1; 4fd800b3a8 2011-02-23 kinaba: for(;e;x=MUL(x,x),e>>=1) 4fd800b3a8 2011-02-23 kinaba: if(e&1) 4fd800b3a8 2011-02-23 kinaba: v = MUL(v, x); 4fd800b3a8 2011-02-23 kinaba: return v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class IOIString { public: 4fd800b3a8 2011-02-23 kinaba: string s; 4fd800b3a8 2011-02-23 kinaba: LL nI, nQ; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int countIOIs(vector <string> mask) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: s = accumulate( mask.begin(), mask.end(), string("") ); 4fd800b3a8 2011-02-23 kinaba: nI = count( s.begin(), s.end(), 'I' ); 4fd800b3a8 2011-02-23 kinaba: nQ = count( s.begin(), s.end(), '?' ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return SUB( POW(2,nQ), non_IOI() ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL non_IOI() 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: if( nI == 0 ) ans = ADD(ans, 1); // #I == 0 4fd800b3a8 2011-02-23 kinaba: if( nI == 0 ) ans = ADD(ans, nQ); // #I == 1 4fd800b3a8 2011-02-23 kinaba: if( nI == 1 ) ans = ADD(ans, 1); // #I == 1 4fd800b3a8 2011-02-23 kinaba: for(int D=1; D<s.size(); D+=2) 4fd800b3a8 2011-02-23 kinaba: for(int S=0; S<D; ++S) 4fd800b3a8 2011-02-23 kinaba: ans = ADD(ans, non_IOI_2(S, D)); // #I >= 2 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL non_IOI_2( int S, int D ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: string t; 4fd800b3a8 2011-02-23 kinaba: for(int i=S; i<s.size(); i+=D) 4fd800b3a8 2011-02-23 kinaba: t += s[i]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // t 以外の部分は全部 'O' でないとダメ 4fd800b3a8 2011-02-23 kinaba: if( count(t.begin(), t.end(), 'I') < nI ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // t は /O*II+O*/ にマッチしないとダメ。4状態のオートマトンでDP 4fd800b3a8 2011-02-23 kinaba: LL q0=1, q1=0, q2=0, q3=0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<t.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL p0 = (t[i]=='O' ? q0 : t[i]=='I' ? 0 : q0); 4fd800b3a8 2011-02-23 kinaba: LL p1 = (t[i]=='O' ? 0 : t[i]=='I' ? q0 : q0); 4fd800b3a8 2011-02-23 kinaba: LL p2 = (t[i]=='O' ? 0 : t[i]=='I' ? ADD(q1,q2) : ADD(q1,q2)); 4fd800b3a8 2011-02-23 kinaba: LL p3 = (t[i]=='O' ? ADD(q2,q3) : t[i]=='I' ? 0 : ADD(q2,q3)); 4fd800b3a8 2011-02-23 kinaba: q0=p0, q1=p1, q2=p2, q3=p3; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ADD(q2, q3); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, IOIString().countIOIs(mask));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string mask_[] = {"IO?"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string mask_[] = {"????"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 4; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string mask_[] = {"?II"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string mask_[] = {"I??O??I"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 16; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string mask_[] = {"???I???????O???","???????????O??IO????????I???"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 438952513; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string mask_[] = {"?"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: string mask_[] = { 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: "??????????????????????????????????????????????????", 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 967087276; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE