H - Equations

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 -4
1 1 1 1

Sample Output

39088
0

先介绍一下中途相遇法:

中途相遇法,我的看法是用空间换取时间的一种算法(具体的我也没查)。

解决这道题第一种方法很容易就能想到四层暴力循环,因为我一直秉持着暴力出奇迹的想法,然后就超时了。。

先来说一下一些特殊的情况吧,比如当a,b,c,d全部大于0或者小于零的时候是不成立的,所以不需要跑,只需要进行简单的判断即可,因为数据量比较大所以这一步的判断是非常重要的!!!

1.暴力破解----超时

2.第一次优化,枚举a,b,c,对d进行二分查找来确定,如果不加刚才讨论的特殊情况是会超时的,加上就没问题了;

时间复杂度:O(n^3*log(n))//我也不太会判断,可能是吧(:笑哭

#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
int arr[maxn];
int max_sum[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
    if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
        return 1;
    }
    return 0;
}
int main(){


    while(~scanf("%d %d %d %d",&a,&b,&c,&d)){
        
            
            if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0)){
                cout<<0<<endl;
                continue;
            }
            int num=0;
            map<int,int> mp;
            for(int i=1;i<=100;i++){
                for(int j=1;j<=100;j++){
                    for(int k=1;k<=100;k++){
                        int temp=(a*i*i+b*j*j+c*k*k);
//                        if(!mp.count(temp)){
//                            mp[temp]++;
//                        }else{
//                            mp[temp]=1;
//                        }

                        int left=1,right=100;
                        while(left<=right){
                            int mid=(right+left)/2;
                            if(temp>=0){
                                if(d*mid*mid+temp>0){
                                    left=mid+1;
                                }else if(d*mid*mid+temp==0){
                                    num++;
                                    break;
                                }else{
                                    right=mid-1;
                                }
                            }else{
                                if(d*mid*mid+temp<0){
                                    left=mid+1;
                                }else if(d*mid*mid+temp==0){
                                    num++;
                                    break;
                                }else{
                                    right=mid-1;
                                }

                            }
                        }



                    }
                }
            }




            printf("%d\n",num*16);
    }

    return 0;
}

3.采用map,先枚举 a,b,存入map中再枚举c,d进行查找,如果符合就计入;同样如果不加判断的特殊情况还是会超时,加上就OK了;

时间复杂度:O(n^2*logn(n) )

#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
int arr[maxn];
int max_sum[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
    if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
        return 1;
    }
    return 0;
}
int main(){


    while(~scanf("%d %d %d %d",&a,&b,&c,&d)){
        if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0)){
                cout<<0<<endl;
                continue;
            }
            int num=0;
            map<int,int> mp;
            for(int i=1;i<=100;i++){
                for(int j=1;j<=100;j++){
                        int temp=(a*i*i+b*j*j);
                        if(mp.count(temp)){
                            mp[temp]++;
                        }else{
                            mp.insert(make_pair(temp,1));
                        }

                }
            }

            for(int i=1;i<=100;i++){
                for(int j=1;j<=100;j++){
                    int temp=(c*i*i+d*j*j)*(-1);
                    if(mp.count(temp)){
                        num+=mp[temp];
                    }
                }
            }






            printf("%d\n",num*16);
    }

    return 0;
}

4.将map换成unordered_map,因为不会进行排序,所以会快点,但不幸的是还是超时了,庆幸的是,加上讨论的特殊情况就不会超时了;

#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
#include <unordered_map>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
int arr[maxn];
int max_sum[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
    if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
        return 1;
    }
    return 0;
}
int main(){


    while(~scanf("%d %d %d %d",&a,&b,&c,&d)){
            if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0)){
                cout<<0<<endl;
                continue;
            }
            int num=0;
            unordered_map<int,int> mp;
            for(int i=1;i<=100;i++){
                for(int j=1;j<=100;j++){
                        int temp=(a*i*i+b*j*j);
                        if(mp.count(temp)){
                            mp[temp]++;
                        }else{
                            mp.insert(make_pair(temp,1));
                        }

                }
            }

            for(int i=1;i<=100;i++){
                for(int j=1;j<=100;j++){
                    int temp=(c*i*i+d*j*j)*(-1);
                    if(mp.count(temp)){
                        num+=mp[temp];
                    }
                }
            }






            printf("%d\n",num*16);
    }

    return 0;
}

5.采用hash,我到这里才觉得真正用到了中途相遇的思想,用空间来换取时间,

定义两个hash数组,和使用map的方法类似,但是hash用的是数组的下标,所以必须保证是正数,分开存储是比较好的方法;

第一步,如果a*i*i+b*j*j>=0的话,就存入hash1[]中,<0就取绝对值存入hash2[]中;

第二布,枚举x3,x4,如果c*x3*x3+d*x4*x4>0的话,因为必须和是0,所以num加的是存储负数的哈希数组对应的值,num+=hash2[temp];如果<0,num+=hash1[-temp]

时间复杂度:O(n^2)

#include <iostream>
#include <set>
#include <cstdio>
#include <map>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits.h>
//#include <unordered_map>
using namespace std;
typedef long long LL;
const int maxn=1000000+100;
int Hash1[maxn];//>0
int Hash2[maxn];
int a,b,c,d;
int fuc(int x1,int x2,int x3,int x4){
    if(a*x1*x1+b*x2*x2+c*x3*x3+d*x4*x4==0){
        return 1;
    }
    return 0;
}
int main(){


    while(~scanf("%d %d %d %d",&a,&b,&c,&d)){
            if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0)){
                cout<<0<<endl;
                continue;
            }
            memset(Hash1,0,sizeof(Hash1));
            memset(Hash2,0,sizeof(Hash2));
            int num=0;
            for(int i=1;i<=100;i++){
                for(int j=1;j<=100;j++){
                        int temp=(a*i*i+b*j*j);
                        if(temp>=0) Hash1[temp]++;
                        else Hash2[-temp]++;

                }
            }

            for(int i=1;i<=100;i++){
                for(int j=1;j<=100;j++){
                    int temp=c*i*i+d*j*j;
                    if(temp>0) num+=Hash2[temp];
                    else num+=Hash1[-temp];

                }
            }


            printf("%d\n",num*16);
    }

    return 0;
}

比较重要的就是那个特殊情况的判断,真的十分重要!

好了,差不多了,有错的地方欢迎大佬留言! 

 

 

 

圣维南方程组的程序实现需要一定的数学基础和编程经验。以下是一份使用Python语言实现的圣维南方程组的代码示例,供参考: ```python import numpy as np import matplotlib.pyplot as plt # 参数设置 g = 9.81 # 重力加速度 L = 1.0 # 河道长度 H0 = 0.2 # 初始河道深度 dx = 0.1 # 空间步长 dt = 0.01 # 时间步长 t_max = 5 # 最大模拟时间 # 空间网格 x = np.arange(0, L+dx, dx) N = len(x) # 初始条件 h = H0 * np.ones(N) # 河道深度 hu = np.zeros(N) # 河道动量 hv = np.zeros(N) # 河道动量 # 时间网格 t = np.arange(0, t_max, dt) M = len(t) # 模拟主循环 for n in range(M-1): # 计算水流速度 u = hu / h v = hv / h # 计算水流动量 q = h * u # 计算水流加速度 dhdt = -np.gradient(q, dx) dudt = -g * np.gradient(h, dx) - u * np.gradient(u, dx) - v * np.gradient(u, dx) dvdt = -g * np.gradient(h, dx) - u * np.gradient(v, dx) - v * np.gradient(v, dx) # 更新河道深度和动量 h += dhdt * dt hu += dudt * dt hv += dvdt * dt # 边界条件 h[0] = H0 hu[0] = 0 hv[0] = 0 h[-1] = H0 hu[-1] = 0 hv[-1] = 0 # 绘图 plt.clf() plt.plot(x, h, label=&#39;h&#39;) plt.plot(x, u, label=&#39;u&#39;) plt.plot(x, v, label=&#39;v&#39;) plt.legend() plt.title(&#39;t = {:.2f}&#39;.format(t[n])) plt.xlabel(&#39;x&#39;) plt.ylabel(&#39;h/u/v&#39;) plt.grid() plt.pause(0.01) ``` 上述代码中,我们使用了NumPy库和Matplotlib库来进行数值计算和绘图。首先,我们设置了一些模拟参数,包括重力加速度、河道长度、初始河道深度、空间步长、时间步长和最大模拟时间。然后,我们定义了空间网格和初始条件,并在主循环中进行数值计算和更新。最后,我们使用Matplotlib库来绘制河道深度、水流速度和水流动量随空间的变化情况,并通过动态展示的方式来观察模拟结果。 需要注意的是,上述代码仅为示例代码,可能存在一些不足之处。在实际应用中,需要根据具体问题进行适当的修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值