File Annotation
Not logged in
17966b23b6 2015-02-10        kinaba: #include <iostream>
17966b23b6 2015-02-10        kinaba: #include <sstream>
17966b23b6 2015-02-10        kinaba: #include <iomanip>
17966b23b6 2015-02-10        kinaba: #include <vector>
17966b23b6 2015-02-10        kinaba: #include <string>
17966b23b6 2015-02-10        kinaba: #include <map>
17966b23b6 2015-02-10        kinaba: #include <set>
17966b23b6 2015-02-10        kinaba: #include <algorithm>
17966b23b6 2015-02-10        kinaba: #include <numeric>
17966b23b6 2015-02-10        kinaba: #include <iterator>
17966b23b6 2015-02-10        kinaba: #include <functional>
17966b23b6 2015-02-10        kinaba: #include <complex>
17966b23b6 2015-02-10        kinaba: #include <queue>
17966b23b6 2015-02-10        kinaba: #include <stack>
17966b23b6 2015-02-10        kinaba: #include <cmath>
17966b23b6 2015-02-10        kinaba: #include <cassert>
17966b23b6 2015-02-10        kinaba: #include <tuple>
17966b23b6 2015-02-10        kinaba: using namespace std;
17966b23b6 2015-02-10        kinaba: typedef long long LL;
17966b23b6 2015-02-10        kinaba: typedef complex<double> CMP;
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: static const unsigned MODVAL = 1000000007;
17966b23b6 2015-02-10        kinaba: struct mint
17966b23b6 2015-02-10        kinaba: {
17966b23b6 2015-02-10        kinaba: 	unsigned val;
17966b23b6 2015-02-10        kinaba: 	mint():val(0){}
17966b23b6 2015-02-10        kinaba: 	mint(int      x):val(x%MODVAL) {}
17966b23b6 2015-02-10        kinaba: 	mint(unsigned x):val(x%MODVAL) {}
17966b23b6 2015-02-10        kinaba: 	mint(LL       x):val(x%MODVAL) {}
17966b23b6 2015-02-10        kinaba: };
17966b23b6 2015-02-10        kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
17966b23b6 2015-02-10        kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
17966b23b6 2015-02-10        kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
17966b23b6 2015-02-10        kinaba: mint operator+(mint x, mint y) { return x+=y; }
17966b23b6 2015-02-10        kinaba: mint operator-(mint x, mint y) { return x-=y; }
17966b23b6 2015-02-10        kinaba: mint operator*(mint x, mint y) { return x*=y; }
17966b23b6 2015-02-10        kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: // cnt, ith
17966b23b6 2015-02-10        kinaba: typedef tuple<LL,LL> item;
17966b23b6 2015-02-10        kinaba: struct cmp {
17966b23b6 2015-02-10        kinaba: 	bool operator()(const item& lhs, const item& rhs) {
17966b23b6 2015-02-10        kinaba: 		LL e1,b1,e2,b2;
17966b23b6 2015-02-10        kinaba: 		tie(e1,b1) = lhs;
17966b23b6 2015-02-10        kinaba: 		tie(e2,b2) = rhs;
17966b23b6 2015-02-10        kinaba: 		// b1 2^e1 > b2 2^e2 ?
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 		LL e = abs(e1-e2);
17966b23b6 2015-02-10        kinaba: 		if(e1==e2)
17966b23b6 2015-02-10        kinaba: 			return b1 > b2;
17966b23b6 2015-02-10        kinaba: 		if(e1 > e2) {
17966b23b6 2015-02-10        kinaba: 			// b1 2^e > b2 ?
17966b23b6 2015-02-10        kinaba: 			LL bb=b1;
17966b23b6 2015-02-10        kinaba: 			for(int i=0; i<e; ++i) {
17966b23b6 2015-02-10        kinaba: 				bb *= 2;
17966b23b6 2015-02-10        kinaba: 				if(bb>b2) return true;
17966b23b6 2015-02-10        kinaba: 			}
17966b23b6 2015-02-10        kinaba: 			return false;
17966b23b6 2015-02-10        kinaba: 		} else {
17966b23b6 2015-02-10        kinaba: 			// b1 < b2 2^e ?
17966b23b6 2015-02-10        kinaba: 			LL bb=b2;
17966b23b6 2015-02-10        kinaba: 			for(int i=0; i<e; ++i) {
17966b23b6 2015-02-10        kinaba: 				bb *= 2;
17966b23b6 2015-02-10        kinaba: 				if(b1<bb) return true;
17966b23b6 2015-02-10        kinaba: 			}
17966b23b6 2015-02-10        kinaba: 			return false;
17966b23b6 2015-02-10        kinaba: 		}
17966b23b6 2015-02-10        kinaba: 	}
17966b23b6 2015-02-10        kinaba: };
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: class KitayutaMart { public:
17966b23b6 2015-02-10        kinaba: 	int lastPrice(int N, int K)
17966b23b6 2015-02-10        kinaba: 	{
17966b23b6 2015-02-10        kinaba: 		// Invariant: Can buy N apples with max in (K*2^(L-1), K*2^(R-1)]
17966b23b6 2015-02-10        kinaba: 		int L=0, R=N;
17966b23b6 2015-02-10        kinaba: 		while(R-L>1) {
17966b23b6 2015-02-10        kinaba: 			int C=(L+R)/2;
17966b23b6 2015-02-10        kinaba: 			(N<=max_buy(K, C) ? R : L) = C;
17966b23b6 2015-02-10        kinaba: 		}
17966b23b6 2015-02-10        kinaba: 		// L+1==R
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 		// Invariant: Can buy N apples with max in (LL*2^(R-1), RR*2^(R-1)]
17966b23b6 2015-02-10        kinaba: 		int Ll=K/2, RR=K;
17966b23b6 2015-02-10        kinaba: 		while(RR-Ll>1) {
17966b23b6 2015-02-10        kinaba: 			int CC=(Ll+RR)/2;
17966b23b6 2015-02-10        kinaba: 			(N<=max_buy(K, R, CC) ? RR : Ll) = CC;
17966b23b6 2015-02-10        kinaba: 		}
17966b23b6 2015-02-10        kinaba: 		//Ll+1==RR
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 		// Should be close enough. Do naively.
17966b23b6 2015-02-10        kinaba: 		LL total = K*L + Ll;
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 		priority_queue<item, vector<item>, cmp> Q;
17966b23b6 2015-02-10        kinaba: 		Q.emplace(R, RR);
17966b23b6 2015-02-10        kinaba: 		int c=R+1;
17966b23b6 2015-02-10        kinaba: 		for(LL div=2;; div*=2,++c) {
17966b23b6 2015-02-10        kinaba: 			total += Ll/div;
17966b23b6 2015-02-10        kinaba: 			Q.emplace(c, Ll/div+1);
17966b23b6 2015-02-10        kinaba: 			if(Ll/div==0)
17966b23b6 2015-02-10        kinaba: 				break;
17966b23b6 2015-02-10        kinaba: 		}
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 		int ans = 0;
17966b23b6 2015-02-10        kinaba: 		for(; total<=N; ++total) {
17966b23b6 2015-02-10        kinaba: 			LL cnt,ith;
17966b23b6 2015-02-10        kinaba: 			tie(cnt,ith) = Q.top();
17966b23b6 2015-02-10        kinaba: 			Q.pop();
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 			ans = (mint(ith)*POW(2,cnt-1)).val;
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 			if(ith==1)
17966b23b6 2015-02-10        kinaba: 				Q.emplace(cnt+1,ith);
17966b23b6 2015-02-10        kinaba: 			if(ith<K)
17966b23b6 2015-02-10        kinaba: 				Q.emplace(cnt, ith+1);
17966b23b6 2015-02-10        kinaba: 		}
17966b23b6 2015-02-10        kinaba: 		return ans;
17966b23b6 2015-02-10        kinaba: 	}
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 	LL max_buy(LL K, LL C) {
17966b23b6 2015-02-10        kinaba: 		return max_buy(K,C,K);
17966b23b6 2015-02-10        kinaba: 	}
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: 	LL max_buy(LL K, LL C, LL Km) {
17966b23b6 2015-02-10        kinaba: 		// Buy many apples with max<=Km*2^(C-1) ?
17966b23b6 2015-02-10        kinaba: 		LL total = K*(C-1) + Km;
17966b23b6 2015-02-10        kinaba: 		for(LL div=2; Km/div>=1; div*=2)
17966b23b6 2015-02-10        kinaba: 			total += Km/div;
17966b23b6 2015-02-10        kinaba: 		return total;
17966b23b6 2015-02-10        kinaba: 	}
17966b23b6 2015-02-10        kinaba: };
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: // BEGIN CUT HERE
17966b23b6 2015-02-10        kinaba: #include <ctime>
17966b23b6 2015-02-10        kinaba: double start_time; string timer()
17966b23b6 2015-02-10        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
17966b23b6 2015-02-10        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
17966b23b6 2015-02-10        kinaba:  { os << "{ ";
17966b23b6 2015-02-10        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
17966b23b6 2015-02-10        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
17966b23b6 2015-02-10        kinaba: void verify_case(const int& Expected, const int& Received) {
17966b23b6 2015-02-10        kinaba:  bool ok = (Expected == Received);
17966b23b6 2015-02-10        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
17966b23b6 2015-02-10        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
17966b23b6 2015-02-10        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
17966b23b6 2015-02-10        kinaba: #define END	 verify_case(_, KitayutaMart().lastPrice(N, K));}
17966b23b6 2015-02-10        kinaba: int main(){
17966b23b6 2015-02-10        kinaba: 
17966b23b6 2015-02-10        kinaba: CASE(0)
17966b23b6 2015-02-10        kinaba: 	int N = 3;
17966b23b6 2015-02-10        kinaba: 	int K = 1;
17966b23b6 2015-02-10        kinaba: 	int _ = 4;
17966b23b6 2015-02-10        kinaba: END
17966b23b6 2015-02-10        kinaba: CASE(1)
17966b23b6 2015-02-10        kinaba: 	int N = 3;
17966b23b6 2015-02-10        kinaba: 	int K = 2;
17966b23b6 2015-02-10        kinaba: 	int _ = 2;
17966b23b6 2015-02-10        kinaba: END
17966b23b6 2015-02-10        kinaba: CASE(2)
17966b23b6 2015-02-10        kinaba: 	int N = 5;
17966b23b6 2015-02-10        kinaba: 	int K = 3;
17966b23b6 2015-02-10        kinaba: 	int _ = 4;
17966b23b6 2015-02-10        kinaba: END
17966b23b6 2015-02-10        kinaba: CASE(3)
17966b23b6 2015-02-10        kinaba: 	int N = 1000000000;
17966b23b6 2015-02-10        kinaba: 	int K = 1;
17966b23b6 2015-02-10        kinaba: 	int _ = 570312504;
17966b23b6 2015-02-10        kinaba: END
17966b23b6 2015-02-10        kinaba: CASE(4)
17966b23b6 2015-02-10        kinaba: 	int N = 987654321;
17966b23b6 2015-02-10        kinaba: 	int K = 876543210;
17966b23b6 2015-02-10        kinaba: 	int _ = 493827168;
17966b23b6 2015-02-10        kinaba: END
17966b23b6 2015-02-10        kinaba: /*
17966b23b6 2015-02-10        kinaba: CASE(5)
17966b23b6 2015-02-10        kinaba: 	int N = ;
17966b23b6 2015-02-10        kinaba: 	int K = ;
17966b23b6 2015-02-10        kinaba: 	int _ = ;
17966b23b6 2015-02-10        kinaba: END
17966b23b6 2015-02-10        kinaba: CASE(6)
17966b23b6 2015-02-10        kinaba: 	int N = ;
17966b23b6 2015-02-10        kinaba: 	int K = ;
17966b23b6 2015-02-10        kinaba: 	int _ = ;
17966b23b6 2015-02-10        kinaba: END
17966b23b6 2015-02-10        kinaba: */
17966b23b6 2015-02-10        kinaba: }
17966b23b6 2015-02-10        kinaba: // END CUT HERE