hrbust 1013 Eqs【二分】

Eqs
Time Limit: 5000 MSMemory Limit: 65536 K
Total Submit: 585(236 users)Total Accepted: 341(219 users)Rating: Special Judge: No
Description
Consider equations having the following form: 
a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=0 
The coefficients are given integers from the interval [-50,50]. 
It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}. 

Determine how many solutions satisfy the given equation.
Input

For each test case :

Line 1: Five coefficients a1, a2, a3, a4, a5, separated by blanks.

Process to the end of file.

Output

For each test case :

Line 1: A single integer that is the number of the solutions for the given equation.(The output will fit in 32 bit signed integers.)

Sample Input
37 29 41 43 47
Sample Output
654

题目大意:

输入五个数a1,a2,a3,a4,a5,对应公式:a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0 ,其中一个合法的【x1,x2,x3,x4,x5】,就是一个可行解,对应已知每个xi的范围都是【-50,,50】&&xi!=0,问一共有多少个可行解。


思路:


1、首先考虑将整个式子拆分:

a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0-------------->a1x1^3+ a2x2^3+ a3x3^3=-(a4x4^3+ a5x5^3);

①那么我们三层forO(100^3)来枚举左边的和。记录到数组a【i】中。

②然后我们两层forO(100^2)来枚举右边的和。记录到数组b【i】中。


2、那么此时a【i】的数据量为1e6,b【i】的数据量为1e4,那么我们可以考虑枚举b【i】的值,然后在a数组中二分查找最左边出现-b【i】的位子记为zuo,再二分查找最右边出现-b【i】的位子记为you,那么ans+=you-zuo+1。时间复杂度O(10^4log10^6);明显是5000ms能够承受的一个时间复杂度。

总时间复杂度:O(100^3+100^2+10^4log10^6),其实我们也可以考虑枚举a【i】的值,来二分查找b数组出现-a【i】的左右位子,然后记录答案,其时间复杂度:O(100^3+100^2+10^6log10^4),其实看起来是差不多的。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1004700];
int b[147000];
int Erfendd(int tmp)
{
    int l=0;
    int r=1000000-1;
    int mid;
    int ans=-1;
    while(r>=l)
    {
        mid=(l+r)/2;
        if(a[mid]>=tmp)
        {
            r=mid-1;
            if(a[mid]==tmp)
            {
                ans=mid;
            }
        }
        else l=mid+1;
    }
    return ans;
}
int Erfenuu(int tmp)
{
    int l=0;
    int r=1000000-1;
    int mid;
    int ans=-1;
    while(r>=l)
    {
        mid=(l+r)/2;
        if(a[mid]>tmp)
        {
            r=mid-1;
        }
        else
        {
            l=mid+1;
            if(a[mid]==tmp)
            {
                ans=mid;
            }
        }
    }
    return ans;
}
int main()
{
    int a1,a2,a3,a4,a5;
    while(~scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5))
    {
        int cont=0;
        memset(a,0,sizeof(a));
        for(int i=-50;i<=50;i++)
        {
            for(int j=-50;j<=50;j++)
            {
                for(int k=-50;k<=50;k++)
                {
                    if(i==0||j==0||k==0)continue;
                    a[cont++]=i*i*i*a1+j*j*j*a2+k*k*k*a3;
                }
            }
        }
        int cont2=0;
        for(int i=-50;i<=50;i++)
        {
            for(int j=-50;j<=50;j++)
            {
                if(i==0||j==0)continue;
                b[cont2++]=(i*i*i*a4+j*j*j*a5);
            }
        }
        sort(a,a+cont);
        int ans=0;
        for(int i=0;i<cont2;i++)
        {
            int zuo=Erfendd(-b[i]);
            if(zuo==-1)continue;
            int you=Erfenuu(-b[i]);
            ans+=you-zuo+1;
        }
        printf("%d\n",ans);
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值