fbe763b30e 2016-05-26 kinaba: #include <iostream> fbe763b30e 2016-05-26 kinaba: #include <sstream> fbe763b30e 2016-05-26 kinaba: #include <iomanip> fbe763b30e 2016-05-26 kinaba: #include <vector> fbe763b30e 2016-05-26 kinaba: #include <string> fbe763b30e 2016-05-26 kinaba: #include <map> fbe763b30e 2016-05-26 kinaba: #include <set> fbe763b30e 2016-05-26 kinaba: #include <algorithm> fbe763b30e 2016-05-26 kinaba: #include <numeric> fbe763b30e 2016-05-26 kinaba: #include <iterator> fbe763b30e 2016-05-26 kinaba: #include <functional> fbe763b30e 2016-05-26 kinaba: #include <complex> fbe763b30e 2016-05-26 kinaba: #include <queue> fbe763b30e 2016-05-26 kinaba: #include <stack> fbe763b30e 2016-05-26 kinaba: #include <cmath> fbe763b30e 2016-05-26 kinaba: #include <cassert> fbe763b30e 2016-05-26 kinaba: #include <tuple> fbe763b30e 2016-05-26 kinaba: using namespace std; fbe763b30e 2016-05-26 kinaba: typedef long long LL; fbe763b30e 2016-05-26 kinaba: typedef complex<double> CMP; fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: static const unsigned MODVAL = 1000000007; fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: struct mint fbe763b30e 2016-05-26 kinaba: { fbe763b30e 2016-05-26 kinaba: unsigned val; fbe763b30e 2016-05-26 kinaba: mint():val(0){} fbe763b30e 2016-05-26 kinaba: mint(int x):val(x%MODVAL) {} fbe763b30e 2016-05-26 kinaba: mint(unsigned x):val(x%MODVAL) {} fbe763b30e 2016-05-26 kinaba: mint(LL x):val(x%MODVAL) {} fbe763b30e 2016-05-26 kinaba: }; fbe763b30e 2016-05-26 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } fbe763b30e 2016-05-26 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } fbe763b30e 2016-05-26 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } fbe763b30e 2016-05-26 kinaba: mint operator+(mint x, mint y) { return x+=y; } fbe763b30e 2016-05-26 kinaba: mint operator-(mint x, mint y) { return x-=y; } fbe763b30e 2016-05-26 kinaba: mint operator*(mint x, mint y) { return x*=y; } fbe763b30e 2016-05-26 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } fbe763b30e 2016-05-26 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } fbe763b30e 2016-05-26 kinaba: mint operator/(mint x, mint y) { return x/=y; } fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: class TriangleTriples { public: fbe763b30e 2016-05-26 kinaba: int count(int A, int B, int C) fbe763b30e 2016-05-26 kinaba: { fbe763b30e 2016-05-26 kinaba: return (mint(A)*B*C - cut(A,B,C) - cut(B,C,A) - cut(C,A,B)).val; fbe763b30e 2016-05-26 kinaba: } fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: // #{(a,b,c)<=(A,B,C) | a+b<=c} fbe763b30e 2016-05-26 kinaba: mint cut(LL A, LL B, LL C) fbe763b30e 2016-05-26 kinaba: { fbe763b30e 2016-05-26 kinaba: if(A>B) swap(A,B); fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: //---------------------------- fbe763b30e 2016-05-26 kinaba: // Sigma fbe763b30e 2016-05-26 kinaba: // #{a+b <= 1} fbe763b30e 2016-05-26 kinaba: // #{a+b <= 2} fbe763b30e 2016-05-26 kinaba: // ... fbe763b30e 2016-05-26 kinaba: // #{a+b <= C} fbe763b30e 2016-05-26 kinaba: //---------------------------- fbe763b30e 2016-05-26 kinaba: // Sigma fbe763b30e 2016-05-26 kinaba: // #{a+b == 1} * C fbe763b30e 2016-05-26 kinaba: // #{a+b == 2} * C-1 fbe763b30e 2016-05-26 kinaba: // ... fbe763b30e 2016-05-26 kinaba: // #{a+b == C} * 1 fbe763b30e 2016-05-26 kinaba: //---------------------------- fbe763b30e 2016-05-26 kinaba: // where fbe763b30e 2016-05-26 kinaba: // fbe763b30e 2016-05-26 kinaba: // #{a+b == k} fbe763b30e 2016-05-26 kinaba: //= fbe763b30e 2016-05-26 kinaba: // k-1 for k <= A fbe763b30e 2016-05-26 kinaba: // A for A+1<= k <= B fbe763b30e 2016-05-26 kinaba: // A-(k-B)+1 for B+1<= k <= A+B fbe763b30e 2016-05-26 kinaba: // 0 for A+B< k fbe763b30e 2016-05-26 kinaba: //---------------------------- fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: mint total = 0; fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: //for(int k=1; k<=min(C,A); ++k) fbe763b30e 2016-05-26 kinaba: // total += mint(k-1) * (C-k+1); fbe763b30e 2016-05-26 kinaba: { fbe763b30e 2016-05-26 kinaba: LL x = min(C,A); fbe763b30e 2016-05-26 kinaba: total += mint(0+x-1)*x/2 * C - mint(x-1)*x*(2*x-1)/6; fbe763b30e 2016-05-26 kinaba: } fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: // for(int k=A+1; k<=min(C,B); ++k) fbe763b30e 2016-05-26 kinaba: // total += mint(A) * (C-k+1); fbe763b30e 2016-05-26 kinaba: if(A+1 <= min(C,B)) { fbe763b30e 2016-05-26 kinaba: LL x = A+1, y = min(C,B); fbe763b30e 2016-05-26 kinaba: total += mint(A) * (C-x+1+C-y+1) * (y-x+1) / 2; fbe763b30e 2016-05-26 kinaba: } fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: //for(int k=B+1; k<=min(C,A+B); ++k) fbe763b30e 2016-05-26 kinaba: // total += mint(A-(k-B)+1) * (C-k+1); fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: if(B+1 <= min(C,A+B)) { fbe763b30e 2016-05-26 kinaba: LL X = min(C,A+B)-B, D = C-B; fbe763b30e 2016-05-26 kinaba: //for(LL k=0; k<=X; ++k) fbe763b30e 2016-05-26 kinaba: // total += mint(A-k)*(D-k); fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: LL E = abs(D-A); fbe763b30e 2016-05-26 kinaba: //for(LL k=0; k<=X; ++k) fbe763b30e 2016-05-26 kinaba: // total += mint(k)*(k+E); fbe763b30e 2016-05-26 kinaba: total += mint(X)*(X+1)/2*E + mint(X)*(X+1)*(2*X+1)/6; fbe763b30e 2016-05-26 kinaba: } fbe763b30e 2016-05-26 kinaba: return total; fbe763b30e 2016-05-26 kinaba: } fbe763b30e 2016-05-26 kinaba: }; fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: // BEGIN CUT HERE fbe763b30e 2016-05-26 kinaba: #include <ctime> fbe763b30e 2016-05-26 kinaba: double start_time; string timer() fbe763b30e 2016-05-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } fbe763b30e 2016-05-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) fbe763b30e 2016-05-26 kinaba: { os << "{ "; fbe763b30e 2016-05-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) fbe763b30e 2016-05-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } fbe763b30e 2016-05-26 kinaba: void verify_case(const int& Expected, const int& Received) { fbe763b30e 2016-05-26 kinaba: bool ok = (Expected == Received); fbe763b30e 2016-05-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; fbe763b30e 2016-05-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } fbe763b30e 2016-05-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); fbe763b30e 2016-05-26 kinaba: #define END verify_case(_, TriangleTriples().count(A, B, C));} fbe763b30e 2016-05-26 kinaba: int main(){ fbe763b30e 2016-05-26 kinaba: fbe763b30e 2016-05-26 kinaba: CASE(0) fbe763b30e 2016-05-26 kinaba: int A = 1; fbe763b30e 2016-05-26 kinaba: int B = 10; fbe763b30e 2016-05-26 kinaba: int C = 20; fbe763b30e 2016-05-26 kinaba: int _ = 10; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: CASE(2) fbe763b30e 2016-05-26 kinaba: int A = 10; fbe763b30e 2016-05-26 kinaba: int B = 10; fbe763b30e 2016-05-26 kinaba: int C = 10; fbe763b30e 2016-05-26 kinaba: int _ = 505; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: CASE(3) fbe763b30e 2016-05-26 kinaba: int A = 1; fbe763b30e 2016-05-26 kinaba: int B = 1; fbe763b30e 2016-05-26 kinaba: int C = 1; fbe763b30e 2016-05-26 kinaba: int _ = 1; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: CASE(1) fbe763b30e 2016-05-26 kinaba: int A = 2; fbe763b30e 2016-05-26 kinaba: int B = 2; fbe763b30e 2016-05-26 kinaba: int C = 1000000000; fbe763b30e 2016-05-26 kinaba: int _ = 6; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: CASE(4) fbe763b30e 2016-05-26 kinaba: int A = 123456789; fbe763b30e 2016-05-26 kinaba: int B = 987654321; fbe763b30e 2016-05-26 kinaba: int C = 555555555; fbe763b30e 2016-05-26 kinaba: int _ = 64296241; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: CASE(4) fbe763b30e 2016-05-26 kinaba: int A = 123456789; fbe763b30e 2016-05-26 kinaba: int B = 555555555; fbe763b30e 2016-05-26 kinaba: int C = 987654321; fbe763b30e 2016-05-26 kinaba: int _ = 64296241; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: CASE(4) fbe763b30e 2016-05-26 kinaba: int A = 555555555; fbe763b30e 2016-05-26 kinaba: int B = 987654321; fbe763b30e 2016-05-26 kinaba: int C = 123456789; fbe763b30e 2016-05-26 kinaba: int _ = 64296241; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: /* fbe763b30e 2016-05-26 kinaba: CASE(5) fbe763b30e 2016-05-26 kinaba: int A = ; fbe763b30e 2016-05-26 kinaba: int B = ; fbe763b30e 2016-05-26 kinaba: int C = ; fbe763b30e 2016-05-26 kinaba: int _ = ; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: CASE(6) fbe763b30e 2016-05-26 kinaba: int A = ; fbe763b30e 2016-05-26 kinaba: int B = ; fbe763b30e 2016-05-26 kinaba: int C = ; fbe763b30e 2016-05-26 kinaba: int _ = ; fbe763b30e 2016-05-26 kinaba: END fbe763b30e 2016-05-26 kinaba: */ fbe763b30e 2016-05-26 kinaba: } fbe763b30e 2016-05-26 kinaba: // END CUT HERE