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