Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4888
The problem of two parameters. Solution: enumerate the smaller parameter, and then the second parameter.
Think of binary, then the two parameters will not be and then the brute force, but in fact K can be very large, so time is not enough
Self-written binary enumeration + fast power program wa for a long time .... I don't understand what's wrong with someone else's reference...
//#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <iostream>#include <iomanip>#include <cmath>#include <map>#include <set>#include <queue>using namespace std;#define ls(rt) rt*2#define rs(rt) rt*2+1#define ll long long#define ull unsigned long long#define rep(i,s,e) for(int i=s;i<e;i++)#define repe(i,s,e) for(int i=s;i<=e;i++)#define CL(a,b) memset(a,b,sizeof(a))#define IN(s) freopen(s,"r",stdin)#define OUT(s) freopen(s,"w",stdout)const ll ll_INF = ((ull)(-1))>>1;const double EPS = 1e-8;const double pi = acos(-1.0);const int INF = 100000000;ll n,ansr,ansk;void test(ll d, ll up, int r){ ll sum; while(d<up) { ll mid=(d+up)/2; sum=0; ll know=1;//k^i int flag=0; for(int i=1;i<=r;i++) { know*=mid; sum+=know; if(sum>1000000000000LL || sum<0 ) { flag=1; break; } } if(flag) { up=mid; continue; } if(sum>n)up=mid; else d=mid+1; if(sum+1 == n || sum==n) { if(mid*r < ansr*ansk) ansr=r,ansk=mid; break; } }}int main(){ IN("zoj3665.txt"); while(~scanf("%lld",&n)) { ansr=1; ansk=n-1;// for(int i=2;i<=64;i++) test(1ll,1000000,i); printf("%lld %lld\n",ansr,ansk); } return 0;}
Zoj 3665 Binary Number Two Parameters