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

  • 相关阅读:
    对100以内的两位数,请使用一个两重循环打印出所有十位数数字比个位数数字小的数,
    给定两个不同的变量,不使用中间变量的情况下互换两个变量的值,使用函数直接生成
    将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
    POJ 2138 最长路
    POJ 2133 暴搜
    POJ 1988 带偏移量的并查集
    POJ 1989 贪心
    POJ 1991 DP
    POJ 3175 枚举
    POJ 3168 排序+扫描
  • 原文地址:https://www.cnblogs.com/asuran/p/1579397.html
Copyright © 2011-2022 走看看