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