
解方程
发布日期:2021-05-06 17:49:04
浏览次数:7
分类:技术文章
本文共 2603 字,大约阅读时间需要 8 分钟。
哈希解法:#include#include #include #define ll long longusing namespace std;const int maxn=1e6+7;int hash1[maxn],hash2[maxn];int main(int argc, char** argv) { int a,b,c,d; while(~scanf("%d%d%d%d",&a,&b,&c,&d)){ if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&d<0&&c<0)){ printf("0\n"); continue; } int ans=0; memset(hash1,0,sizeof(hash1)); memset(hash2,0,sizeof(hash2)); for(int i=1;i<=100;++i){ for(int j=1;j<=100;++j){ int x=a*i*i+b*j*j; if(x>=0) hash1[x]++; else hash2[-x]++; } } for(int i=1;i<=100;++i){ for(int j=1;j<=100;++j){ int x=c*i*i+d*j*j; if(x>0) ans+=hash2[x]; else ans+=hash1[-x]; } } printf("%d\n",ans*16); } return 0;}
二分能用stl的就哟个stl,自己写容易炸
二分解方程
#includeusing namespace std;const int N = 1e5 + 9;int x[N], y[N];int a, b, c, d;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\n";continue; } long long ans = 0; int cnt = 0; for(int i = 1; i <= 100; ++i) for(int j = 1; j <= 100; ++j) x[cnt] = a*i*i + b*j*j, y[cnt++] = c*i*i + d*j*j; sort(x,x+cnt); sort(y,y+cnt); for(int i = 0; i < cnt; ++i) { int l = 0, r = cnt - 1; while(l < r) { int mid = (l + r) / 2; if(x[i] + y[mid] < 0) l = mid + 1; else r = mid; } long long tmp = 0; if(x[i] + y[r] == 0) { ++tmp; int k = r-1; while(y[k] == y[r] && k >= 0) --k,++tmp; k = r + 1; while(y[k] == y[r] && k < cnt) ++k, ++tmp; ans += tmp; while(x[i] == x[i+1] && i+1 < k) ans+=tmp, ++i; } } printf("%lld\n",ans<<4); // ans是4个数全部正的情况 // 4个数可正可负 , 2的4次方 } return 0;}
#includeusing namespace std;const int N = 1e7 + 9;int x[N], y[N];int main(){ int t, n, k; cin >> t; while(t--) { long long ans = 0; int cnt = 0; cin >> n >> k; for(int i = -n; i <= n; ++i) for(int j = -n; j <= n; ++j) x[cnt] = i * i + i + j * j + j, y[cnt++] = k * (i * i + i + j * j + j); sort(x,x+cnt); sort(y,y+cnt); //for(int i = 0; i < cnt; ++i) cout << x[i] << " ";cout << endl; //for(int i = 0; i < cnt; ++i) cout << y[i] << " ";cout << endl; for(int i = 0; i < cnt; ++i) { int pos = lower_bound(y,y+cnt, x[i]) - y; if(pos == cnt) { while(x[i] == x[i+1]) ++i;continue; } long long tmp = 0; if(y[pos] == x[i]) { ++tmp; int k = pos - 1; while(y[pos] == y[k] && k >= 0) --k, ++tmp; k = pos + 1; while(y[pos] == y[k] && k < cnt) ++k, ++tmp; ans += tmp; while(x[i] == x[i+1]) ans += tmp, ++i; } } cout << ans << endl; } return 0;}/*31 11 21 3*/
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月04日 17时16分54秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
R3 PRO 3200G和r7 3700u 哪个好
2019-03-03
入手评测 联想小新Pro14和Air14Plus哪个好?区别对比
2019-03-03
程序人生:没有伞的孩子要学会奔跑
2019-03-03
Express Animate for mac(动画特效制作软件)
2019-03-03
macOS Big Sur系统中如何开启设置触控板三指拖拽功能?
2019-03-03
修复苹果Mac中的快速视频播放错误的方法
2019-03-03
苹果HomePod智能音箱怎么使用广播功能?
2019-03-03
Mac系统投屏到电视机的方法
2019-03-03
【Docker&ARM】ARM架构服务器上docker的安装
2019-03-03
【Tinyproxy】CentOS7.X http代理tinyproxy的安装配置与使用
2019-03-03
php-foreach遍历一维数组
2019-03-03
php--自定义错误处理函数的使用方法
2019-03-03
php--异常处理主动抛出异常的使用方法
2019-03-03
php--简易的相册1
2019-03-03
php---xml基础知识
2019-03-03
php--class static
2019-03-03
php--匿名函数的使用
2019-03-03