日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ-1840 Eqs Hash

發布時間:2025/6/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ-1840 Eqs Hash 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目大意:

有以下等式:a1*x13+a2*x23+a3*x33+a4*x43+a5*x53=0.x1,x2,x3,x4,x5都就在區間[-50,50]之間的整數,且x1,x2,x3,x4,x5都不等于0.問:給定a1,a2,a3,a4,a5的情況下,x1,x2,x3,x4,x5共有多少種可能的取值?

解題思路:

這道題想了很久,以為是簡單的暴力枚舉呢。。。。然后怎么想都覺得要超時。之后經人提醒,可以把前兩項的結果映射到一個數組里,然后用后三項遍歷查找,然后用暴力寫了,內存幾乎爆掉。。。。

然后知道最好的方法是hash,用了之后發現有點bug。就是當數據全是0的時候,hash表中只有0這一條鏈,這樣在查找的時候需要100*100*100*100*100=100億次,鐵定要超時,因為我的程序跑了64s。。。Orz

之后想到一個方法,可以用一個數組記錄每個數字出現的次數,只要這個數字出現后,只需要count+=這個數字出現的次數就可以了。就可以解決這個問題了。


#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<ctime> #include<cmath> #include<string> using namespace std;#define N 12350 #define MAX 12345 #define MAXSUM 12500000 #define CLR(arr, what) memset(arr, what, sizeof(arr))template<class T> class Hash { private:int Key[N], Head[N], Next[N], Same[N];int top; public:int count;void search( int x);void push(int x);bool pre(int x);void clear(); };template<class T> inline void Hash<T>::clear() {top = 0;count = 0;CLR(Head, -1);CLR(Next, -1);CLR(Same, 0); }template<class T> inline bool Hash<T>::pre(int x) {int temp;temp = abs(x) % MAX;for(int i = Head[temp]; i != -1; i = Next[i]) //記錄重復{if(x == Key[i]){Same[i]++;return true;}}return false; }template<class T> inline void Hash<T>::push(int x) {if(pre(x) == true) //出現過,Same記錄return ;else //沒出現過{int temp;temp = abs(x) % MAX;Key[top] = x;Next[top] = Head[temp];Head[temp] = top;Same[top] = 1;top++;} }template<class T> inline void Hash<T>::search(int x) {int temp;temp = abs(x) % MAX;for(int i = Head[temp]; i != -1; i = Next[i]){if(x == -Key[i])count += Same[i];} } Hash<int> h;int main() {int T;int a, b, c, d, e;int temp;scanf("%d", &T);while(T--){h.clear();scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);for(int i = -50; i <= 50; ++i){for(int j = -50; j <= 50; ++j){if(i == 0 || j == 0)continue;temp = a * i * i * i + b * j * j * j;h.push(temp);}}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)continue;temp = c * i * i * i + d * j * j * j + e * k * k * k;if(temp > MAXSUM || temp < -MAXSUM)continue;h.search(temp);}}}printf("%d\n", h.count);}return 0; }

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的POJ-1840 Eqs Hash的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。