hrbust 1013 Eqs【二分】

来源:互联网 发布:bt离线下载软件 编辑:程序博客网 时间:2024/05/16 04:11

EqsTime Limit: 5000 MSMemory Limit: 65536 KTotal Submit: 585(236 users)Total Accepted: 341(219 users)Rating: Special Judge: NoDescription

Consider equations having the following form: 
a1x13+ a2x23+ a3x33+ a4x43+ a5x53=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 Input37 29 41 43 47Sample Output654

题目大意:

输入五个数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
原创粉丝点击