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

  • 相关阅读:
    Oracle 获取创建控制文件的脚本
    ESXi下误删磁盘分区
    Oracle 3DES加密示例
    Oracle job 执行多个存储过程
    解开紧箍咒
    国际关系之美国南海战略意图(转)
    GHOST系统锁定主页常用软件及解决方案
    cut命令详解
    grep命令详解
    awk命令详解
  • 原文地址:https://www.cnblogs.com/asuran/p/1579397.html
Copyright © 2020-2023  润新知