hdu 4430 枚举+二分 范围运算失误
来源:互联网 发布:linux ftp命令 带端口 编辑:程序博客网 时间:2024/05/29 08:09
题目:
Yukari's Birthday
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5683 Accepted Submission(s): 1370
Problem Description
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.
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.
Input
There are about 10,000 test cases. Process to the end of file.
Each test consists of only an integer 18 ≤ n ≤ 1012.
Each test consists of only an integer 18 ≤ n ≤ 1012.
Output
For each test case, output r and k.
Sample Input
181111111
Sample Output
1 172 103 10
总共有N支蜡烛,要把它们放在蛋糕上 蜡烛必须放成一个同心圆的形状,圆心可以选择放一个蜡烛或者不放 从里到外第i层圆可以放k^i支蜡烛
问怎样放(选择K咯)可以使得r*k最小 如果有多种方案,选择r最小的方案 输出K和R
分析:
队友想到了用枚举+二分.....我们分析得k最小为2时,r最大也不会超过log(1e12)/log(2)=39 因此r的范围是1-39 而k的范围是2-1e12,所以应该枚举r,二分k,找出所有刚好能将蜡烛摆好的情况,记录最小值就可以了.....
代码:
二分写的太少了,实现花了好久.....
依然WA代码
#include<bits/stdc++.h>using namespace std;#define ll long longconst ll inf=(ll)1<<63-1;//不减一是负数!!!!!!!!!// 注意long long 输出格式,win测评系统用%I64d,linux 用%lldint main(){// ll n; while(scanf("%lld",&n)==1){ ll R,K,RK=inf;//RK初始化为(ll)1<<63爆炸 ll t,ans; for(ll i=1;i<=64;++i){//枚举R ll l=2,r=1e6+30;//这里的r和之前的r重名...debug n遍 .......... ll mid; while(l<=r){//二分K mid=(l+r)>>1; t=1,ans=0; int f=0; for(ll j=1;j<=i;++j){ t*=mid; ans+=t; if(ans>n){f=1; break;} } if(f){//mid太大了 r=mid-1; } else if(ans==n){ //cout<<"i=:"<<i<<" mid=:"<<mid<<" i*mid:"<<i*mid<<" RK=:"<<RK<<endl; if(i*mid<RK){ R=i; K=mid; RK=i*mid;}//之前没更新RK SB..... else if((i*mid==RK&&i<R)){ R=i; K=mid; RK=i*mid;}//之前以为这种情况包含在上一种情况里面了... break; //找到了=n的接下来也有可能找到=n-1的 而这里不break二分又不能处理,所以=n和=n-1的情况分开处理比较好 } else l=mid+1; } } for(ll i=1;i<=64;++i){//枚举R ll l=2,r=1e6+30;//这里的r和之前的r重名...debug n遍 .......... ll mid; while(l<=r){//二分K mid=(l+r)>>1; t=1,ans=0; int f=0; for(ll j=1;j<=i;++j){ t*=mid; ans+=t; if(ans>n){f=1; break;} } if(f){//mid太大了 r=mid-1; } else if(ans==(n-1)){ //cout<<"i=:"<<i<<" mid=:"<<mid<<" i*mid:"<<i*mid<<" RK=:"<<RK<<endl; if(i*mid<RK){ R=i; K=mid; RK=i*mid;} else if((i*mid==RK&&i<R)){ R=i; K=mid; RK=i*mid;} break; } else l=mid+1; } } printf("%lld %lld\n",R,K); }return 0;}
因为参数r 以及i==1的情况没有特殊处理,疯狂WA
AC代码:
#include<bits/stdc++.h>using namespace std;#define ll long longconst ll inf=(ll)1<<63-1;//不减一是负数!!!!!!!!!// 注意long long 输出格式,win测评系统用%I64d,linux 用%lldint main(){//483MS1688K ll n; while(scanf("%lld",&n)==1){ ll R=1,K=n-1,RK=n-1;//RK初始化为(ll)1<<63爆炸 ll t,ans; for(ll i=2;i<=40;++i){//枚举R ll l=2,r=1e6+30;//这里的r和之前的r重名...debug n遍 .......... //r=1e12+30 WA ????????????????????????????????????? ll mid; while(l<=r){//二分K mid=(l+r)>>1; t=1,ans=0; int f=0; for(ll j=1;j<=i;++j){ t*=mid; ans+=t; if(ans>n){f=1; break;} } if(f){//mid太大了 r=mid-1; } else if(ans==n){ //cout<<"i=:"<<i<<" mid=:"<<mid<<" i*mid:"<<i*mid<<" RK=:"<<RK<<endl; if(i*mid<RK){ R=i; K=mid; RK=i*mid;}//之前没更新RK SB..... else if((i*mid==RK&&i<R)){ R=i; K=mid; RK=i*mid;}//之前以为这种情况包含在上一种情况里面了... break; //找到了=n的接下来也有可能找到=n-1的 而这里不break二分又不能处理,所以=n和=n-1的情况分开处理比较好 } else l=mid+1; } } for(ll i=2;i<=40;++i){//枚举R ll l=2,r=1e6+30;//这里的r和之前的r重名...debug n遍 .......... ll mid; while(l<=r){//二分K mid=(l+r)>>1; t=1,ans=0; int f=0; for(ll j=1;j<=i;++j){ t*=mid; ans+=t; if(ans>n){f=1; break;} } if(f){//mid太大了 r=mid-1; } else if(ans==(n-1)){ //cout<<"i=:"<<i<<" mid=:"<<mid<<" i*mid:"<<i*mid<<" RK=:"<<RK<<endl; if(i*mid<RK){ R=i; K=mid; RK=i*mid;} else if((i*mid==RK&&i<R)){ R=i; K=mid; RK=i*mid;} break; } else l=mid+1; } } cout<<R<<" "<<K<<endl;//两种方式都可以 //printf("%lld %lld\n",R,K); }return 0;}
队友1A代码,先贴过来:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <string>#include <vector>#include <stack>#include <bitset>#include <set>#include <list>#include <deque>#include <map>#include <queue>#define Mod 1000000007using namespace std;typedef long long ll;const double PI=acos(-1.0);const double eps=1e-6;const int INF=1000000000;const int maxn=100;ll n;ll bi_search(int r, ll ans){ll left=2;ll right=ans;while(left<=right){ll mid=(left+right)>>1;double t=(pow(mid*1.0,r+1)-mid)/(mid+1);if(t>((double)ans)){right=mid-1;continue;}ll temp=0;ll tt=1;for(int i=0;i<r;i++){tt*=mid;temp+=tt;}if(temp==ans)return mid;else if(temp<ans)left=mid+1;else right=mid-1;}return 0;}int main(){ll n;ll ans,k;int ar;while(scanf("%lld",&n)!=EOF){ans=n-1;ar=1;k=n-1;for(int r=2;r<=40;r++){ll t=bi_search(r,n);if(t==0)continue;if(r*t<ans){ans=r*t;ar=r;k=t;}else if(r*t==ans&&r<ar){ar=r;k=t;}}for(int r=2;r<=40;r++){ll t=bi_search(r,n-1);if(t==0)continue;if(r*t<ans){ans=r*t;ar=r;k=t;}else if(r*t==ans&&r<ar){ar=r;k=t;}}printf("%d %lld\n",ar,k);}return 0;}
阅读全文
0 0
- hdu 4430 枚举+二分 范围运算失误
- hdu 4430 枚举+二分
- hdu 4430 二分+枚举
- hdu 4430 Yukari's Birthday 枚举+二分
- hdu 4430 Yukari's Birthday(二分+枚举)
- Hdu 4430 Yukari's Birthday 枚举+二分
- HDU 4430 Yukari's Birthday (二分+枚举)
- zoj 3665 hdu 4430 (二分枚举)
- HDU 4430 Yukari's Birthday 枚举+二分
- HDU 4430 Yukari's Birthday(枚举+二分)
- HDU 4430 Yukari's Birthday【二分+枚举】
- HDU 4430 Yukari's Birthday (枚举 二分)
- HDU 5676(枚举 + 二分)
- hdu 5265(二分+枚举)
- HDU - 6012 枚举 + 二分
- hdu 4282 枚举,非二分
- HDU 4430 & ZOJ 3665 Yukari's Birthday(二分+枚举)
- HDU 4430 Yukari's Birthday(枚举+二分)
- hadoop 2.8 hdfs 命令错误总结
- Android中继承AppCompatActivity后如何实现无标题全屏显示
- Centos 安装ClamAv 杀毒软件
- 聊天系统实战记录
- 有对象的程序猿都是怎么写代码的
- hdu 4430 枚举+二分 范围运算失误
- Python3.x中出现 "AttributeError: module 'urllib' has no attribute 'urlopen'" 的解决方法
- 几个性能测试工具
- 数据库审核系统和防统方系统的异同
- C语言学习_C Primer Plus:关于声明变量和printf的先后问题
- CentOS-CentOS版本查询命令
- 图像局部不变性特征提取与描述
- Netty的高性能及NIO的epoll空轮询bug
- 【Spring】Spring Framework Reference Documentation中文版24