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;
    }

  • 相关阅读:
    【源码笔记】BlogEngine.Net 中的权限管理
    Asp.Net MVC 分页、检索、排序整体实现
    AngularJS 初印象------对比 Asp.net MVC
    Asp.Net MVC Filter 实现方式和作用范围控制
    上传图片压缩+水印(文字、图片)+验证码
    2014年及五年计划
    【读书笔记】Asp.Net MVC 上传图片到数据库(会的绕行)
    VS2012 生成事件
    CSS position绝对定位absolute relative
    CSS兼容性解决方法!important的IE7,Firefox问题
  • 原文地址:https://www.cnblogs.com/asuran/p/1579397.html
Copyright © 2011-2022 走看看