Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- using namespace std;
- /// Compiles just fine (TM) with G++ 4.7 in -std=c++0x mode
- typedef unsigned __int128 llu;
- typedef long long unsigned ull;
- llu bb(llu x){
- llu best=x;
- if (x==1) return 1;
- llu k=1,kb=2;
- while (kb<=x){
- llu l=k,r=(__int128)-1; r>>=(__int128)2;
- while (l<r){
- llu n=(l>>(__int128)1)+(r>>(__int128)1)+(l&r&1);
- llu res=1;
- for (llu m=1; m<=k and res<=x; m++)
- res*=(m+n-k),res/=m;
- if (res==x){
- best=min(best,n);
- break;
- }
- if (res<x) l=n+1;
- else r=n;
- }
- ++k;kb*=(k*2-1)*(k*2),kb/=k*k;
- }
- return best;
- }
- int main(){
- if(freopen("checkpoint.in","r",stdin));
- if(freopen("checkpoint.out","w",stdout));
- int tsts; if(scanf("%d",&tsts)); for (int tst=1; tst<=tsts; tst++){
- llu ways; ull proxy; if(scanf("%llu",&proxy)); ways=proxy;
- llu best=-1;
- for (llu i=1; i*i<=ways; i++)
- if (ways%i==0)
- best=min(best,bb(i)+bb(ways/i));
- printf("Case #%d: %llu\n",tst,(ull)best);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement