POJ1840

题目大意:已知方程a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,其中ai的范围在【-50,50】中,xi的范围在【-50,50】中且xi都不为0,(i=1,2,3,4,5)。求在给定a1,a2,a3,a4,a5的值,有多少种使得方程成立的情况即x1,x2,x3,x4,x5有多少种可能的取值。

本题思路:大部分的人看到题后,都会直接枚举x1,x2,x3,x4,x5,但枚举五个变量有100的5次方次,肯定会超时。

所以需要用到方程变形  a3x33+ a4x43+ a5x5= -a1x13_ a2x23      

先枚举x1,x2,把结果保存在hash表中,然后再枚举x3,x4,x5,在哈希表中找结果,如果满足上述的方程就记录下来。

#include<stdio.h>
#include<string.h>
const int maxn=6;
const int MP=1250319;
struct node
{
    int v;
    node* next;
};
int x[maxn];
int a[maxn];
node* pt;
node* hash[MP+1];
int ans;
int main()
{
    int t,p;
    memset(hash,0,sizeof(hash));
    for(int i=0; i<5; i++)
        scanf("%d",&a[i]);
    for(x[0]=-50; x[0]<=50; x[0]++)
    {
        if(x[0])
        {
            for(x[1]=-50; x[1]<=50; x[1]++)
            {
                if(x[1])
                {
                    t=x[0]*x[0]*x[0]*a[0]+x[1]*x[1]*x[1]*a[1];
                    p=(t>0? t:-t)%MP;
                    pt=new node;
                    pt->v=t;
                    pt->next=hash[p];
                    hash[p]=pt;
                }
            }
        }
    }
   ans=0;
    for(x[2]=-50; x[2]<=50; x[2]++)
        if(x[2])
            for(x[3]=-50; x[3]<=50; x[3]++)
                if(x[3])
                    for(x[4]=-50; x[4]<=50; x[4]++)
                        if(x[4])
                        {
                            t=x[2]*x[2]*x[2]*a[2]+x[3]*x[3]*x[3]*a[3]+x[4]*x[4]*x[4]*a[4];
                            p=(t>0?t:-t)%MP;
                            pt=hash[p];
                            while(pt)
                            {
                                if(pt->v==t)
                                    ans++;
                                pt=pt->next;
                            }


                        }
    printf("%d\n",ans);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值