hdu 4430 Yukari's Birthday
2013-09-28 20:32
369 查看
Yukari's Birthday
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1665 Accepted Submission(s): 341
[align=left]Problem Description[/align]
Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most important part, birthday cake! But it's a big challenge for them to place n candles on the top of the cake. As Yukari has lived for such a long long time, though she herself insists that she is a 17-year-old girl.
To make the birthday cake look more beautiful, Ran and Chen decide to place them like r ≥ 1 concentric circles. They place ki candles equidistantly on the i-th circle, where k ≥ 2, 1 ≤ i ≤ r. And it's optional to place at most one candle at the center of the cake. In case that there are a lot of different pairs of r and k satisfying these restrictions, they want to minimize r × k. If there is still a tie, minimize r.
[align=left]Input[/align]
There are about 10,000 test cases. Process to the end of file.
Each test consists of only an integer 18 ≤ n ≤ 1012.
[align=left]Output[/align]
For each test case, output r and k.
[align=left]Sample Input[/align]
18 111 1111
[align=left]Sample Output[/align]
1 17 2 10 3 10
// 因为 2^64-1 差不多是 10^18 所以 1+k^1+k^2+k^3+..+k^r == n ,r 不会很大 //所以我们可以枚举 r 接下来就是 二分查找 k 是不是存在 // 我们在计算 1+k^1+k^2+k^3+..+k^r 时 不要使用 等比公式 ,因为会超 long long // 我们可以一项项计算 1+k^1+k^2+k^3+..+k^r ; 在计算 k ^ i 时 要判断 是不是超n // 判断 k^i = k^(i-1) * k > n 所以 改为除法 k^(i-1) > n / k #include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<cmath> #pragma comment(linker,"/STACK:1024000000,1024000000") using namespace std ; #define LL long long LL n , kk , rr , m ; LL pow1( LL k , int r ) { LL ans = 1 , sum = 0 ; for( int i = 1 ; i <= r ;i++ ){ if( ans > n / k ) return n+1 ; ans *= k ; if( sum > n-ans ) return n+1 ; sum += ans ; } return sum ; } void dfs( int r ) { LL L , R , mid , sum ; L = 2 ; R = n-1 ; while( R >= L ) { mid = (R+L)/2 ; sum = pow1(mid,r) ; if( sum == n || sum == n-1 ) break ; if( sum > n ) R = mid-1 ; else L = mid+1 ; } LL k = mid ; if( L > R ) return ; if( sum == n || sum == n-1 ) { if(k*r < rr*kk) { rr = r ; kk = k ; } return ; } } int main() { int i ; while( scanf("%I64d" , &n ) != EOF ) { m = (LL)sqrt(n*1.0) ; kk = n-1 ; rr = 1 ; for( int j = 2 ; j <= 47 ;j++ ) dfs(j) ; // hdu 用 lld 会wa .... printf("%I64d %I64d\n" ,rr , kk ) ; } }
相关文章推荐
- android+lucene实现全文检索并高亮关键字
- 设计模式与架构的一些话
- 点击Android系统back键
- C#手动加载类库和释放类库的代码
- 百度2014校招笔试题(二)
- 2014百度笔试现场突发奇想(就收购搜狗来说)
- servlet2.5 文件上传和下载简单讲解
- [置顶] 基础算法荟萃目录
- gcc编译器
- 哈夫曼编码(备忘)
- TCP窗口知识汇总
- 编译程序与操作系统的关系
- dojo第一天
- Markdown 语法说明
- 贝叶斯及其部分应用
- 图解SQL视频
- Linux Performance Analysis and Tools(Linux性能分析和工具)
- 后缀数组的实现和字符串匹配
- AES相关资料
- Linux 设备文件与udev