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