hdu 1496 && poj 1840 (简单哈希)

来源:互联网 发布:淘宝怎么写标题 编辑:程序博客网 时间:2024/06/06 20:13

Equations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5573    Accepted Submission(s): 2209


Problem Description
Consider equations having the following form: 

a*x1^2+b*x2^2+c*x3^2+d*x4^2=0
a, b, c, d are integers from the interval [-50,50] and any of them cannot be 0.

It is consider a solution a system ( x1,x2,x3,x4 ) that verifies the equation, xi is an integer from [-100,100] and xi != 0, any i ∈{1,2,3,4}.

Determine how many solutions satisfy the given equation.
 

Input
The input consists of several test cases. Each test case consists of a single line containing the 4 coefficients a, b, c, d, separated by one or more blanks.
End of file.
 

Output
For each test case, output a single line containing the number of the solutions.
 

Sample Input
1 2 3 -41 1 1 1
 

Sample Output
390880
 

给出一个含有8个为知参数的等式  参数都是范围的

给出其中四个系数的值 求另外四个参数满足该等式的总可能数

首先将等式变性  a*x1^2+b*x2^2=-c*x3^2-d*x4^2

因为数据范围相对较小  所以可以通过两次循环用数组存储等式左边能够出现的所有情况的值

然后再通过两次循环 找出等式右边能够出现的值在数组中出现的情况数 将所有的情况数相加

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#define eps 1e-8#define op operator#define MOD  10009#define MAXN  2000100#define INF 0x7fffffff#define FOR(i,a,b)  for(int i=a;i<=b;i++)#define FOV(i,a,b)  for(int i=a;i>=b;i--)#define REP(i,a,b)  for(int i=a;i<b;i++)#define REV(i,a,b)  for(int i=a-1;i>=b;i--)#define MEM(a,x)    memset(a,x,sizeof a)#define ll __int64using namespace std;int hs[MAXN];int main(){//freopen("ceshi.txt","r",stdin);    int a,b,c,d;    while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)    {        if(a>0&&b>0&&c>0&&d>0||a<0&&b<0&&c<0&&d<0)        {            puts("0");            continue;        }        MEM(hs,0);        int ans=0;        for(int i=1;i<=100;i++)            for(int j=1;j<=100;j++)                hs[a*i*i+b*j*j+1000010]++;        for(int i=1;i<=100;i++)            for(int j=1 ;j<=100;j++)        {            ans+=hs[-c*i*i-d*j*j+1000010];        }        for(int i=1;i<=100;i++)            for(int j=1 ;j<=100;j++)        {            hs[-c*i*i-d*j*j+1000010]=0;        }        printf("%d\n",ans*16);    }    return 0;}


Eqs
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 12551 Accepted: 6148

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

poj 1840 与上题题意一样 只是后面的幂变成了3  参数个数变成了5
所需的数组变得更大  对内存要求高  把数组类型改为short 直接求解  参数不为0
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#define eps 1e-8#define op operator#define MOD  10009#define MAXN  25000010#define INF 0x7fffffff#define FOR(i,a,b)  for(int i=a;i<=b;i++)#define FOV(i,a,b)  for(int i=a;i>=b;i--)#define REP(i,a,b)  for(int i=a;i<b;i++)#define REV(i,a,b)  for(int i=a-1;i>=b;i--)#define MEM(a,x)    memset(a,x,sizeof a)#define ll __int64using namespace std;short hs[MAXN];int main(){//freopen("ceshi.txt","r",stdin);    int a,b,c,d,e;    while(scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)!=EOF)    {        MEM(hs,0);        int ans=0;        for(int i=-50;i<=50;i++)        {            if(i==0)                continue;            for(int j=-50;j<=50;j++)            {                if(j==0)                    continue;                for(int k=-50;k<=50;k++)                {                    if(k==0)                        continue;                    int sum=a*i*i*i+b*j*j*j+c*k*k*k;                    if(sum<0)                        sum+=25000000;                    hs[sum]++;                }            }        }         for(int i=-50;i<=50;i++)         {             if(i==0)                continue;             for(int j=-50;j<=50;j++)             {                 if(j==0)                    continue;                 int sum=-d*i*i*i-e*j*j*j;                 if(sum<0)                    sum+=25000000;                 ans+=hs[sum];             }         }        printf("%d\n",ans);    }    return 0;}


  

0 0
原创粉丝点击