zoukankan      html  css  js  c++  java
  • poj 1840 Eqs (Hash)

    /*
    这题10^8的暴力可以出答案 但是 慢.....
    有个小小的bug 出题人卡int 却没注意short
    用short可以不用hash 直接搞 
    49428K 313MS
    */ #include<iostream> #include<cstdio> #include<cstring> #define base 12500000 using namespace std; int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,ans; short f[12500000*2+100]; int main() { scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); for(x1=-50;x1<=50;x1++)if(x1) for(x2=-50;x2<=50;x2++)if(x2) { int s=a1*x1*x1*x1+a2*x2*x2*x2; s+=base;f[s]++; } for(x3=-50;x3<=50;x3++)if(x3) for(x4=-50;x4<=50;x4++)if(x4) for(x5=-50;x5<=50;x5++)if(x5) { int s=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5; if(s>12500000||s<-12500000)continue; s=-s;s+=base;ans+=f[s]; } printf("%d ",ans); return 0; }
    /*
    突然觉得自己没学过hash一样.....
    这题的问题在于 两部分计算出的答案需要存一下
    但是数很大 作为下标存不下 那就行办法叫他不做下标
    对于每个值s 计算它的hsah值 把这个值控制在一定范围里
    当然这样一搞很大概率会重复 不要紧 我们用边表挨着存下来
    把hash值作为点的编号 然后指向s
    查找某个s有几个对应的 的时候 就把s的hash值指向的值遍历一下
    统计有几个与s相同的 
    12412K 891MS
    */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 1000010 #define mod 1000007 using namespace std; int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,ans; int head[maxn],num; struct node{int v,pre;}e[maxn]; void Add(int to) { int x;x=to>0?to:-to; int from=(x%mod+x/mod)%mod; num++;e[num].v=to; e[num].pre=head[from]; head[from]=num; } int find(int to) { int ret=0;int x;x=to>0?to:-to; int from=(x%mod+x/mod)%mod; for(int i=head[from];i;i=e[i].pre) if(e[i].v==to)ret++; return ret; } int main() { scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5); for(x1=-50;x1<=50;x1++)if(x1) for(x2=-50;x2<=50;x2++)if(x2) for(x3=-50;x3<=50;x3++)if(x3) { int s=a1*x1*x1*x1+a2*x2*x2*x2+a3*x3*x3*x3; Add(s); } for(x4=-50;x4<=50;x4++)if(x4) for(x5=-50;x5<=50;x5++)if(x5) { int s=a4*x4*x4*x4+a5*x5*x5*x5; ans+=find(s); } printf("%d ",ans); return 0; }
    /*最后贴一下我同桌的 二分查找 704K 1907MS */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #define maxn 10010
    using namespace std;
    int a[6],x[6],f[200],tot,ans,p[maxn];
    int main()
    {
        int i,j,k;
        for(i=1;i<=5;i++)
        scanf("%d",&a[i]);
        for(x[1]=-50;x[1]<=50;x[1]++)
        {
            if(x[1]==0)continue;
            for(x[2]=-50;x[2]<=50;x[2]++)
            {
                if(x[2]==0)continue;
                int sum=a[1]*x[1]*x[1]*x[1]+a[2]*x[2]*x[2]*x[2];
                p[++tot]=sum;
            }
        }
        sort(p+1,p+tot+1);
        for(x[3]=-50;x[3]<=50;x[3]++)
        {        
            if(x[3]==0)continue;
            for(x[4]=-50;x[4]<=50;x[4]++)
            {
                if(x[4]==0)continue;
                for(x[5]=-50;x[5]<=50;x[5]++)
                {
                    if(x[5]==0)continue;
                    int sum=a[3]*x[3]*x[3]*x[3]+a[4]*x[4]*x[4]*x[4]+a[5]*x[5]*x[5]*x[5];
                    ans+=(upper_bound(p+1,p+tot+1,-sum)-p)-(lower_bound(p+1,p+tot+1,-sum)-p);
                }        
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    js转化 保留2位小数
    python练习:打印九九乘法表
    PyCharm常用快捷键及工具
    python关键字
    Python学习资源
    Jira项目导入,被导入项目与目的系统数据类型不一致导入不成功的解决方案
    压测的时候到底要不要加集合点?
    Java Vuser协议JDBC脚本编写(MySQL)
    eclipse工具使用
    oracle忘记sys,system密码的解决方法
  • 原文地址:https://www.cnblogs.com/yanlifneg/p/5766617.html
Copyright © 2011-2022 走看看