hdu 4282——A very hard mathematic problem

2 篇文章 0 订阅

题意:找出X^Z + Y^Z + XYZ = K的解的个数
思路:暴力枚举。枚举x和z,然后二分y。因为z最小是2。当z为2的时候,就是(x+y)^2=k,解可以预处理出来。剩下的就是z大于等于3。这时x满足x的三次方小于k。可以看出x不超过1400左右。如果x和y都取1,因为k最大是2^31,也就是说z不超过31。最大计算的次数就是31*1400再乘以二分的次数。二分次数不超过31次。
代码如下:

#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <string>
#include <queue>
#include <bitset>
#include<assert.h>
using namespace std;

typedef unsigned long long ll;

ll fpow(ll x,ll n){
    ll ret=1;
    while(n){
        if(n&1)ret*=x;
        x*=x;
        n>>=1;
    }
    return ret;
}

int main()
{
//    cout<<"my"<<endl;
//    freopen("data.txt","r",stdin);
    ll k;
    while(cin>>k&&k){
        ll ans=0;
        ll c = sqrt(k*1.0);
        if(c*c==k){
            ans+=(c-1)/2;
        }
        for(ll x=1;x<1400;++x){
            for(ll z=3;z<=31;++z){
                ll px =fpow(x,z);
                if(px>k)break;
                ll l=x+1;
                ll r = k;
                while(1){
                    ll yy=(l+r)/2;
                    ll pyy = fpow(yy,z);
                    ll cnt=px+pyy+x*yy*z;
                    if(cnt==k){
                        ans++;break;
                    } else if(cnt > k){
                        r = yy - 1;
                    } else if(cnt<k){
                        l = yy + 1;
                    }
                    if(l>r)break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值