ed7454da2a 2016-10-26 kinaba: #include <iostream> ed7454da2a 2016-10-26 kinaba: #include <sstream> ed7454da2a 2016-10-26 kinaba: #include <iomanip> ed7454da2a 2016-10-26 kinaba: #include <vector> ed7454da2a 2016-10-26 kinaba: #include <string> ed7454da2a 2016-10-26 kinaba: #include <map> ed7454da2a 2016-10-26 kinaba: #include <set> ed7454da2a 2016-10-26 kinaba: #include <algorithm> ed7454da2a 2016-10-26 kinaba: #include <numeric> ed7454da2a 2016-10-26 kinaba: #include <iterator> ed7454da2a 2016-10-26 kinaba: #include <functional> ed7454da2a 2016-10-26 kinaba: #include <complex> ed7454da2a 2016-10-26 kinaba: #include <queue> ed7454da2a 2016-10-26 kinaba: #include <stack> ed7454da2a 2016-10-26 kinaba: #include <cmath> ed7454da2a 2016-10-26 kinaba: #include <cassert> ed7454da2a 2016-10-26 kinaba: #include <tuple> ed7454da2a 2016-10-26 kinaba: using namespace std; ed7454da2a 2016-10-26 kinaba: typedef long long LL; ed7454da2a 2016-10-26 kinaba: typedef complex<double> CMP; ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: static const unsigned MODVAL = 1000000007; ed7454da2a 2016-10-26 kinaba: struct mint ed7454da2a 2016-10-26 kinaba: { ed7454da2a 2016-10-26 kinaba: unsigned val; ed7454da2a 2016-10-26 kinaba: mint() :val(0) {} ed7454da2a 2016-10-26 kinaba: mint(int x) :val(x%MODVAL) {} ed7454da2a 2016-10-26 kinaba: mint(unsigned x) :val(x%MODVAL) {} ed7454da2a 2016-10-26 kinaba: mint(LL x) :val(x%MODVAL) {} ed7454da2a 2016-10-26 kinaba: }; ed7454da2a 2016-10-26 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val + y.val; } ed7454da2a 2016-10-26 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; } ed7454da2a 2016-10-26 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } ed7454da2a 2016-10-26 kinaba: mint operator+(mint x, mint y) { return x += y; } ed7454da2a 2016-10-26 kinaba: mint operator-(mint x, mint y) { return x -= y; } ed7454da2a 2016-10-26 kinaba: mint operator*(mint x, mint y) { return x *= y; } ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: mint POW(mint x, LL e) { mint v = 1; for (; e; x *= x, e >>= 1) if (e & 1) v *= x; return v; } ed7454da2a 2016-10-26 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL - 2); } ed7454da2a 2016-10-26 kinaba: mint operator/(mint x, mint y) { return x /= y; } ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: vector<mint> FAC_(1, 1); ed7454da2a 2016-10-26 kinaba: mint FAC(LL n) { while (FAC_.size() <= n) FAC_.push_back(FAC_.back()*LL(FAC_.size())); return FAC_[n]; } ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: // nCk :: O(log MODVAL) time, O(n) space. ed7454da2a 2016-10-26 kinaba: mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n - k)); } ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: class FibonacciStringSum { public: ed7454da2a 2016-10-26 kinaba: int get(int n, int a, int b) ed7454da2a 2016-10-26 kinaba: { ed7454da2a 2016-10-26 kinaba: mint total = 0; ed7454da2a 2016-10-26 kinaba: for (int n0 = 0; n0 <= n; ++n0) { ed7454da2a 2016-10-26 kinaba: int n1 = n - n0; ed7454da2a 2016-10-26 kinaba: if(n1 <= n0+1) ed7454da2a 2016-10-26 kinaba: total += POW(n0, a) * POW(n1, b) * num(n0, n1); ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: return total.val; ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: mint num(int n0, int n1) ed7454da2a 2016-10-26 kinaba: { ed7454da2a 2016-10-26 kinaba: if (n1 == 0) ed7454da2a 2016-10-26 kinaba: return 1; ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: mint all = 0; ed7454da2a 2016-10-26 kinaba: // [10] [0] pat ed7454da2a 2016-10-26 kinaba: if (n1 <= n0) { ed7454da2a 2016-10-26 kinaba: all += C(n1 + (n0 - n1), n1); ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: // [10] [0] 1 pat ed7454da2a 2016-10-26 kinaba: all += C(n1 - 1 + (n0 - n1 + 1), n1 - 1); ed7454da2a 2016-10-26 kinaba: return all; ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: }; ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: // BEGIN CUT HERE ed7454da2a 2016-10-26 kinaba: #include <ctime> ed7454da2a 2016-10-26 kinaba: double start_time; string timer() ed7454da2a 2016-10-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ed7454da2a 2016-10-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ed7454da2a 2016-10-26 kinaba: { os << "{ "; ed7454da2a 2016-10-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ed7454da2a 2016-10-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ed7454da2a 2016-10-26 kinaba: void verify_case(const int& Expected, const int& Received) { ed7454da2a 2016-10-26 kinaba: bool ok = (Expected == Received); ed7454da2a 2016-10-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ed7454da2a 2016-10-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } ed7454da2a 2016-10-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ed7454da2a 2016-10-26 kinaba: #define END verify_case(_, FibonacciStringSum().get(n, a, b));} ed7454da2a 2016-10-26 kinaba: int main(){ ed7454da2a 2016-10-26 kinaba: ed7454da2a 2016-10-26 kinaba: CASE(0) ed7454da2a 2016-10-26 kinaba: int n = 3; ed7454da2a 2016-10-26 kinaba: int a = 0; ed7454da2a 2016-10-26 kinaba: int b = 0; ed7454da2a 2016-10-26 kinaba: int _ = 5; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(1) ed7454da2a 2016-10-26 kinaba: int n = 3; ed7454da2a 2016-10-26 kinaba: int a = 0; ed7454da2a 2016-10-26 kinaba: int b = 1; ed7454da2a 2016-10-26 kinaba: int _ = 5; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(2) ed7454da2a 2016-10-26 kinaba: int n = 10; ed7454da2a 2016-10-26 kinaba: int a = 10; ed7454da2a 2016-10-26 kinaba: int b = 10; ed7454da2a 2016-10-26 kinaba: int _ = 518500021; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(3) ed7454da2a 2016-10-26 kinaba: int n = 5000; ed7454da2a 2016-10-26 kinaba: int a = 20; ed7454da2a 2016-10-26 kinaba: int b = 20; ed7454da2a 2016-10-26 kinaba: int _ = 880245669; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: /* ed7454da2a 2016-10-26 kinaba: CASE(4) ed7454da2a 2016-10-26 kinaba: int n = ; ed7454da2a 2016-10-26 kinaba: int a = ; ed7454da2a 2016-10-26 kinaba: int b = ; ed7454da2a 2016-10-26 kinaba: int _ = ; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: CASE(5) ed7454da2a 2016-10-26 kinaba: int n = ; ed7454da2a 2016-10-26 kinaba: int a = ; ed7454da2a 2016-10-26 kinaba: int b = ; ed7454da2a 2016-10-26 kinaba: int _ = ; ed7454da2a 2016-10-26 kinaba: END ed7454da2a 2016-10-26 kinaba: */ ed7454da2a 2016-10-26 kinaba: } ed7454da2a 2016-10-26 kinaba: // END CUT HERE