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

  • 相关阅读:
    Boost练习程序(multi_index_container)
    mathematica练习程序(图像取反)
    【转】媒体播放器三大底层架构
    CentOS安装中文支持
    Retrofit2文件上传下载及其进度显示
    Andorid面试问题整理
    5分钟实现Android中更换头像功能
    Android中突发情况Activity数据的保存和恢复
    5分钟让你学会用最高效的工具解析所有Json
    android http 和https请求
  • 原文地址:https://www.cnblogs.com/asuran/p/1579397.html
Copyright © 2011-2022 走看看