zoukankan      html  css  js  c++  java
  • POJ 1840, Eqs

    Time Limit: 5000MS  Memory Limit: 65536K
    Total Submissions: 3409  Accepted: 1514


    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

    Source
    Romania OI 2002


    // POJ1840.cpp : Defines the entry point for the console application.
    //

    #include 
    <iostream>
    using namespace std;

    struct Node
    {
        Node():count(
    0), num(0), next(NULL){}
        
    int num;
        
    int count;
        Node
    * next;
    };

    int main(int argc, char* argv[])
    {
        
    int a[5];
        cin 
    >> a[0>> a[1>> a[2>> a[3>> a[4];

        
    const int SIZE = 33119;
        Node hash[SIZE];

        
    //create lookup table
        int X[51];
        
    for (int i = 0; i <=50++i) X[i] = i * i * i;

        
    //init hash table
        for (int i = -50; i <=50++i)
            
    for (int j = -50; j <= 50++j)
                
    if (i != 0 && j != 0)
                {
                    
    int num = a[0* (i < 0 ? -X[-i]:X[i]) + a[1* (j < 0 ? -X[-j]:X[j]);
                    
    int key = num % SIZE;
                    
    if (key < 0) key += SIZE;
                    Node
    * pt = &hash[key];
                    
    bool found = false;
                    
    while (pt->next != NULL)
                    {
                        pt 
    = pt->next;
                        
    if (pt->num == num)
                        {
                            
    ++(pt->count);
                            found 
    = true;
                            
    break;
                        }
                    }
                    
    if (found == false)
                    {
                        Node
    * pc = new Node;
                        pc
    ->count = 1;
                        pc
    ->num = num;
                        pt
    ->next = pc;        
                    };
                };

        
    //search hash table and find match
        int cnt = 0;
        
    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)
                    {
                        
    int num = -(a[2* (i < 0 ? -X[-i]:X[i]) +
              a[3* (j < 0 ? -X[-j]:X[j]) + a[4* (k < 0 ? -X[-k]:X[k]));
                        
    int key = num % SIZE;
                        
    if (key < 0) key += SIZE;

                        Node
    * pt = &hash[key];
                        
    while (pt->next != NULL)
                        {
                            pt 
    = pt->next;
                            
    if (pt->num == num)
                            {
                                cnt 
    += pt->count;
                                
    break;
                            }
                        }
                    };

        cout 
    << cnt <<endl;
        
    return 0;
    }

  • 相关阅读:
    用Webshell直接杀入内网
    别再说找不到Python练手项目了,这80个拿去过冬
    亲爱的,我是一条Linux运维技术学习路径呀。
    一份C++学习资源,咬牙切齿地好用呀
    技术变现,到底怎么变?本文或能成为你的“点金石”
    40个大数据学习资源,个个是干货,最后7个太给力
    零基础如何学好数据分析?
    8个程序员专用软件/网站,个个是神器,第一个最惊喜......
    如何写一个优秀的GitHub项目README文档?
    超全PHP学习资源整理:入门到进阶系列
  • 原文地址:https://www.cnblogs.com/asuran/p/1579397.html
Copyright © 2011-2022 走看看