0fce772883 2015-11-18 kinaba: #include <iostream> 0fce772883 2015-11-18 kinaba: #include <sstream> 0fce772883 2015-11-18 kinaba: #include <iomanip> 0fce772883 2015-11-18 kinaba: #include <vector> 0fce772883 2015-11-18 kinaba: #include <string> 0fce772883 2015-11-18 kinaba: #include <map> 0fce772883 2015-11-18 kinaba: #include <set> 0fce772883 2015-11-18 kinaba: #include <algorithm> 0fce772883 2015-11-18 kinaba: #include <numeric> 0fce772883 2015-11-18 kinaba: #include <iterator> 0fce772883 2015-11-18 kinaba: #include <functional> 0fce772883 2015-11-18 kinaba: #include <complex> 0fce772883 2015-11-18 kinaba: #include <queue> 0fce772883 2015-11-18 kinaba: #include <stack> 0fce772883 2015-11-18 kinaba: #include <cmath> 0fce772883 2015-11-18 kinaba: #include <cassert> 0fce772883 2015-11-18 kinaba: #include <tuple> 0fce772883 2015-11-18 kinaba: using namespace std; 0fce772883 2015-11-18 kinaba: typedef long long LL; 0fce772883 2015-11-18 kinaba: typedef complex<double> CMP; 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: static const unsigned MODVAL = 1000000007; 0fce772883 2015-11-18 kinaba: struct mint 0fce772883 2015-11-18 kinaba: { 0fce772883 2015-11-18 kinaba: unsigned val; 0fce772883 2015-11-18 kinaba: mint():val(0){} 0fce772883 2015-11-18 kinaba: mint(int x):val(x%MODVAL) {} 0fce772883 2015-11-18 kinaba: mint(unsigned x):val(x%MODVAL) {} 0fce772883 2015-11-18 kinaba: mint(LL x):val(x%MODVAL) {} 0fce772883 2015-11-18 kinaba: }; 0fce772883 2015-11-18 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 0fce772883 2015-11-18 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 0fce772883 2015-11-18 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 0fce772883 2015-11-18 kinaba: mint operator+(mint x, mint y) { return x+=y; } 0fce772883 2015-11-18 kinaba: mint operator-(mint x, mint y) { return x-=y; } 0fce772883 2015-11-18 kinaba: mint operator*(mint x, mint y) { return x*=y; } 0fce772883 2015-11-18 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 0fce772883 2015-11-18 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 0fce772883 2015-11-18 kinaba: mint operator/(mint x, mint y) { return x/=y; } 0fce772883 2015-11-18 kinaba: vector<mint> FAC_(1,1); 0fce772883 2015-11-18 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*LL(FAC_.size()) ); return FAC_[n]; } 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: // nCk :: O(1) time, O(n^2) space. 0fce772883 2015-11-18 kinaba: vector< vector<mint> > CP_; 0fce772883 2015-11-18 kinaba: mint C(int n, int k) { 0fce772883 2015-11-18 kinaba: while( CP_.size() <= n ) { 0fce772883 2015-11-18 kinaba: int nn = CP_.size(); 0fce772883 2015-11-18 kinaba: CP_.push_back(vector<mint>(nn+1,1)); 0fce772883 2015-11-18 kinaba: for(int kk=1; kk<nn; ++kk) 0fce772883 2015-11-18 kinaba: CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk]; 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: return k<0 || n<k ? 0 : CP_[n][k]; 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: class AlmostEulerianGraph { public: 0fce772883 2015-11-18 kinaba: int calculateGraphs(int n) 0fce772883 2015-11-18 kinaba: { 0fce772883 2015-11-18 kinaba: memo.assign(n+1, -1); 0fce772883 2015-11-18 kinaba: return (calcExactEulerian(n) * (LL(n)*(n-1)/2+1)).val; 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: vector<int> memo; 0fce772883 2015-11-18 kinaba: mint calcExactEulerian(int n) 0fce772883 2015-11-18 kinaba: { 0fce772883 2015-11-18 kinaba: if(n == 1) 0fce772883 2015-11-18 kinaba: return 1; 0fce772883 2015-11-18 kinaba: if(memo[n] != -1) 0fce772883 2015-11-18 kinaba: return memo[n]; 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: mint sum = 0; 0fce772883 2015-11-18 kinaba: for(int k=1; k<n; ++k) 0fce772883 2015-11-18 kinaba: sum += k * C(n, k) * POW(2, C(n-k-1, 2).val) * calcExactEulerian(k); 0fce772883 2015-11-18 kinaba: return memo[n] = (POW(2, C(n-1, 2).val) - sum/n).val; 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: }; 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: // BEGIN CUT HERE 0fce772883 2015-11-18 kinaba: #include <ctime> 0fce772883 2015-11-18 kinaba: double start_time; string timer() 0fce772883 2015-11-18 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 0fce772883 2015-11-18 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 0fce772883 2015-11-18 kinaba: { os << "{ "; 0fce772883 2015-11-18 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 0fce772883 2015-11-18 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 0fce772883 2015-11-18 kinaba: void verify_case(const int& Expected, const int& Received) { 0fce772883 2015-11-18 kinaba: bool ok = (Expected == Received); 0fce772883 2015-11-18 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 0fce772883 2015-11-18 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 0fce772883 2015-11-18 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 0fce772883 2015-11-18 kinaba: #define END verify_case(_, AlmostEulerianGraph().calculateGraphs(n));} 0fce772883 2015-11-18 kinaba: int main(){ 0fce772883 2015-11-18 kinaba: 0fce772883 2015-11-18 kinaba: CASE(0) 0fce772883 2015-11-18 kinaba: int n = 3; 0fce772883 2015-11-18 kinaba: int _ = 4; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(1) 0fce772883 2015-11-18 kinaba: int n = 2; 0fce772883 2015-11-18 kinaba: int _ = 0; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(2) 0fce772883 2015-11-18 kinaba: int n = 42; 0fce772883 2015-11-18 kinaba: int _ = 29010676; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: CASE(3) 0fce772883 2015-11-18 kinaba: int n = 2000; 0fce772883 2015-11-18 kinaba: int _ = -1; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: /* 0fce772883 2015-11-18 kinaba: CASE(4) 0fce772883 2015-11-18 kinaba: int n = ; 0fce772883 2015-11-18 kinaba: int _ = ; 0fce772883 2015-11-18 kinaba: END 0fce772883 2015-11-18 kinaba: */ 0fce772883 2015-11-18 kinaba: } 0fce772883 2015-11-18 kinaba: // END CUT HERE