大数乘法小心数据溢出

HDU 4430 Yukari’s Birthday

 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 k i 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 ≤ 10 12.
Output
For each test case, output r and k.
Sample Input

18
111
1111

Sample Output

1 17
2 10
3 10

这是一道可以用二分做的题目,但是与经典的二分题目不大一样。题目的大概思路就是蛋糕上面插蜡烛,已知蜡烛的总数为n,最中间可以放一支蜡烛或者不放,求实r*k最小的r,k序列,如果有多个r*k值相同的那么取最小值,进一步求r最小的序列。(遍历r(2~40),二分k)
在做题的 过程中,有数据溢出的case,所以可以限定二分的st和ed;
st=1;ed=( long long )pow(1e13,1.0/r);或者在求sum(等比数列求和)时对r每一项ai和sum都去判断,也可以防止溢出,
代码如下:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define INF 0x3f3f3f3f
#define MAX 100
using namespace std;
//开根号
long long n;

int check(long long  mid,long long r){
    long long sum=0,res=1;
    for(int i=1;i<=r;i++){
        res*=mid;
        sum+=res;
        if(sum>n)break;
    }
    if(sum==n||sum==n-1)return 1;
    else if(sum<n-1)return 2;
    else return 3;
}

int main(){
    while(cin>>n){
    long long ansr=1,ansk=n-1,tempk=n-1;//res=ansr*ansk,
    for(long long r=2;r<=50;r++){
        long long st=0,ed=(long long)pow(1e13,1.0/r)+1;
        while(ed-st>1){
            long long mid=st+(ed-st)/2;
            if(check(mid,r)==1){tempk=mid;break;}
            else if(check(mid,r)==2)st=mid;
            else if(check(mid,r)==3)ed=mid;
        }
        if((ansr*ansk>tempk*r)){ansr=r;ansk=tempk;}//res=r*tempk;
    }
        printf("%lld %lld\n",ansr,ansk);}
    return 0;
}
//不开根号
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define INF 0x3f3f3f3f
#define MAX 100
using namespace std;
long long n;

int check(long long  mid,long long r){
    long long sum=0,res=1;
    for(int i=1;i<=r;i++){
        if(n/res<mid)return 3;
        res*=mid;
        if(res>n||res<0)return 3;
        sum+=res;
        if(sum>n)break;
    }
    if(sum==n||sum==n-1)return 1;
    else if(sum<n-1)return 2;
    else return 3;
}

int main(){
    while(cin>>n){
    long long ansr=1,ansk=n-1,tempk=n-1;//res=ansr*ansk,
    for(long long r=2;r<=50;r++){
        long long st=0,ed=n;//ed=(long long)pow(1e13,1.0/r)+1;
        while(ed-st>1){
            long long mid=st+(ed-st)/2;
            if(check(mid,r)==1){tempk=mid;break;}
            else if(check(mid,r)==2)st=mid;
            else if(check(mid,r)==3)ed=mid;
        }
        if((ansr*ansk>tempk*r)){ansr=r;ansk=tempk;}//res=r*tempk;
    }
        printf("%lld %lld\n",ansr,ansk);}
    return 0;
}

真的好气哦。。。继续写四则运算计算器。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值