zoukankan      html  css  js  c++  java
  • POJ 1840 Eqs 二分+map/hash

    Description

    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

    The only line of input contains the 5 coefficients a1, a2, a3, a4, a5, separated by blanks.

    Output

    The output will contain on the first line the number of the solutions for the given equation.

    Sample Input

    37 29 41 43 47

    Sample Output

    654

    题意:
    给出5个数(<=50)a1,a2,a3,a4,a5 ,分别与5个未知数的3次方 联立方程=0 为a1x1^3+ a2x2^3+a3x3^3+ a4x4^3+ a5x5^3=0  |xi|<=50并xi!=0 求有多少组解。
    题解
    二分+map标记,先暴力出x1,x2,x3对应的a1x13+ a2x23+ a3x33 ; 存入数组中,再对应暴力 去 二分查找出等于 负的a4*x43次方+a5*x53次方 相应的下标 及对应个数;

    代码:

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <ctime>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <set>
     8 #include <vector>
     9 #include <queue>
    10 #include <map>
    11 #include <stack>
    12 #define MOD 1000000007
    13 #define maxn 20000001
    14 using namespace std;
    15 typedef long long LL;
    16 int read()
    17 {
    18     int x=0,f=1;
    19     char ch=getchar();
    20     while(ch<'0'||ch>'9')
    21     {
    22         if(ch=='-')f=-1;
    23         ch=getchar();
    24     }
    25     while(ch>='0'&&ch<='9')
    26     {
    27         x=x*10+ch-'0';
    28         ch=getchar();
    29     }
    30     return x*f;
    31 }
    32 //*******************************************************************
    33 __int64 a[1000005];
    34 map< int ,int > mp;
    35 int t;
    36 int jug(__int64 x)
    37 {
    38 
    39     int l=0;
    40     int r=t;
    41     int xx;
    42     int mid;
    43     while(l<=r)
    44     {
    45         mid=(l+r)/2;
    46         if(a[mid]>x)
    47         {
    48             r=mid-1;
    49         }
    50         else if(a[mid]<x)
    51         {
    52             l=mid+1;
    53             if(a[l]==x)return mp[x];
    54         }
    55         else return mp[x];
    56     }
    57   return 0;
    58 }
    59 int main()
    60 {
    61 
    62     int a1,a2,a3,a4,a5;
    63     t=0;
    64     scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
    65     for(int x1=-50; x1<=50; x1++)
    66     {
    67         if(x1==0) continue;
    68         for(int x2=-50; x2<=50; x2++)
    69         {
    70             if(x2==0)continue;
    71             a[++t]=(a1*x1*x1*x1+a2*x2*x2*x2);
    72             if(mp.count(a[t]))
    73             mp[a[t]]++;
    74             else mp[a[t]]=1;
    75         }
    76     }
    77     sort(a+1,a+t+1);
    78     int ans=0;
    79     for(int x3=-50; x3<=50; x3++)
    80     {
    81         if(x3==0)continue;
    82         for(int x4=-50; x4<=50; x4++)
    83         {
    84             if(x4==0) continue;
    85             for(int x5=-50; x5<=50; x5++)
    86             {
    87                 if(x5==0) continue;
    88                 __int64 aaa=-1*(a3*x3*x3*x3+x4*a4*x4*x4+a5*x5*x5*x5);
    89                 ans+=jug(aaa);
    90             }
    91         }
    92     }
    93     printf("%d
    ",ans);
    94     return 0;
    95 }
      这是哈希标记法
     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <ctime>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <set>
     8 #include <vector>
     9 #include <queue>
    10 #include <map>
    11 #include <stack>
    12 #define maxn 25000000
    13 #define inf 1000000007
    14 using namespace std;
    15 typedef long long LL;
    16 int read()
    17 {
    18     int x=0,f=1;
    19     char ch=getchar();
    20     while(ch<'0'||ch>'9')
    21     {
    22         if(ch=='-')f=-1;
    23         ch=getchar();
    24     }
    25     while(ch>='0'&&ch<='9')
    26     {
    27         x=x*10+ch-'0';
    28         ch=getchar();
    29     }
    30     return x*f;
    31 }
    32 //**********************************************************
    33 
    34 short  hash[25000001];
    35 int main()
    36 {
    37     int a1,a2,a3,a4,a5,x1,x2,x3,x4,x5,sum;
    38     scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
    39     memset(hash,0,sizeof(hash));
    40     for(x1=-50; x1<=50; x1++)
    41     {
    42         if(x1==0)
    43             continue;
    44         for(x2=-50; x2<=50; x2++)
    45         {
    46             if(x2==0)
    47                 continue;
    48             sum=(a1*x1*x1*x1+a2*x2*x2*x2)*-1;
    49             if(sum<0)sum+=maxn;
    50                 hash[sum]++;
    51         }
    52     }
    53     int cnt = 0;
    54     for(x3=-50; x3<=50; x3++)
    55     {
    56         if(x3==0)
    57             continue;
    58         for(x4=-50; x4<=50; x4++)
    59         {
    60             if(x4==0)
    61                 continue;
    62             for(x5=-50; x5<=50; x5++)
    63             {
    64                 if(x5==0)
    65                     continue;
    66                 sum=a3*x3*x3*x3+a4*x4*x4*x4+a5*x5*x5*x5;
    67                 if(sum<0)sum+=maxn;
    68                     cnt+=hash[sum];
    69             }
    70         }
    71     }
    72     printf("%d
    ",cnt);
    73     return 0;
    74 }
    
    
  • 相关阅读:
    Centos7 ftp服务器搭建
    搭建单机版spark
    JAVA JDBC大数据量导入Mysql
    springboot使用自带连接池连接postgre
    linux centos6安装postgresql
    SpringBoot中配置不序列化返回值为null的属性
    sublime 3 安装格式化JSON插件
    HIve分组查询返回每组的一条记录
    FastJson乱序问题
    深入理解迪杰斯特拉算法
  • 原文地址:https://www.cnblogs.com/zxhl/p/4655903.html
Copyright © 2011-2022 走看看